Introduction for Crypto-JS and a common issue
original post date: July 26, 2016
Overview
Using JS encryption is a viable option when HTTPS is not available, or when front-end security needs to be increased.
Crypto-js supports Hashes like MD5/SHA-1/SHA-2/SHA-3/HMAC/PBKDF2, and ciphers like AES/DES/3DES/Rabbit/RC4/RC4Drop, with optional Block Modes and Padding. It can load a single mode only.
Link: https://code.google.com/archive/p/crypto-js/ && https://github.com/brix/crypto-js
MD5
md5.js
can be referenced separately.
1 | CryptoJS.MD5("test"); |
PBKDF2
pbkdf2.js
can be referenced separately.
1 | // official examples |
AES
The default is AES-CBC-Pkcs5 (PKcs7), while other ciphers can be referenced separately. The key length is automatically determined with the length of the password entered. If using [Passphrase(https://en.wikipedia.org/wiki/Passphrase)], it's 256 bits.
Encryption: (here's the issue mentioned before)
1 | var str = '123456'; // content to encrypt |
Decryption: 1
2
3
4
5
6
7
8var decrypted = CryptoJS.AES.decrypt(encrypted, key, {
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
// to utf-8 string
decrypted = CryptoJS.enc.Utf8.stringify(decrypted);
Other Ciphers
It's basically the same, except that Rabbit and RC4 don't support defining mode and padding.
References:
[1] https://blog.zhengxianjun.com/2015/05/javascript-crypto-js/
[2] http://stackoverflow.com/questions/35529804/using-crypto-js-to-encrypt-password-and-send-form-via-ajax-and-decrypt-in-java