I am making an application which needs Java based AES Encryption and JavaScript based decryption. I am using the following code for encryption as a basic form.
p
For Java and JavaScript to able to inter operate, it is essential that no defaults are used while creating Key or the Cipher. The iteration count, key length, padding, salt and IV should all be the same.
Reference: https://github.com/mpetersen/aes-example
Sample code below:
Encrypting String in Java:
String keyValue = "Abcdefghijklmnop";
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
KeySpec spec = new PBEKeySpec(keyValue.toCharArray(), hex("dc0da04af8fee58593442bf834b30739"),
1000, 128);
Key key = new SecretKeySpec(factory.generateSecret(spec).getEncoded(), "AES");
Cipher c = Cipher.getInstance(“AES/CBC/PKCS5Padding”);
c.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(hex("dc0da04af8fee58593442bf834b30739")));
byte[] encVal = c.doFinal("The Quick Brown Fox Jumped over the moon".getBytes());
String base64EncodedEncryptedData = new String(Base64.encodeBase64(encVal));
System.out.println(base64EncodedEncryptedData);
}
Decrypting the same string in JavaScript:
var iterationCount = 1000;
var keySize = 128;
var encryptionKey ="Abcdefghijklmnop";
var dataToDecrypt = "2DZqzpXzmCsKj4lfQY4d/exg9GAyyj0hVK97kPw5ZxMFs3jQiEQ6LLvUsBLdkA80" //The base64 encoded string output from Java;
var iv = "dc0da04af8fee58593442bf834b30739"
var salt = "dc0da04af8fee58593442bf834b30739"
var aesUtil = new AesUtil(keySize, iterationCount);
var plaintext = aesUtil.decrypt(salt, iv, encryptionKey, dataToDecrypt);
console.log(plaintext);
**//AESUtil - Utility class for CryptoJS**
var AesUtil = function(keySize, iterationCount) {
this.keySize = keySize / 32;
this.iterationCount = iterationCount;
};
AesUtil.prototype.generateKey = function(salt, passPhrase) {
var key = CryptoJS.PBKDF2(passPhrase, CryptoJS.enc.Hex.parse(salt),
{ keySize: this.keySize, iterations: this.iterationCount });
return key;
}
AesUtil.prototype.decrypt = function(salt, iv, passPhrase, cipherText) {
var key = this.generateKey(salt, passPhrase);
var cipherParams = CryptoJS.lib.CipherParams.create({
ciphertext: CryptoJS.enc.Base64.parse(cipherText)
});
var decrypted = CryptoJS.AES.decrypt(cipherParams,key,
{ iv: CryptoJS.enc.Hex.parse(iv) });
return decrypted.toString(CryptoJS.enc.Utf8);
}
}