I have an excisting webservice which encrypts and decrypts with AES, now i have to encrypt in the same way as java but in javascript. I have read all the topics about doing this in javascript but haven't found any helpfull solution. Javascript always encrypts in a different way and i can't find why.
This is the excisting java code :
public static String encrypt(String data) throws Exception {
byte[] keyValue = encryptionKey.getBytes();
Key key = new SecretKeySpec(keyValue, "AES");
Cipher c = Cipher.getInstance("AES/ECB/PKCS5Padding");
c.init(Cipher.ENCRYPT_MODE, key);
byte[] encVal = c.doFinal(data.getBytes());
String encryptedValue = new BASE64Encoder().encode(encVal);
return encryptedValue;
}
and this is the javascript code i tend to use but gives a different encryption (CryptoJS) :
var encrypted = CryptoJS.AES.encrypt(data, encryptionKey);
or either one of these (GibberishAES) :
// Defaults to 256 bit encryption
var encrypted = GibberishAES.enc(data, encryptionKey);
// change the bit encrytion
GibberishAES.size(128);
var encrypted = GibberishAES.enc(data, encryptionKey);
GibberishAES.size(192);
var encrypted = GibberishAES.enc(data, encryptionKey);
I can't change the implementation in java or the way we do security. Does someone have more experience in this who can tell me what i'm doing wrong here ?