AES encryption in Java and decryption in JavaScript using CryptoJS


Captain Winters:

My code below encrypts some file content in Java by using AES/CTR/NOPADDING mode. I am using javax's crypto package. I am also using the same key to generate the key and iv.

Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding");

        byte[] secretKey = Base64.decodeBase64("uQsaW+WMUrjcsq1HMf+2JQ==");

        SecretKeySpec key = new SecretKeySpec(secretKey, "AES");
        IvParameterSpec iv = new IvParameterSpec(secretKey);

        cipher.init(mode, key , iv);

        FileInputStream fileInputStream = new FileInputStream(sourceFilePath);
        FileOutputStream fileOutputStream = new FileOutputStream(destFilePath);

        int read = 0;
        while ((fileInputStream.available()) > 0) {
            byte[] block = new byte[4096];
            read = fileInputStream.read(block);
            byte[] writeBuffer = cipher.update(block);
            fileOutputStream.write(writeBuffer, 0, read);
        }

        byte[] writeBuffer = cipher.doFinal();
        fileOutputStream.write(writeBuffer, 0, writeBuffer.length);

        fileInputStream.close();
        fileOutputStream.close();

I can't decrypt encrypted content in javascript using cryptojs. Here's what I've tried.

var key = CryptoJS.enc.Hex.parse(atob('uQsaW+WMUrjcsq1HMf+2JQ=='));

var decrypted = CryptoJS.AES.decrypt(encryptedContent, key, {
    mode: CryptoJS.mode.CTR,
    iv: key,
    padding: CryptoJS.pad.NoPadding
});

var decryptedText = CryptoJS.enc.Utf8.stringify(decrypted);

Can someone tell me what I'm doing wrong? or tell me what to do.

I am able to encrypt and decrypt independently using Java and javascript.

topaz:
  • In the CryptoJS-documentation , it is explained which data types and parameters are CryptoJS.decrypt()expected by -method and which encoders are available:

    • The key must be passed as the to CryptoJS.decrypt()-method WordArray. Since the key data is Base64 encoded, you can WordArrayuse CryptoJS.enc.Base64.parse()the -method to convert it to .
    • Ciphertext can be passed to -method as internal to CryptoJS.decrypt()-object . The Java code stores the encrypted data in a file. Assuming that the string contains those data as hex strings (unfortunately this is not from the posted code, so that has to be assumed here), you can use the -method to convert them to -, and Wrapped in -object.WordArrayCipherParamsencryptedContentWordArrayCryptoJS.enc.Hex.parse()CipherParams
    • CryptoJS.decrypt()The -method returns a string that WordArraycan be converted with -method.CryptoJS.enc.Utf8.stringify()
  • If the input file contains the following plain text:

    This is the plain text which needs to be encrypted!
    

    The Java code stores the following sequence of bytes (= encrypted data) in the output file:

    52F415AB673427C42278E8D6F34C16134D7E3FE7986500980ED4063F3CF51162592CE0F5412CCA0BC2DBAE3F2AEC2D585EE8D7
    

    The JavaScript code for decryption is:

    var key = CryptoJS.enc.Base64.parse('uQsaW+WMUrjcsq1HMf+2JQ==');
    
    var encryptedContent = '52F415AB673427C42278E8D6F34C16134D7E3FE7986500980ED4063F3CF51162592CE0F5412CCA0BC2DBAE3F2AEC2D585EE8D7';
    var cipherParams = CryptoJS.lib.CipherParams.create({
        ciphertext: CryptoJS.enc.Hex.parse(encryptedContent)         
    });
    
    var decrypted = CryptoJS.AES.decrypt(cipherParams, key, {
        mode: CryptoJS.mode.CTR,
        iv: key,
        padding: CryptoJS.pad.NoPadding
    });
    
    var decryptedText = CryptoJS.enc.Utf8.stringify(decrypted);
    console.log(decryptedText); 
    

Displays raw plain text in the console. To run code above at least CryptoJS-version 3.1.4 (see version , cdnjs ).

Related


AES encryption/decryption in javascript using CryptoJS

Anthracene I am trying to send AES encrypted messages between javascript and php using a shared key. In Javascript, I am using the CryptoJS library. In php, I am using mycrypt. I am trying to construct an encrypted message in javascript and then decrypt it in

AES encryption/decryption in javascript using CryptoJS

Anthracene I am trying to send AES encrypted messages between javascript and php using a shared secret. In Javascript, I am using the CryptoJS library. In php, I am using mycrypt. I am trying to construct an encrypted message in javascript and then decrypt it

