Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var crypto = require('crypto'),
- algorithm = 'aes-128-cbc',
- password = 'zW7f5YXELwlZMEDvkaeGe1nu51QbAwhB';
- var secretKey = 'zW7f5YXELwlZMEDvkaeGe1nu51QbAwhB';
- // var crypto = require('crypto'),
- // algorithm = 'aes-256-ctr',
- // password = 'd6F3Efeq';
- const key = crypto.createHash('sha256').update(password).digest('base64').substr(0, 16);
- function encrypt(buffer){
- var cipher = crypto.createCipher(algorithm,key)
- var crypted = Buffer.concat([cipher.update(buffer),cipher.final()]);
- return crypted;
- }
- function decrypt(buffer){
- var decipher = crypto.createDecipher(algorithm,key)
- var dec = Buffer.concat([decipher.update(buffer) , decipher.final()]);
- return dec;
- }
- function aesDecrypt (value) {
- // try {
- const encryptedText = Buffer.from(value, 'hex');
- console.log("encryptedText",encryptedText ,encryptedText.toString());
- const key = crypto.createHash('sha256').update(secretKey).digest('base64').substr(0, 16);
- // decipher the string
- console.log("key",key);
- const decipher = crypto.createDecipher(algorithm, Buffer.from(key));
- console.log("decipher",decipher);
- let decrypted = decipher.update(encryptedText, 'hex', 'utf8');
- console.log("decrypted",decrypted, decrypted.length);
- decrypted += decipher.final();
- console.log("decrypted",decrypted, decrypted.length);
- return decrypted;
- // } catch (err) {
- // // in case failed decryption, return the encrypted itself
- // return value;
- // }
- }
- function aesEncrypt(value) {
- // try {
- const key = crypto.createHash('sha256').update(secretKey).digest('base64').substr(0, 16);
- console.log("key",key,Buffer.from(key));
- const cipher = crypto.createCipher(algorithm, Buffer.from(key));
- // cipher.setAutoPadding(false)
- let crypted = cipher.update(value, 'utf8', 'hex');
- console.log("crypted_______________",crypted);
- crypted += cipher.final('hex');
- console.log("crypted",crypted,"_________________", "+++++++",crypted.toString('hex'), value);
- return crypted.toString('hex');
- // } catch (err) {
- // console.log('aesEncryptError', { value, err });
- // // in case failed encryption, return the value itself
- // return value;
- // }
- }
- // var hw = encrypt(new Buffer("NAM_DEP_TRAI_28", "utf8"))
- // outputs hello world
- // console.log(hw.toString());
- // console.log(decrypt(hw).toString('utf8'));
- aesEncrypt("NAM_DEP_TRAI_28");
- aesDecrypt("788b395a68ec33f0ff10d2a790c6c235");
- // aesDecrypt("1bddc75575ea788075fe18538d8e665a")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement