brix / crypto-js

JavaScript library of crypto standards.
Other
15.83k stars 2.39k forks source link

Encrypting in Java and Decrypting in JS with CryptoJS #409

Open alish1129 opened 2 years ago

alish1129 commented 2 years ago

I am trying to decode cipher code generated from Java using CryptoJS. The code used to encrypt is:

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.security.Key;
public class Test {
   private static final String SECRET_KEY = "SECRETKEY";
   private static final byte[] KEY_VALUE = SECRET_KEY.getBytes();
   public static synchronized String encrypt(String text) {
     Key key;
     try {
        key = generateKey();
        final Cipher c = Cipher.getInstance("AES");
        c.init(Cipher.ENCRYPT_MODE, key);
        final byte[] encryptedValue = c.doFinal(text.trim().getBytes());
        return convertToBase64(encryptedValue);
    } catch (final Exception e) {
        System.out.println("Error while executing [encrypt] with arguments [text] values [{}]" + text + " " + e);
    }
   }
   private static String convertToBase64(final byte[] data) {
        byte[] base64Encoded = null;
        base64Encoded = Base64.encodeBase64URLSafe(data);
        return new String(base64Encoded);
   }
   private static synchronized Key generateKey() throws Exception {
       final Key key = new SecretKeySpec(KEY_VALUE, "AES");
       return key;
   }
}

This decrypt method in Java works fine.

     public String decrypt(String code) {
        Key key;
        try {
            key = generateKey();
            final Cipher c = Cipher.getInstance("AES");
            c.init(Cipher.DECRYPT_MODE, key);
            final byte[] decodedValue = c.doFinal(reverseFromBase64(code.trim()));
            return new String(decodedValue);
        } catch (final Exception e) {
            System.out.println("Error while executing [decrypt] with  arguments [encryptedData] values [{}] " + code + " " +
                    e);
        }
    }
private static byte[] reverseFromBase64(final String data) {
        final byte[] reservedBase64 = Base64.decodeBase64(data);

        return reservedBase64;
}

However, when I try to decrypt it from Javascript using CryptoJS. I am getting an Error: Malformed UTF-8 data.

This is my JS decrypt function:

const cryptoJS = require('crypto-js')

function decryptCode (code) { 
    try {
        const parsedBase64Key = cryptoJS.enc.Base64.parse(secretKey);
        const decryptedData = cryptoJS.AES.decrypt(code, parsedBase64Key, {
            mode: cryptoJS.mode.ECB,
            padding: cryptoJS.pad.Pkcs7
        });
        const decryptedText = decryptedData.toString(cryptoJS.enc.Utf8);
        console.log(`Cipher Decrypting was Successful`);
        return decryptedText;
    } catch (error) {
        throw Error(`Error while decrypting the cipher code: ${error}`);
    }
}

decryptCode("7ksKkAFXC6B_V_8Tjb1Eqg");

What am I doing wrong?

iFederx commented 2 years ago

Why sometimes do you write crypto and other cryptoJS? Maybe should you use always cryptoJS ? where const cryptoJS = require("crypto-js"); so, your code should be:

function decryptCode (code) { 
    try {
        const parsedBase64Key = cryptoJS.enc.Base64.parse(secretKey);
        const decryptedData = cryptoJS.AES.decrypt(code, parsedBase64Key, {
            mode: cryptoJS.mode.ECB,
            padding: cryptoJS.pad.Pkcs7
        });
        const decryptedText = decryptedData.toString(cryptoJS.enc.Utf8);
        console.log(`Cipher Decrypting was Successful`);
        return decryptedText;
    } catch (error) {
        throw Error(`Error while decrypting the cipher code: ${error}`);
    }
}

Finally, is code parameter a Base64 encoded bundle?