gpt4 book ai didi

java - 从 Angular 8 加密时,解密文本(AES 128 位)开头的垃圾值

转载 作者:行者123 更新时间:2023-12-01 19:44:49 25 4
gpt4 key购买 nike

我的后端 AES-128 解密逻辑是正确的,因为当我在 java 中进行 AES-128 加密时,它会正确解密。现在我尝试复制 Angular 8 中的加密逻辑,然后解密的文本现在在初始字节处包含垃圾值,并且剩余的一半字符串是正确的(类似于这样的 ��da%:����mL���͔1", “add”:“uk”,“com”:“wow”})。

下面是我的加密 Angular 代码

encryptData(value: any) {
    var iv = CryptoJS.enc.Base64.parse(this.generateIV());
    var key = CryptoJS.enc.Hex.parse('0123456789abcdef0123456789abcdef');
    var aesEncrypted = CryptoJS.AES.encrypt(CryptoJS.enc.Utf8.parse(value.toString()), key,{
        keySize: 128 / 8,
        iv: iv,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7
      });

    console.log(JSON.stringify({
payloadToken: this.getRSAEncryption("0123456789abcdef0123456789abcdef"), 
payloadVariable: aesEncrypted.iv.toString() , 
payloadText: aesEncrypted.toString()}
));
    return aesEncrypted.toString();

  }

generateIV() {
    var key = "";
    var hex = "0123456789abcdef";

    for (var i = 0; i < 15; i++) {
      key += hex.charAt(Math.floor(Math.random() * 15));
    }
    return key;
  }

下面是我在java中的解密逻辑

public static String packetDecryption(String requestPacket){
DecryptedTokenPacket decryptedTokenPacket = GSON.fromJson(requestPacket,
DecryptedTokenPacket.class);
SecretKey key2 = new SecretKeySpec(decryptedTokenPacket.getDecData(), 0,
decryptedTokenPacket.getDecData().length, EncConstants.ENC_ALGO);
// Instantiate the cipher
Cipher cipher = Cipher.getInstance(EncConstants.CBC_ALGO);
byte[] ivBytes = new Base64().decode(decryptedTokenPacket.getPayloadVariable());
cipher.init(Cipher.DECRYPT_MODE, key2, new IvParameterSpec(ivBytes));
byte[] encryptedTextBytes = new Base64().decode(decryptedTokenPacket.getPayloadText());
byte[] decryptedTextBytes;
decryptedTextBytes = cipher.doFinal(encryptedTextBytes);
return new String(decryptedTextBytes);
}

//this function is used in the gateway for decrypting the secret key and then pass through
public DecryptedTokenPacket decryptKeyUsingRSA(EncryptedPacket encryptedPacket){
Cipher cipher = Cipher.getInstance(RSA_ALGO);
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] decData = cipher.doFinal(new Base64().decode(encryptedPacket.getPayloadToken()));
decryptedTokenPacket.setPayloadText(encryptedPacket.getPayloadText());
decryptedTokenPacket.setPayloadVariable(encryptedPacket.getPayloadVariable());
decryptedTokenPacket.setDecData(decData);
return decryptedTokenPacket;
}

bean 类

public class DecryptedTokenPacket {
private String payloadVariable;
private String payloadText;
private byte[] decData;
}

public class EncryptedPacket {
private String payloadVariable;
private String payloadText;
private String payloadToken;
}

一开始不确定是什么导致了这个垃圾值。有人可以帮我解决这个问题吗?

最佳答案

const iv = CryptoJS.lib.WordArray.random(128/8);

关于java - 从 Angular 8 加密时,解密文本(AES 128 位)开头的垃圾值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59134922/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com