Reusable pieces of code and docs pointers to get started with DES encryption in node.

DES-ECB

1
2
3
4
5
6
7
8
9
10
11
function encodeDesECB(textToEncode, keyString) {
var key = new Buffer(keyString.substring(0, 8), 'utf8');
var cipher = crypto.createCipheriv('des-ecb', key, '');
var c = cipher.update(textToEncode, 'utf8', 'base64');
c += cipher.final('base64');
return c;
}

The key needs to be 8-bit, you can change the code to use 'ascii', 'utf8', 'utf16le', 'ucs2', 'base64', 'binary' or 'hex'. More info here

For the input and output encodings in cipher.update(data[, input_encoding][, output_encoding]), check the documentation here

For the final encoding in cipher.final([output_encoding]), check the documentation here

DES-CBC

1
2
3
4
5
6
7
8
9
10
11
12
13
function encodeDesCBC(textToEncode, keyString, ivString) {
var key = new Buffer(keyString.substring(0, 8), 'utf8');
var iv = new Buffer(ivString.substring(0, 8), 'utf8');
var cipher = crypto.createCipheriv('des-cbc', key, iv);
var c = cipher.update(textToEncode, 'utf8', 'base64');
c += cipher.final('base64');
return c;
}

The iv (Initialization Vector) needs to be 8-bit, same as the key.