AES encryption in JavaScript and decryption in Java


Zenz 0

I have an existing web service that encrypts and decrypts using AES, now I have to encrypt in the same way as in Java, but in javascript. I've read all the threads about doing this in javascript but haven't found any useful solution yet. Javascript is always encrypted differently and I can't find a reason why.

Here is the existing 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;
}

Here's the javascript code I tend to use, but with a different encryption (CryptoJS):

var encrypted = CryptoJS.AES.encrypt(data, encryptionKey);

or any of the following (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. Can someone with more experience in this area tell me what I'm doing wrong?

Holger

You're only looking at encryption algorithms, but also pay attention to block mode and padding, otherwise you won't create compatible results. According to code.google.com , CryptoJS defaults to CBCand , PKCS7while your Java code uses ECBand PKCS5.

You must match it. You can setup CryptoJS to use ECB. Regarding padding, since CryptoJS doesn't list PKCS5what's supported and Java doesn't PKCS7, it actually lists very few so it's trickier , so implementation may depend on what padding algorithm is supported by the AES provider, but at least NoPaddingBoth are supported, Java and CryptoJS.

Related


AES javascript encryption and Java decryption

Encoder I have implemented RSA encryption in javascrypt and RSA decryption in java, it's just a simple process. But the problem is that I have to encrypt a lot of data in one go, which is not possible with RSA, either I have to split the data to be encrypted (

AES javascript encryption and Java decryption

Encoder I have implemented RSA encryption in javascrypt and RSA decryption in java, it's just a simple process. But the problem is that I have to encrypt a lot of data in one go, which is not possible with RSA, either I have to split the data to be encrypted (

AES javascript encryption and Java decryption

Encoder I have implemented RSA encryption in javascrypt and RSA decryption in java, it's just a simple process. But the problem is that I have to encrypt a lot of data in one go, which is not possible with RSA, either I have to split the data to be encrypted (

AES encryption in JavaScript and decryption in Java

Zenz 0 I have an existing web service that encrypts and decrypts with AES, now I have to encrypt in the same way as Java, but with javascript. I've read all the threads about doing this in javascript but haven't found any useful solution yet. Javascript is alw

AES javascript encryption and Java decryption

Encoder I have implemented RSA encryption in javascrypt and RSA decryption in java, it's just a simple process. But the problem is that I have to encrypt a lot of data in one go, which is not possible with RSA, either I have to split the data to be encrypted (

AES javascript encryption and Java decryption

Encoder I have implemented RSA encryption in javascrypt and RSA decryption in java, it's just a simple process. But the problem is that I have to encrypt a lot of data in one go, which is not possible with RSA, either I have to split the data to be encrypted (

AES encryption and decryption in Java

death: I'm pretty new to encryption/decryption and have to encrypt some data files, but not entirely sure I'm going about it the right way. Right now, I have a script to encrypt all files that are not included in my repository, but the decryptor is included in

Java AES encryption and decryption

Praneeth I want to encrypt and decrypt a password using 128 bit AES encryption and a 16 byte key. There was an error javax.crypto.BadPaddingExceptiondecrypting the value . Do I lose anything when decrypting? public static void main(String args[]) { Test t

Java AES encryption and decryption

Praneeth I want to encrypt and decrypt a password using 128 bit AES encryption and a 16 byte key. There was an error javax.crypto.BadPaddingExceptiondecrypting the value . Do I lose anything when decrypting? public static void main(String args[]) { Test t

AES encryption and decryption in Java

death: I'm pretty new to encryption/decryption and have to encrypt some data files, but not entirely sure I'm going about it the right way. Right now, I have a script to encrypt all files that are not included in my repository, but the decryptor is included in

AES encryption and decryption in Java

death: I'm pretty new to encryption/decryption and have to encrypt some data files, but not entirely sure I'm going about it the right way. Right now, I have a script to encrypt all files that are not included in my repository, but the decryptor is included in

Java AES encryption and decryption

Praneeth I want to encrypt and decrypt a password using 128 bit AES encryption and a 16 byte key. There was an error javax.crypto.BadPaddingExceptiondecrypting the value . Do I lose anything when decrypting? public static void main(String args[]) { Test t

AES encryption and decryption in Java

death: I'm pretty new to encryption/decryption and have to encrypt some data files, but not entirely sure I'm going about it the right way. Right now, I have a script to encrypt all files that are not included in my repository, but the decryptor is included in

AES encryption and decryption in Java

death: I'm pretty new to encryption/decryption and have to encrypt some data files, but not entirely sure I'm going about it the right way. Right now, I have a script to encrypt all files that are not included in my repository, but the decryptor is included in

AES encryption and decryption using Java

Yogesh D Here's what I'm doing, it might seem a little clunky, but it might help you out. I get one BadPaddingException. Read almost all related topics but couldn't find a proper solution. I am new to encryption decryption programming and need to implement it

AES encryption and decryption using Java

Yogesh D Here's what I'm doing, it might seem a little clunky, but it might help you out. I get one BadPaddingException. Read almost all related topics but couldn't find a proper solution. I am new to encryption decryption programming and need to implement it