AES encryption/decryption in javascript using CryptoJS

Anthracene I am trying to send AES encrypted messages between javascript and php using a shared secret. In Javascript, I am using the CryptoJS library. In php, I am using mycrypt. I am trying to construct an encrypted message in javascript and then decrypt it

AES encryption/decryption in javascript using CryptoJS

Anthracene I am trying to send AES encrypted messages between javascript and php using a shared secret. In Javascript, I am using the CryptoJS library. In php, I am using mycrypt. I am trying to construct an encrypted message in javascript and then decrypt it

AES CBC: JavaScript/CryptoJS encryption -> Golang decryption

DjH Note: This is for personal use and learning only, I don't plan to use my encryption for public use. I need to AES256 encrypt the string, but my current attempt ends up with something like the server side string Salted__Vέ��|��l��ʼ8XCQlYwhen hex decoded . W

AES CBC: JavaScript/CryptoJS encryption -> Golang decryption

DjH Note: This is for personal use and learning only, I don't plan to use my encryption for public use. I need to AES256 encrypt the string, but my current attempt ends up with something like the server side string Salted__Vέ��|��l��ʼ8XCQlYwhen hex decoded . W

AES CBC: JavaScript/CryptoJS encryption -> Golang decryption

DjH Note: This is for personal use and learning only, I don't plan to use my encryption for public use. I need to AES256 encrypt the string, but Salted__Vέ��|��l��ʼ8XCQlYwhen hex decodes it , my current attempt ends up with something like the string on the ser

AES encryption with CryptoJS and decryption with CodeIgniter

fire brand I'm trying to encrypt a username (written in Codeigniter 3) sent to my server via a POST request, so I'm using CryptoJS to do it client-side like this: var user = $('.user').val(); var key = "<? echo($key);?>"; //$key is created on the server side v

AES encryption with CryptoJS and decryption with CodeIgniter

fire brand I'm trying to encrypt a username (written in Codeigniter 3) sent to my server via a POST request, so I'm using CryptoJS to do it client-side like this: var user = $('.user').val(); var key = "<? echo($key);?>"; //$key is created on the server side v

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

AES encryption using CryptoJS

Sergio David Romero I need to implement AES encryption using JavaScript. Use AES/CBC/NoPadding mode, and created a way to do it in 16-length blocks. I have solved it using Java. look like: public static String encrypt(byte[] key, byte[] initVector, String valu

AES encryption using CryptoJS

Sergio David Romero I need to implement AES encryption using JavaScript. Use AES/CBC/NoPadding mode, and created a way to do it in 16-length blocks. I have solved it using Java. looks like: public static String encrypt(byte[] key, byte[] initVector, String val

AES encryption using Java and decryption using Java

rkj : I'm making an application that requires Java-based AES encryption and JavaScript-based decryption. I am using the following code as base form for encryption. public class AESencrp { private static final String ALGO = "AES"; private static final byte

AES encryption using Java and decryption using Java

j I'm making an application that requires Java-based AES encryption and JavaScript-based decryption. I am using the following code as base form for encryption. public class AESencrp { private static final String ALGO = "AES"; private static final byte[] k

AES encryption using Java and decryption using Java

rkj : I'm making an application that requires Java-based AES encryption and JavaScript-based decryption. I am using the following code as base form for encryption. public class AESencrp { private static final String ALGO = "AES"; private static final byte

AES-CTR encryption in Go and decryption in CryptoJS

Mark: I'm having trouble decrypting text encrypted in Go lang using CryptoJS. Here is the Go code : https://play.golang.org/p/xCbl48T_iN package main import ( "crypto/aes" "crypto/cipher" "encoding/base64" "fmt" ) func main() { key := []b

AES-CTR encryption in Go and decryption in CryptoJS

Mark: I'm having trouble decrypting text encrypted in Go lang using CryptoJS. Here is the Go code : https://play.golang.org/p/xCbl48T_iN package main import ( "crypto/aes" "crypto/cipher" "encoding/base64" "fmt" ) func main() { key := []b

AES-CTR encryption in Go and decryption in CryptoJS

Mark: I'm having trouble decrypting text encrypted in Go lang using CryptoJS. Here is the Go code : https://play.golang.org/p/xCbl48T_iN package main import ( "crypto/aes" "crypto/cipher" "encoding/base64" "fmt" ) func main() { key := []b