Crypto-js JavaScript crypto library:
Example:
/*
// all AES's scripts
<script type="text/javascript" src="js/crypto.js"></script>
<script type="text/javascript" src="js/sha1.js"></script>
<script type="text/javascript" src="js/hmac.js"></script>
<script type="text/javascript" src="js/pbkdf2.js"></script>
<script type="text/javascript" src="js/blockmodes.js"></script>
<script type="text/javascript" src="js/aes.js"></script>
*/
// or a single script that encompasses all the scripts
<script type="text/javascript" src="js/aes-full.js"></script>
<script type="text/javascript">
/*** encrypt */
var ciphertext = Crypto.AES.encrypt("plaintext", "Secret Passphrase");
/*** decrypt */
var plaintext = Crypto.AES.decrypt("ciphertext", "Secret Passphrase");
</script>
The modes of operation currently available are:
- ECB
- CBC
- CFB
- OFB (the default)
- CTR
And the padding schemes currently available are:
- iso7816 (the default)
- ansix923
- iso10126
- pkcs7
- ZeroPadding
- NoPadding
Bugs in the code:
29 Mag 2012, 22:16 - [ Padding schemes corrupted ]
ZeroPadding
<script type="text/javascript" src="js/2.5.3-crypto-sha1-hmac-pbkdf2-blockmodes-aes.js"></script>
<script type="text/javascript">
var plaintext = "plaintext" // this is 9 bytes
/*** encrypt */
var ciphertext = Crypto.AES.encrypt(plaintext, "Secret Passphrase",
{ mode: new Crypto.mode.OFB(Crypto.pad.ZeroPadding) });
// ciphertext --> "SwcOSeXEGMIQUKEn7ibuoV28KfNXBPH3tngp3ORySr0="
/*** decrypt */
var plain = Crypto.AES.decrypt(ciphertext, "Secret Passphrase",
{ mode: new Crypto.mode.OFB(Crypto.pad.ZeroPadding) });
// plain --> "plaintext???????" // this is 16 bytes
/*** verification */
if (plaintext .length != plain.length) {
// ERROR: the plaintext does not match.
} else { // same. }
// The plaintext is different with this padding schemes (ZeroPadding).
</script>
3 Dic 2011, 12:06 - [ Error encrypt / decrypt CTR ]
bug fixed in version 2.5.3
<script type="text/javascript" src="js/2.4.0-crypto-sha1-hmac-pbkdf2-blockmodes-aes.js"></script>
<script type="text/javascript">
/*** encrypt */
var ciphertext = Crypto.AES.encrypt("plaintext", "Secret Passphrase", { mode: new Crypto.mode.CTR });
/*** decrypt */
var plaintext = Crypto.AES.decrypt("ciphertext", "Secret Passphrase", { mode: new Crypto.mode.CTR });
/* outoput error: */
malformed URI sequence 6-a*2);return j}};n=n.charenc={};n.UTF...rCode(f[b]));return
j.join("")}}})(); <- 2.4.0-...-aes.js (line 10)
</script>