Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var crypto = require('crypto');
- const algorithm = 'aes-128-ecb';
- //const algorithm = 'aes-128-cbc';
- function encrypt(key, iv, plaintext) { // IV is ignored in ECB mode
- var cipher = crypto.createCipheriv(algorithm, key, iv);
- cipher.setAutoPadding(false); // IMPORTANT: By default OpenSSL uses PKCS#7 padding. (http://www.di-mgt.com.au/cryptopad.html)
- var ciphertext = Buffer.concat([cipher.update(plaintext), cipher.final()]);
- return ciphertext;
- }
- function decrypt(key, iv, ciphertext) {
- var decipher = crypto.createDecipheriv(algorithm, key, iv);
- decipher.setAutoPadding(false); // IMPORTANT: By default OpenSSL uses PKCS#7 padding.
- var plaintext = Buffer.concat([decipher.update(ciphertext), decipher.final()]);
- return plaintext;
- }
- var plaintext = new Buffer("OvoJeSkrivenTxt1OvoJeSkrivenTxt2", "utf8");
- console.log("Plaintext: ", plaintext.toString());
- var salt = "Ovo je moja sol.";
- crypto.pbkdf2("hunter2", salt, 60000, 16, "sha256", function(err, key){
- console.log("Key: ", key);
- var ciphertext = encrypt(key, iv, plaintext);
- console.log("Encrypted: ", ciphertext);
- var decryptedtext = decrypt(key, iv, ciphertext);
- console.log("Decrypted: ", decryptedtext.toString());
- });
- //var key = crypto.randomBytes(16);
- var iv = new Buffer("");
- //console.log("IV: ", iv);
- /*
- console.log("Key: ", key);
- var ciphertext = encrypt(key, iv, plaintext);
- console.log("Encrypted: ", ciphertext);
- var decryptedtext = decrypt(key, iv, ciphertext);
- console.log("Decrypted: ", decryptedtext.toString());
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement