Last active
January 20, 2022 08:16
-
-
Save yoavniran/c78a0991e0152b306c25 to your computer and use it in GitHub Desktop.
nodejs crypto - simple encrypt & decrypt using IV (Initialization Vector)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
var crypto = require("crypto"); | |
var EncryptionHelper = (function () { | |
function getKeyAndIV(key, callback) { | |
crypto.pseudoRandomBytes(16, function (err, ivBuffer) { | |
var keyBuffer = (key instanceof Buffer) ? key : new Buffer(key) ; | |
callback({ | |
iv: ivBuffer, | |
key: keyBuffer | |
}); | |
}); | |
} | |
function encryptText(cipher_alg, key, iv, text, encoding) { | |
var cipher = crypto.createCipheriv(cipher_alg, key, iv); | |
encoding = encoding || "binary"; | |
var result = cipher.update(text, "utf8", encoding); | |
result += cipher.final(encoding); | |
return result; | |
} | |
function decryptText(cipher_alg, key, iv, text, encoding) { | |
var decipher = crypto.createDecipheriv(cipher_alg, key, iv); | |
encoding = encoding || "binary"; | |
var result = decipher.update(text, encoding); | |
result += decipher.final(); | |
return result; | |
} | |
return { | |
CIPHERS: { | |
"AES_128": "aes128", //requires 16 byte key | |
"AES_128_CBC": "aes-128-cbc", //requires 16 byte key | |
"AES_192": "aes192", //requires 24 byte key | |
"AES_256": "aes256" //requires 32 byte key | |
}, | |
getKeyAndIV: getKeyAndIV, | |
encryptText: encryptText, | |
decryptText: decryptText | |
}; | |
})(); | |
module.exports = EncryptionHelper; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var encryptionHelper = require("./simple-nodejs-iv-encrypt-decrypt.js") | |
var story = "this is the story of the brave prince who went off to fight the horrible dragon... he set out on his quest one sunny day"; | |
var algorithm = encryptionHelper.CIPHERS.AES_256; | |
console.log("testing encryption and decryption"); | |
console.log("text is: " + story); | |
encryptionHelper.getKeyAndIV("1234567890abcdefghijklmnopqrstuv", function (data) { //using 32 byte key | |
console.log("got key and iv buffers"); | |
var encText = encryptionHelper.encryptText(algorithm, data.key, data.iv, story, "base64"); | |
console.log("encrypted text = " + encText); | |
var decText = encryptionHelper.decryptText(algorithm, data.key, data.iv, encText, "base64"); | |
console.log("decrypted text = " + decText); | |
assert.equal(decText, story); | |
}); |
Can you use Buffer.from(key) instead, due to its deprecation?
Yes if you have following message : DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
You can use Buffer.from(key) instead new Buffer(key)
But it's working only for variable contenting a string
For variable contenting a number you can change : new Buffer(num) to Buffer.alloc(num)
//CryptoJS example
var key = "H98zM6i/55yNJfkFsbu0HrzlFo17FtR9";
var iv = key.slice(0, 16);
//Create Key
key = CryptoJS.enc.Utf8.parse(key);
//Get Iv
iv = CryptoJS.enc.Utf8.parse(iv);
var encrypted = CryptoJS.AES.encrypt("testtest", key,{ iv: iv});
//Encrypt string
var encrypted_data = encrypted.toString();
console.log(encrypted_data)
var decrypted = CryptoJS.AES.decrypt(encrypted_data, key,{ iv: iv});
var decrypted_data = decrypted.toString(CryptoJS.enc.Utf8)
console.log(decrypted_data)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
missing
var assert = require('assert');
in test.js?