简体   繁体   English

使用crypto-js在JS上复制Java解密

[英]Replicate Java decryption on JS using crypto-js

I have a java code that decrypts the data when provided with data and key.我有一个 java 代码,它在提供数据和密钥时解密数据。 The java class and function is as follow, java class 和 function 如下,

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Base64;
 
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.security.Key;
 
public class AES {
 
    private static SecretKeySpec secretKey;
    private static byte[] key;
 
    public static String decrptyBySyymetricKey(String encryptedSek, byte[] appKey) {
        Key aesKey = new SecretKeySpec(appKey, "AES"); // converts bytes(32 byte random generated) to key
        
        try {
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); // encryption type = AES with padding PKCS5
            cipher.init(Cipher.DECRYPT_MODE, aesKey); // initiate decryption type with the key
        
            byte[] encryptedSekBytes = Base64.getDecoder().decode(encryptedSek); // decode the base64 encryptedSek to bytes
        
            byte[] decryptedSekBytes = cipher.doFinal(encryptedSekBytes); // decrypt the encryptedSek with the initialized cipher containing the key(Results in bytes)
        
            String decryptedSek = Base64.getEncoder().encodeToString(decryptedSekBytes); // convert the decryptedSek(bytes) to Base64 StriNG
            return decryptedSek; // return results in base64 string
        }catch(Exception e) {
            return "Exception; "+e;
        }
    }
    
    public static void main(String[] args){
    final String secretKey = "r16glPt7vyO6g22KH4JcpzUIdnUXIy5p";
     
    String custom = AES.decrptyBySyymetricKey("ul1tu6I0tLcfOYAW3Yug0HNP9sKo7O2AUuMLQjs62TOE5g0v9VTzB21EKdSAvlSM", "r16glPt7vyO6g22KH4JcpzUIdnUXIy5p".getBytes());
  
    }
}

Now I need to replicate the above using vanilla JS and Crypto-js library.现在我需要使用 vanilla JS 和 Crypto-js 库来复制上述内容。 However I am unable to do so.但是我无法这样做。 I am not able to figure out where I am going wrong.我无法弄清楚我哪里出错了。

const encryptedsek = 'ul1tu6I0tLcfOYAW3Yug0HNP9sKo7O2AUuMLQjs62TOE5g0v9VTzB21EKdSAvlSM';
const password = 'r16glPt7vyO6g22KH4JcpzUIdnUXIy5p';
        
var parsedBase64Key  = CryptoJS.enc.Base64.parse(password);

var d = CryptoJS.AES.decrypt(encryptedsek, parsedBase64Key, {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7
}).toString( CryptoJS.enc.Utf8 );

I am thinking that the way I am using the password is causing the issue.我在想我使用密码的方式导致了这个问题。 It does not produces any output.它不会产生任何 output。 Any pointers will be helpful.任何指针都会有所帮助。 Also I tried different decoders from crypto-js on password but it does not works.我还尝试了密码上的crypto-js不同的解码器,但它不起作用。

Thanks in advance.提前致谢。

I was able to solve it.我能够解决它。 What I was doing wrong was I was using the encrypted password to decrypt my value.我做错的是我使用加密密码来解密我的值。 I realized this when I saw the password was different than the one I set.当我看到密码与我设置的密码不同时,我意识到了这一点。 It dawn upon me that the password that I am using is encrypted one and I need to use my actual password.我突然意识到我使用的密码是加密的,我需要使用我的实际密码。

The following worked once I used my actual password instead of the encrypted one.一旦我使用了我的实际密码而不是加密的密码,以下方法就起作用了。

var encryptedInfo = "5jo90pB0Sat8ftkSwS4s5cZQj2bM55kbikGKLxw/2bvk57gBPEnolPiMy3C2wr3x";

var password =  "my_secret_non_encrypted_password";
password = CryptoJS.enc.Utf8.parse(password)

var decrypt = CryptoJS.AES.decrypt(encryptedInfo.toString(), password, {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7
}).toString(CryptoJS.enc.Base64);

console.log('decrypt ', decrypt);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM