This is how you can encrypt/decrypt in JS and Java using Crypto and javax.crypto respectively.
If you don't care about interoperability of different environment, you can get started pretty quickly using createCypher(algorithm, password) in your JS code, but it is not very portable as you don't know how raw key and initialization vector is derived from the password.
Changing the JS code to use createCipheriv(algorithm, key, iv) instead, will give you a portable encryption/decryption:
encryptString : function encryptString(str, encryptionKey, iv) {
var cipher = crypto.createCipheriv('aes-128-cbc', encryptionKey, iv);
var cipherText = cipher.update(str, 'binary', 'base64');
var cipherTextRemaining = cipher.final('base64');
return cipherText + cipherTextRemaining;
},
decryptString : function decryptString(str, encryptionKey, iv) {
var desipher = crypto.createDecipheriv('aes-128-cbc', encryptionKey, iv);
var desipherText = desipher.update(str, 'base64', 'binary');
var desipherTextRemaining = desipher.final('binary');
return desipherText + desipherTextRemaining;
},
This is the equivalent Java code that does the same thing:
public static String encryptString(String clearText, byte[] key, byte[] initialVector) throws Exception
{
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
SecretKeySpec secretKeySpecy = new SecretKeySpec(key, "AES");
IvParameterSpec ivParameterSpec = new IvParameterSpec(initialVector);
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpecy, ivParameterSpec);
byte[] encrypted = cipher.doFinal(clearText.getBytes());
return new String(Base64.encodeBase64(encrypted, false));
}
public static String decryptString(String cipherText, byte[] key, byte[] initialVector) throws Exception
{
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
SecretKeySpec secretKeySpecy = new SecretKeySpec(key, "AES");
IvParameterSpec ivParameterSpec = new IvParameterSpec(initialVector);
cipher.init(Cipher.DECRYPT_MODE, secretKeySpecy, ivParameterSpec);
return new String(cipher.doFinal(Base64.decodeBase64(cipherText)));
}
crypto.createCipherandcrypto.createDecipher? What library are you using? I thought you were using CryptoJS, but it doesn't seem to have those functions. Are you using Forge?