Last active
April 14, 2024 20:51
-
-
Save yakuter/a6d860317b175d6336638ac51a3feaff to your computer and use it in GitHub Desktop.
CryptoJS AES Example
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset='utf-8'> | |
<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 src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/pbkdf2.js"></script> | |
<script> | |
// Code goes here | |
var keySize = 256; | |
var ivSize = 128; | |
var iterations = 100; | |
var message = "Hello World"; | |
var password = "Secret Password"; | |
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(ivSize/8); | |
var encrypted = CryptoJS.AES.encrypt(msg, key, { | |
iv: iv, | |
padding: CryptoJS.pad.Pkcs7, | |
mode: CryptoJS.mode.CBC, | |
hasher: CryptoJS.algo.SHA256 | |
}); | |
// 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, | |
hasher: CryptoJS.algo.SHA256 | |
}) | |
return decrypted; | |
} | |
var encrypted = encrypt(message, password); | |
var decrypted = decrypt(encrypted, password); | |
console.log("Encrypted: "+ encrypted) | |
console.log("Decrypted: "+ decrypted.toString(CryptoJS.enc.Utf8)) | |
</script> | |
</head> | |
<body> | |
</body> | |
</html> |
Way shorter version:
<!DOCTYPE html> <html> <head> <meta charset='utf-8'> <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/aes.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/pbkdf2.js"></script> <script> </script> var encrypted=CryptoJS.AES.encrypt("Hello Encrypted World!", "my secret key").toString() var decrypted=CryptoJS.AES.decrypt(encrypted, "my secret key").toString(CryptoJS.enc.Utf8) console.log("Encrypted: "+ encrypted) console.log("Decrypted: "+ decrypted) </head> <body> </body> </html>
Thank you. This one helped me a lot :)
Way shorter version:
<!DOCTYPE html> <html> <head> <meta charset='utf-8'> <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/aes.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/pbkdf2.js"></script> <script> </script> var encrypted=CryptoJS.AES.encrypt("Hello Encrypted World!", "my secret key").toString() var decrypted=CryptoJS.AES.decrypt(encrypted, "my secret key").toString(CryptoJS.enc.Utf8) console.log("Encrypted: "+ encrypted) console.log("Decrypted: "+ decrypted) </head> <body> </body> </html>
Thank you. This one helped me a lot :)
I guessed that, that's why I posted it. I like "linear" things like that too.
There is a mistake in the code!
The hasher is part of the key derivation function not part of the encrypted function
var key = CryptoJS.PBKDF2(pass, salt, {
keySize: keySize/32,
iterations: iterations,
hasher: CryptoJS.algo.SHA256
});
var iv = CryptoJS.lib.WordArray.random(ivSize/8);
var encrypted = CryptoJS.AES.encrypt(msg, key, {
iv: iv,
padding: CryptoJS.pad.Pkcs7,
mode: CryptoJS.mode.CBC
});
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Way shorter version: