Skip to content

Instantly share code, notes, and snippets.

@csanz
Created August 30, 2011 16:06
Show Gist options
  • Save csanz/1181250 to your computer and use it in GitHub Desktop.
Save csanz/1181250 to your computer and use it in GitHub Desktop.
Simple String Encryption & Decryption with Node.js
function encrypt(text){
var cipher = crypto.createCipher('aes-256-cbc','d6F3Efeq')
var crypted = cipher.update(text,'utf8','hex')
crypted += cipher.final('hex');
return crypted;
}
function decrypt(text){
var decipher = crypto.createDecipher('aes-256-cbc','d6F3Efeq')
var dec = decipher.update(text,'hex','utf8')
dec += decipher.final('utf8');
return dec;
}
var hw = encrypt("hello world")
decrypt(hw)
// feel free to change >> d6F3Efeq
// To test just copy + paste the above inside the node shell
// TIP: always encrypt IDs before sending via HTTP
@Maj0rrush
Copy link

Hey i use this to convert long urls in a handy format :-) Works like a charme thx !

@jrgleason
Copy link

Thanks this is great! IV should be used as well but is really just making it more safer. http://crypto.stackexchange.com/questions/732/why-use-an-initialization-vector-iv

@Jmlevick
Copy link

Just made a npm module inspired by this example and used crypto.createCipheriv() for initialization vector (IV) support, (thus, more secure encryption) check it out at: https://www.npmjs.com/package/node-cryptex

@praveenguptavi081
Copy link

Hey Thanks all,
it would be great if someone give a example here

@shamii087
Copy link

shamii087 commented Apr 23, 2018

var crypto = require("crypto")
var encrypt_decrypt={
"encrypt": function ('password'){
var key = "123|a123123123123123@&";
var cipher = crypto.createCipher('aes-256-cbc', key);
var crypted = cipher.update(data, 'utf-8', 'hex');
crypted += cipher.final('hex');
return crypted;
},
"decrypt":function('password') {
var key = "123|a123123123123123@&";
var decipher = crypto.createDecipher('aes-256-cbc', key);
var decrypted = decipher.update(data, 'hex', 'utf-8');
decrypted += decipher.final('utf-8');
return decrypted;
}
}
module.exports = encrypt_decrypt;

@praveenguptavi081 : You can use above code in any files where ever you want, Just requre this file.

feel free to change this: var key = "123|a123123123123123@&";
Thanks.

@MasterJames
Copy link

Just wondering how you encrypt from the client browser (and know the cipher to matchup). This shows a way to decrypt in nodejs.

@PELock
Copy link

PELock commented Feb 22, 2019

One way encryption

<script type="text/javascript">
// encrypted with https://www.stringencrypt.com (v1.3.0) [JavaScript]
// message = "hello world"
var message = "\u1EBF\u1A63\u027F\u02A7\u1F4F\uD8EB\u260F\u1E2F\u27DF\u0283\u1B9F";
 
for (var psNxK = 0, ZoVYc = 0; psNxK < 11; psNxK++)
{
        ZoVYc = message.charCodeAt(psNxK);
        ZoVYc -= psNxK;
        ZoVYc -= 0x58DD;
        ZoVYc ^= psNxK;
        ZoVYc -= psNxK;
        ZoVYc -= 0xFE90;
        ZoVYc ^= psNxK;
        ZoVYc = (((ZoVYc & 0xFFFF) >> 5) | (ZoVYc << 11)) & 0xFFFF;
        ZoVYc += 0xCF2E;
        ZoVYc ^= 0x7189;
        ZoVYc --;
        ZoVYc ^= 0x7FE4;
        ZoVYc ^= psNxK;
        ZoVYc -= 0xAADC;
        ZoVYc = (((ZoVYc & 0xFFFF) >> 14) | (ZoVYc << 2)) & 0xFFFF;
        ZoVYc --;
        ZoVYc = (((ZoVYc & 0xFFFF) >> 2) | (ZoVYc << 14)) & 0xFFFF;
        ZoVYc -= psNxK;
        ZoVYc ^= psNxK;
        ZoVYc --;
        ZoVYc ^= psNxK;
        ZoVYc ^= 0x8366;
        ZoVYc ^= psNxK;
        ZoVYc --;
        ZoVYc = ((ZoVYc << 13) | ( (ZoVYc & 0xFFFF) >> 3)) & 0xFFFF;
        message = message.substr(0, psNxK) + String.fromCharCode(ZoVYc & 0xFFFF) + message.substr(psNxK + 1);
}
 
alert(message);
</script>

@guneaditi
Copy link

what is the equivalent code for the same in ASP.net?

@hitautodestruct
Copy link

Hey

DeprecationWarning: crypto.createCipher is deprecated.

Please note that createCipher has been deprecated so use this instead.
Taken from this SO answer

const crypto = require('crypto');
const algorithm = 'aes-256-ctr';
const ENCRYPTION_KEY = 'Put_Your_Password_Here'; // or generate sample key Buffer.from('FoCKvdLslUuB4y3EZlKate7XGottHski1LmyqJHvUhs=', 'base64');
const IV_LENGTH = 16;

function encrypt(text) {
    let iv = crypto.randomBytes(IV_LENGTH);
    let cipher = crypto.createCipheriv(algorithm, Buffer.from(ENCRYPTION_KEY, 'hex'), iv);
    let encrypted = cipher.update(text);
    encrypted = Buffer.concat([encrypted, cipher.final()]);
    return iv.toString('hex') + ':' + encrypted.toString('hex');
}

function decrypt(text) {
    let textParts = text.split(':');
    let iv = Buffer.from(textParts.shift(), 'hex');
    let encryptedText = Buffer.from(textParts.join(':'), 'hex');
    let decipher = crypto.createDecipheriv(algorithm, Buffer.from(ENCRYPTION_KEY, 'hex'), iv);
    let decrypted = decipher.update(encryptedText);
    decrypted = Buffer.concat([decrypted, decipher.final()]);
    return decrypted.toString();
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment