无法解密 angular 中已在 java 中加密的字符串

Unable to Decrypt String in angular which is Encrypted in java

我尝试在 java 中加密一些消息,但是当我尝试将其解密为 angular 时,它没有给出任何正确的输出。

在 angular 中需要进行哪些更改才能解密消息?

这是我的 Java 代码:

public class AesBase64Wrapper{
    
    private static char[] password = "EF737CC29DAE7C80644A5B01544CBA61".toCharArray();
    private static byte iv[];
    static {
        try {             
            iv = getBytes("79994A6EF73DA76C");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public static String encryptAndEncode(String raw,String salt)
    {
        try
        {
            Cipher c = getCipher(1,salt);
            byte[] encryptedVal = c.doFinal(getBytes(raw));
            return Base64.getEncoder().encodeToString(encryptedVal);
        }
        catch (Throwable t)
        {
            throw new RuntimeException(t);
        }
    }
  
    public static String decodeAndDecrypt(String encrypted,String salt) throws Exception
    {
        byte[] decodedValue = Base64.getDecoder().decode(encrypted);
        Cipher c = getCipher(2,salt);
        byte[] decValue = c.doFinal(decodedValue);
        return new String(decValue);
    }
  
    private static byte[] getBytes(String str) throws UnsupportedEncodingException
    {
        return str.getBytes("UTF-8");
    }
    
    private static Cipher getCipher(int mode,String salt)throws Exception
    {

        Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
        c.init(mode, generateKey(salt), new IvParameterSpec(iv));
        return c;
    }
  
  
    private static Key generateKey(String salt) throws Exception
    {
        SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");

        byte[] saltb = getBytes(salt);
    
        KeySpec spec = new PBEKeySpec(password, saltb, 65536, 256);
        SecretKey tmp = factory.generateSecret(spec);
        return new SecretKeySpec(tmp.getEncoded(), "AES");
    }
  
    
    
}

这个是angular:

export class PrepaidCryptoService {

  Key_IV = "79994A6EF73DA76C";
  PASSWORD = "EF737CC29DAE7C80644A5B01544CBA61";
  SALT = "12345";
  finalEncryption;
  constructor() {
  }

  encoder(str) {
    let encoder = new TextEncoder();
    let byteArray = encoder.encode(str)
    return CryptoJS.enc.Utf8.parse(str)
  }

  toWordArray(str) {
    return CryptoJS.enc.Utf8.parse(str);
  }

  encrypt(value) {

      var key = CryptoJS.PBKDF2(this.PASSWORD, this.SALT, {
        keySize : 256, 
        iterations: 65536
      })
      var iv = CryptoJS.enc.Hex.parse(this.Key_IV);
      var encrypted = CryptoJS.AES.encrypt(this.encoder(value), key, {
        iv: this.toWordArray(iv),
        padding: CryptoJS.pad.Pkcs7,
        mode: CryptoJS.mode.CBC
      })

      return encrypted.ciphertext.toString(CryptoJS.enc.Base64);
    }


  decrypt(value) {
 
    var key = CryptoJS.PBKDF2(this.PASSWORD, this.SALT, {
       keySize : 256,
      iterations: 65536
    })
     var iv =  CryptoJS.enc.Utf8.parse(this.Key_IV)
    var decrypted = CryptoJS.AES.decrypt(value, key, {
      iv: this.toWordArray(iv),
      padding: CryptoJS.pad.Pkcs7,
      mode: CryptoJS.mode.CBC
    })

    return decrypted.toString(CryptoJS.enc.Utf8);

  }

}

我尝试了几种不同的方法,但还是出现了这个问题。

I Just Update below methods in my angular code and it works for me :

encrypt(value) {
    var key = CryptoJS.PBKDF2(this.PASSWORD, this.SALT, {
      keySize: 256 / 32,
      iterations: 65536
    })
    var encrypted = CryptoJS.AES.encrypt(this.encoder(value), key, {
      iv: this.toWordArray(this.Key_IV),
      padding: CryptoJS.pad.Pkcs7,
      mode: CryptoJS.mode.CBC
    })
    return encrypted.ciphertext.toString(CryptoJS.enc.Base64);
  }

  decrypt(value) {
    var key = CryptoJS.PBKDF2(this.PASSWORD, this.SALT, {
      keySize: 256 / 32,
      iterations: 65536
    })
    var decrypted = CryptoJS.AES.decrypt(value, key, {
      iv: this.toWordArray(this.Key_IV),
      padding: CryptoJS.pad.Pkcs7,
      mode: CryptoJS.mode.CBC
    })
    return decrypted.toString(CryptoJS.enc.Utf8);
  }
}