Search code examples
javascriptjavaencryptionaescryptojs

How can I decrypt the cipher encrypt from Java using CryptoJS like this


First, I’m so sorry that my English is not good that may make u confusing what am I say.But I really hope that someone can help me……

Here is my Java code to encrypt the cipher:

public static String decryptByAES_CBC(String data, String key) {
    try {
        byte[] decryptFrom = convertHexStringToBytes(data);

        Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
        SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "AES");

        String iv = new StringBuffer(key).reverse().toString();
        IvParameterSpec ivSpec = new IvParameterSpec(iv.getBytes());

        cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);

        byte[] original = cipher.doFinal(decryptFrom);
        return new String(original);
    } catch (Exception e) {
        LogUtil.exception(e);
        return null;
    }
}

Now begin to encrypt in Java:

EncryptionUtil.encryptByAES_CBC("whywhywhywhywhywhy", "9999999999999999”);

Then got the result:

030f7c7d57e82c6fb7d3066363e59c9dd6de7daa486146857552d31403a7cc71

And I can get the same encrypt result in CryptoJS,here is my CryptoJS code:

encodeByAES_CBC(data, key) {
    return CryptoJS.AES.encrypt(data, CryptoJS.enc.Utf8.parse(key), {
            iv : CryptoJS.enc.Utf8.parse(key),
            mode : CryptoJS.mode.CBC,
            padding : CryptoJS.pad.ZeroPadding
        }).ciphertext.toString();
},
decodeByAES_CBC(data, key) {
    return CryptoJS.AES.decrypt(data, CryptoJS.enc.Utf8.parse(key), {
            iv : CryptoJS.enc.Utf8.parse(key),
            mode : CryptoJS.mode.CBC,
            padding : CryptoJS.pad.ZeroPadding
        }).toString(CryptoJS.enc.Utf8);
}

Here is the question which confusing me: For example:

var encrypted = CryptoJS.AES.encrypt("Message", "Secret Passphrase");
var decrypted = CryptoJS.AES.decrypt(encrypted, "Secret Passphrase");
console.log(decrypted.toString(CryptoJS.enc.Utf8)); \\ The output is "Message"

The output is right because the param encrypted is a CipherParams type.

But I don’t know how can I get the right decrypted answer when I only have the ciphertext just like 030f7c7d57e82c6fb7d3066363e59c9dd6de7daa486146857552d31403a7cc71 which I get from server and the key 9999999999999999 .

Em……That’s all,and hope what I say is clear enough……


Solution

  • I have found the way to do.

    var encrypted = CryptoJS.AES.encrypt("Message", "Secret Passphrase”);
    
    CryptoJS.AES.decrypt(CryptoJS.lib.CipherParams.create({
                ciphertext: encrypted.ciphertext,
                salt: encrypted.salt
            }).toString(CryptoJS.format.OpenSSL),key).toString(CryptoJS.enc.Utf8)); \\ The output also is "Message"
    

    And in my Java code, because I use the Noppading(Caution: Noppading in Java = Zeropadding in CryptoJS!!!This trouble me lots of time…),so it is not need to add salt in CryptoJS.lib.CipherParams.create().

    So if I can only get the cipher and the key from Java encrypt.I can decrypt in

    CryptoJS.AES.decrypt(CryptoJS.lib.CipherParams.create({
            ciphertext: CryptoJS.enc.Hex.parse(data)
          }).toString(CryptoJS.format.OpenSSL), CryptoJS.enc.Utf8.parse(key), {
            iv : CryptoJS.enc.Utf8.parse(key),
            mode : CryptoJS.mode.CBC,
            padding : CryptoJS.pad.ZeroPadding
        }).toString(CryptoJS.enc.Utf8);
    

    Still hope what I am say is clear enough to this question.And I hope it can help anyone else...