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

DES-ECB

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

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.