gpt4 book ai didi

java - Android 不支持的 key 大小 : 3 bytes on decrypt text

转载 作者:太空宇宙 更新时间:2023-11-04 10:51:43 24 4
gpt4 key购买 nike

我有简单的 json 对象:

json:

{
"bookInfo": {
"type": "book",
"onvan": "کتاب دو",
"tadvin": "دفتر",
"nasher": "بوستان",
"nobat": "اول",
"shabek": "1100",
"gheymat": "2000 تومان",
"tasvirData": ""
}
}

在使用 123 key 对 c# 进行加密之后,我得到以下结果:

hlAkzlLeVE3JdYrk61dy8901K2tXWqx5qxYd1t8zVSZzx12lD6nxqZRlPRI8yX8PECxaHJ5zdueY0/A0J7Lxxlv2DHvdI/H+Qu2bsQI6X/Qc6ISwlY7Q6c0IiwWtKuFm5f8BC9wNSSqPXkBM7J+hwEtHUBAoh+IMzxNXvnA/hZIp3R2FznX4cdJhs4Lnm003WLGiKwJ1fEgzUl55WKBIh2dMwQwqpTlNmLFIo6ovlJYMt4DTaoeET+VAhHcGtX1u10910EZ1hCqb1pcspE1SPQ==

现在,当我尝试使用 123 key 解密时,我收到此错误:

Unsupported key size: 3 bytes

EncryptUtils 类:

public class EncryptUtils {
public static SecretKey generateKey(String mySecret) throws NoSuchAlgorithmException, InvalidKeySpecException {
return new SecretKeySpec(mySecret.getBytes(), "AES");
}

public static byte[] encryptMsg(String message, SecretKey secret)
throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidParameterSpecException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
Cipher cipher = null;
cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secret);
byte[] cipherText = cipher.doFinal(message.getBytes("UTF-8"));
return cipherText;
}

public static String decryptMsg(byte[] cipherText, SecretKey secret)
throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidParameterSpecException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, UnsupportedEncodingException {
Cipher cipher = null;
cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, secret);
String decryptString = new String(cipher.doFinal(cipherText), "UTF-8");
return decryptString;
}
}

这是我的解密代码:

String mySecret="123";
SecretKey secretKey = EncryptUtils.generateKey(mySecret);
JSONObject bookObject = new JSONObject(EncryptUtils.decryptMsg(bookContent.getBytes("UTF-8"), secretKey));

最佳答案

问题是您传入 hexToBytes 函数的字符串是 Base64 编码的,它不是十六进制字符串,因此将前两个字符读取为整数会导致异常。

将解密中的行更改为:

String decryptString = new String(cipher.doFinal(cipherText), "UTF-8");

致:

String decryptString= cipher.doFinal(Base64.decode(cipherText,Base64.DEFAULT)); 

编码愉快!!

关于java - Android 不支持的 key 大小 : 3 bytes on decrypt text,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47766652/

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