我有简单的json
对象:
JSON:
{
"bookInfo": {
"type": "book",
"onvan": "کتاب دو",
"tadvin": "دفتر",
"nasher": "بوستان",
"nobat": "اول",
"shabek": "1100",
"gheymat": "2000 تومان",
"tasvirData": ""
}
}
<{1>} encryption
c#
之后123
hlAkzlLeVE3JdYrk61dy8901K2tXWqx5qxYd1t8zVSZzx12lD6nxqZRlPRI8yX8PECxaHJ5zdueY0/A0J7Lxxlv2DHvdI/H+Qu2bsQI6X/Qc6ISwlY7Q6c0IiwWtKuFm5f8BC9wNSSqPXkBM7J+hwEtHUBAoh+IMzxNXvnA/hZIp3R2FznX4cdJhs4Lnm003WLGiKwJ1fEgzUl55WKBIh2dMwQwqpTlNmLFIo6ovlJYMt4DTaoeET+VAhHcGtX1u10910EZ1hCqb1pcspE1SPQ==
我有这个结果:
decrypt
现在当我尝试使用123
密钥Unsupported key size: 3 bytes
时,我得到错误:
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;
}
}
EncryptUtils类:
String mySecret="123";
SecretKey secretKey = EncryptUtils.generateKey(mySecret);
JSONObject bookObject = new JSONObject(EncryptUtils.decryptMsg(bookContent.getBytes("UTF-8"), secretKey));
这是我的解密代码:
function change() {
var x = document.getElementById("psw").value = "";
var x = document.getElementById("psw").placeholder = "";
}
答案 0 :(得分:0)
问题是您传递给hexToBytes函数的字符串是Base64编码的,它不是十六进制字符串,因此将前两个字符作为整数读取会导致异常。
将解密中的行更改为:
String decryptString = new String(cipher.doFinal(cipherText), "UTF-8");
要:
String decryptString= cipher.doFinal(Base64.decode(cipherText,Base64.DEFAULT));
快乐的编码!!