NodeJS DES ECB Encryption Of Hex Data With Module 'crypto'
I am trying to encrypt/decrypt hex data using node js module 'crypto' with DES-ECB algorithm. In the official 'crypto' documentation, they give an example of aes-192 encryption in CBC mode (cf attached code) but with the ECB mode no iv (initilization vector) is needed. I don't know how to adapt this code to encrypt in ECB mode with the DES algorithm.
Here's the JavaScript code given in the official documentation of node js 'crypto' module (https://nodejs.org/dist/latest-v10.x/docs/api/crypto.html#crypto_class_cipher):
const crypto = require('crypto');
const algorithm = 'aes-192-cbc';
const password = 'Password used to generate key';
// Key length is dependent on the algorithm. In this case for aes192, it is
// 24 bytes (192 bits).
// Use async `crypto.scrypt()` instead.
const key = crypto.scryptSync(password, 'salt', 24);
// Use `crypto.randomBytes()` to generate a random iv instead of the static iv
// shown here.
const iv = Buffer.alloc(16, 0); // Initialization vector.
const cipher = crypto.createCipheriv(algorithm, key, iv);
let encrypted = '';
cipher.on('readable', () => {
let chunk;
while (null !== (chunk = cipher.read())) {
encrypted += chunk.toString('hex');
}
});
cipher.on('end', () => {
console.log(encrypted);
// Prints: e5f79c5915c02171eec6b212d5520d44480993d7d622a7c4c2da32f6efda0ffa
});
cipher.write('some clear text data');
cipher.end();
In my case I have to change the "algorithm" field to:
const algorithm = 'des-ecb'
But then I also have to adapt the parts dealing with the iv (as no iv is needed with DES-ECB algorithm) and I don't know how to do that...
Thanks a lot!
Answer
You can try this approach, this should do what you want:
const crypto = require('crypto');
const algorithm = 'des-ecb';
const password = 'some password';
// use a hex key here
const key = Buffer.from("d0e276d0144890d3", "hex");
const cipher = crypto.createCipheriv(algorithm, key, null);
let encrypted = cipher.update("Those are my principles, and if you don't like them... well, I have others.", 'utf8', 'hex');
encrypted += cipher.final('hex');
console.log("Encrypted: ", encrypted);
const decipher = crypto.createDecipheriv(algorithm, key, null);
let decrypted = decipher.update(encrypted, 'hex', 'utf8');
decrypted += decipher.final('utf8');
console.log("Decrypted: ", decrypted);
Related Questions
- → Maximum call stack exceeded when instantiating class inside of a module
- → Browserify api: how to pass advanced option to script
- → Node.js Passing object from server.js to external modules?
- → gulp-rename makes copies, but does not replace
- → requiring RX.js in node.js
- → Remove an ObjectId from an array of objectId
- → Can not connect to Redis
- → React: How to publish page on server using React-starter-kit
- → Express - better pattern for passing data between middleware functions
- → Can't get plotly + node.js to stream data coming through POST requests
- → IsGenerator implementation
- → Async/Await not waiting
- → (Socket.io on nodejs) Updating div with mysql data stops without showing error