I am trying to encrypt something in JavaScript using the google's https://code.google.com/p/crypto-js/#AES exactly as it is if the example. The problem is that where I am trying to decrypt it in Java the result is way different. The only difference that I can see is the padding (CryptoJs uses Pkcs7 whereas java uses Pkcs5) after reading a bit I realized that Pkcs7 and Pkcs5 are basically the same.
Here is a sample of the code that i do the encryption in Javascript:
var crypto = require('./aes.js');
var login = 'ABCD';
var key = crypto.CryptoJS.enc.Hex.parse('0123456789012345');
var ive = crypto.CryptoJS.enc.Hex.parse('0123456789012345');
var encrypted = crypto.CryptoJS.AES.encrypt(login, key, {iv: ive});
console.log('encrypted msg = ' + encrypted)
This is the code that I am using in Java to decrypt it:
public String decrypt(byte[] cipherText) throws Exception {
String psk = "0123456789012345";
String iv = "0123456789012345";
try {
String encryptionKey = psk;
final Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding", "SunJCE");
final SecretKeySpec key = new SecretKeySpec(encryptionKey.getBytes(UTF8), "AES");
cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv.getBytes(UTF8)));
return new String(cipher.doFinal(cipherText), UTF8);
} catch (BadPaddingException | IllegalBlockSizeException | UnsupportedEncodingException | InvalidAlgorithmParameterException | InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException | NoSuchProviderException ex) {
LOG.log(Level.SEVERE, ex.getMessage(), ex);
throw new Exception(ex.getMessage());
}
}
Do you have any idea why it fails that bad? Is it a different algorithm or do I fail somewhere else?