Skip to content

Instantly share code, notes, and snippets.

@19h
Created June 20, 2013 14:51
Show Gist options
  • Select an option

  • Save 19h/5823413 to your computer and use it in GitHub Desktop.

Select an option

Save 19h/5823413 to your computer and use it in GitHub Desktop.
AES CBC 256 using Node.js [crypto]
/*
p is plaintext (non-encrypted value) OR CIPHER (already encrypted text)
t is either false [cbc256("value", false, "key")] for encryption or true for decryption
k is key.
*/
/*
Example:
val = "value";
v = cbc256(val, false, "key");
v_ = cbc256(v, true, "key")
// val == v_
*/
cbc256 = function (p, t, k) {
t = t || 0;
crypto = this["crypto"] || require("crypto");
z = "";
if (!t) {
_c = crypto.createCipher("aes-256-cbc", k);
return _c.update(p, "utf8", "hex"), _c["final"]("hex");
} else {
_c = crypto.createDecipher("aes-256-cbc", k);
return _c.update(p, "hex", "utf8"), _c["final"]("utf8");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment