gpt4 book ai didi

解密信用卡号时返回 javax.crypto.IllegalBlockSizeException

转载 作者:行者123 更新时间:2023-11-30 03:30:26 26 4
gpt4 key购买 nike

我的加密方法是:

private static final String ALGORITHM = "AES/ECB/PKCS5Padding";
private static final byte[] KEY = "StBet9834#$10BCy".getBytes();

private String encryptCreditCard(String ccNumber) {
// do some encryption
if (ccNumber == null || ccNumber.length() == 0) {
return "";
}
Key key = new SecretKeySpec(KEY, "AES");
try {
Cipher c = Cipher.getInstance(ALGORITHM);
c.init(Cipher.ENCRYPT_MODE, key);
//return Base64.encodeBytes(c.doFinal(ccNumber.getBytes()));
byte[] ccNumberBytes = ccNumber.getBytes();
byte[] encCCNumber = c.doFinal(ccNumberBytes);
return new String(Base64.encodeBase64(encCCNumber));
} catch (Exception e) {
throw new RuntimeException(e);
}
}

我使用以下方法来解密我的信用卡详细信息:

public void decryptCreditCard() {
Key key = new SecretKeySpec(KEY, "AES");
try {
String ccNumber = this.cardNumber;
if (ccNumber == null || ccNumber.length() == 0) {
return;
}
Cipher c = Cipher.getInstance(ALGORITHM);
c.init(Cipher.DECRYPT_MODE, key);
byte[] ccENCBytes = Base64.decodeBase64(ccNumber);
byte[] ccDECBytes = c.doFinal(ccENCBytes);
this.plainCardNumber = new String(ccDECBytes);
this.last4CreditCard = plainCardNumber.substring(this.plainCardNumber.length() - 4);
} catch (Exception e) {
throw new RuntimeException(e);
}
}

我的测试卡号字符串是:5123456789012346。执行此行时:

byte[] ccDECBytes = c.doFinal(ccENCBytes);

我收到以下异常:

Caused by: javax.crypto.IllegalBlockSizeException: Input length must be multiple of 16 when decrypting with padded cipher
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:750)
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:676)
at com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:313)
at javax.crypto.Cipher.doFinal(Cipher.java:2087)
at stbet.model.core.CustomerAccount.decryptCreditCard(CustomerAccount.java:527)

这个错误说明什么?非常感谢任何帮助。

最佳答案

您的算法必须表示带填充的密码:例如:AES/CBC/PKCS5Padding。

并且编码消息的字符串表示不能丢失字节。您正在使用字符串的默认编码,因此很可能 UTF-8 不适合。您需要字节到字节的映射,例如:“ISO-8859-1”

返回新字符串(Base64.encodeBase64(encCCNumber),"ISO-8859-1");

或者简单地 Base64.encodeBase64String 应该可以完成这项工作;

关于解密信用卡号时返回 javax.crypto.IllegalBlockSizeException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29170447/

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