Created
December 13, 2019 21:34
-
-
Save tiagolr/6a75af642e2b03ac9c35e24577be612e to your computer and use it in GitHub Desktop.
encryptAES
This file contains hidden or 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
function encrypt (msg, pass) { | |
var salt = CryptoJS.lib.WordArray.random(128/8); | |
var key = CryptoJS.PBKDF2(pass, salt, { | |
keySize: keySize/32, | |
iterations: iterations | |
}); | |
var iv = CryptoJS.lib.WordArray.random(128/8); | |
var encrypted = CryptoJS.AES.encrypt(msg, key, { | |
iv: iv, | |
padding: CryptoJS.pad.Pkcs7, | |
mode: CryptoJS.mode.CBC | |
}); | |
// salt, iv will be hex 32 in length | |
// append them to the ciphertext for use in decryption | |
var transitmessage = salt.toString()+ iv.toString() + encrypted.toString(); | |
return transitmessage; | |
} | |
function decrypt (transitmessage, pass) { | |
var salt = CryptoJS.enc.Hex.parse(transitmessage.substr(0, 32)); | |
var iv = CryptoJS.enc.Hex.parse(transitmessage.substr(32, 32)) | |
var encrypted = transitmessage.substring(64); | |
var key = CryptoJS.PBKDF2(pass, salt, { | |
keySize: keySize/32, | |
iterations: iterations | |
}); | |
var decrypted = CryptoJS.AES.decrypt(encrypted, key, { | |
iv: iv, | |
padding: CryptoJS.pad.Pkcs7, | |
mode: CryptoJS.mode.CBC | |
}) | |
return decrypted; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment