Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ar crypto = require('crypto');
- var algorithm = 'aes-128-ecb';
- var key = '1234';
- /*-----------------------------------------------------------*
- * ENCRYPT: AES 128 bit, CBC *
- *-----------------------------------------------------------*/
- var data = "jugs";
- var ciphertext = _encrypt(data, key, algorithm);
- console.log(ciphertext);
- /*-----------------------------------------------------------*
- * DECRYPT: AES 128 bit, CBC *
- *-----------------------------------------------------------*/
- var plain = _decrypt(ciphertext, key, algorithm);
- /********************************************************************
- IMPLEMENTING FUNCTIONS...
- ********************************************************************/
- function _encrypt(data, key, algorithm)
- {
- var clearEncoding = 'utf8';
- var cipherEncoding = 'base64'; // hex, base64
- var cipher = crypto.createCipher(algorithm, key);
- var cipherChunks = [];
- cipherChunks.push(cipher.update(data, clearEncoding, cipherEncoding));
- cipherChunks.push(cipher.final(cipherEncoding));
- return (cipherChunks);
- }
- function _decrypt(ciphertext, key, algorithm)
- {
- var clearEncoding = 'utf8';
- var cipherEncoding = 'base64'; // hex, base64
- var decipher = crypto.createDecipher(algorithm, key);
- var plainChunks = [];
- for (var i = 0;i < ciphertext.length;i++) {
- plainChunks.push(decipher.update(ciphertext[i], cipherEncoding, clearEncoding));
- }
- plainChunks.push(decipher.final(clearEncoding));
- var plaintext = plainChunks.join('');
- return (plaintext);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement