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 (which would complicate the process) or use AES with RSA encryption and decryption. So I choose AES with RSA encryption and decryption.

Here is my JavaScript code using Crypto-js

<script src="rollups/aes.js"></script>
<script src="components/enc-base64-min.js"></script>
<script type="text/javascript" src="rollups/jquery-min.js"></script>
<script type="text/javascript">
    var secretPass = CryptoJS.lib.WordArray.random(16);
    var message = "<username>user</username><password>password</password>";
    var encrypted = CryptoJS.AES.encrypt(message, CryptoJS.enc.Hex.stringify(secretPass));
    var encode = encrypted.ciphertext.toString(CryptoJS.enc.Base64);
    var secretPasses = CryptoJS.enc.Hex.stringify(secretPass);
    console.log('encrypted: ',encrypted);
    console.log('secretPasses: ',secretPasses);
    console.log('encode: ',encode);
    $.ajax({
            url: 'encryption',
            type: 'POST',
            data: {
                encode: encode,
                secretPasses: secretPasses
            },
            success: function(data) {
                console.log('success');
            },
            failure: function(data) {
                console.log('failure');
            }
        });
    </script>

output as Jsp

encrypted: U2FsdGVkX192e9xprFPyuWu3Rxv2+CDMXiu2/TtNDwExvo4Dstx1mbqCHgds27Ng7zhYayVLjifeG15cuHI7hHfmEWvVeo7DDmOUsZmQAEM=
secretPasses: 23f96d28ae9f9c1c8c37050f79acdb37
encode: a7dHG/b4IMxeK7b9O00PATG+jgOy3HWZuoIeB2zbs2DvOFhrJUuOJ94bXly4cjuEd+YRa9V6jsMOY5SxmZAAQw==

In the post method of the servlet, I use sysout to check if the received data is the same. The secret pass I get is the same, and the encoded data is the same. The problem is that the encoded data changes its form in the jsp itself when it is converted from encrypted to encoded. I'm trying to pass "encrypted" directly via ajax, but if I type "alert(typepof encryption);" it says "object" which is pointing to the error. How to pass raw encrypted data to servlet?

System.out.println("secretpasses: "+request.getParameter("secretPasses"));
System.out.println("encode: "+request.getParameter("encode"));

Java output:

secretpasses: 23f96d28ae9f9c1c8c37050f79acdb37
encode: a7dHG/b4IMxeK7b9O00PATG+jgOy3HWZuoIeB2zbs2DvOFhrJUuOJ94bXly4cjuEd+YRa9V6jsMOY5SxmZAAQw==

It would also be welcome if I could get some examples of AES encryption in Java script and decryption in Java. I inform it is AES with RSA encryption and decryption, but it has not been inserted into the current code. If I can get the AES part to work, I can properly perform RSA by encrypting the AEs key.

Artjom B.

You can't pass it directly encryptedto the backend because it's an object containing the ciphertext and some other important data in native CryptoJS format. Without some work, there is no easy way to represent this object in Java.

However, strings can be generated from an object by calling toString()functions on the object . This will give you an OpenSSL formatted string that you can send via ajax . While you can of course parse this string in Java to get the information needed for decryption, it might be easier to pass the ciphertextand saltparameters directly to the backend .

See here how they can be used to decrypt. Note that saltand passwordderives not only the key, but also the IV.

Another possibility is to use better password derivation by leveraging CryptoJS and PBKDF2 in Java. See some examples here .

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 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 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 a

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