Created
August 10, 2016 06:51
-
-
Save dudepare/99f4d682c3db90ea86a4331e32f0006a to your computer and use it in GitHub Desktop.
This is a sample of AES encryption + decryption using CryptoJS. This is a bit different from the examples out there because this one uses a COMPANY header + payload. There's a little bit more processing going on. I also used a CDN for CryptoJS. I hope someone finds this useful.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/components/core-min.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/components/sha256.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/components/enc-base64.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/aes.js"></script> | |
<script> | |
function toWordArray(str){ | |
return CryptoJS.enc.Utf8.parse(str); | |
} | |
function toString(words){ | |
return CryptoJS.enc.Utf8.stringify(words); | |
} | |
function toBase64String(words){ | |
return CryptoJS.enc.Base64.stringify(words); | |
} | |
function encrypt(input, key){ | |
var PROTOCOL_AES256 = 2; | |
var secret_key = CryptoJS.SHA256(key); | |
var header = toWordArray("AMAZON" + String.fromCharCode(PROTOCOL_AES256)); | |
var iv = CryptoJS.lib.WordArray.random(16); | |
var body = CryptoJS.AES.encrypt(json_payload, secret_key, {iv: iv}); | |
// construct the packet | |
// HEADER + IV + BODY | |
header.concat(iv); | |
header.concat(body.ciphertext); | |
// encode in base64 | |
return toBase64String(header); | |
} | |
function decrypt(input, key){ | |
// convert payload encoded in base64 to words | |
var packet = CryptoJS.enc.Base64.parse(input); | |
// helpers to compute for offsets | |
var PROTOCOL_AES256 = 2; | |
var secret_key = CryptoJS.SHA256(key); | |
var header = toWordArray("AMAZON" + String.fromCharCode(PROTOCOL_AES256)); | |
var iv = CryptoJS.lib.WordArray.random(16); | |
// compute for offsets | |
var packet_size = packet.words.length - (iv.words.length + header.words.length); | |
var start = iv.words.length + header.words.length; | |
var end = packet.words.length; | |
var ciphertext = CryptoJS.lib.WordArray.create(packet.words.slice(start, end)); | |
var parsed_iv = CryptoJS.lib.WordArray.create(packet.words.slice(header.words.length, iv.words.length+1)); | |
ciphertext = toBase64String(ciphertext); | |
var decrypted = CryptoJS.AES.decrypt(ciphertext, secret_key, {iv: parsed_iv}); | |
return toString(decrypted); | |
} | |
// data | |
var key = "ZxlNEnojO5HbQngiYvrqu32Br6V"; | |
var data = {}; | |
data["name"] = "andrew"; | |
data["company"] = "amazon"; | |
var json_payload = JSON.stringify(data); | |
console.log(json_payload); | |
var payload = encrypt(json_payload, key); | |
var decoded = decrypt(payload, key); | |
console.log(decoded); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think this fix is right.