gpt4 book ai didi

java - 解密加密消息摘要时出现非法 block 大小异常

转载 作者:行者123 更新时间:2023-12-01 05:14:10 25 4
gpt4 key购买 nike

我想解密加密的消息摘要。我的java程序中有这段代码:

 String bobSignedMsg = SignedMsg;

//At the receiving end, Bob extracts the msg
// length, the msg text, and the digital
// signature from the signed msg.
//Get the message length.
int MsgLen = Integer.parseInt(bobSignedMsg.trim().substring(bobSignedMsg.length()-6));
System.out.println(
"\n12. Bob's calculated msg len: "
+ MsgLen);

//Get the message text.
String bobMsgText = bobSignedMsg.substring(
0,MsgLen);
System.out.println(
"\n13. Bob's extracted msg text: "
+ bobMsgText);

//Bob knows that everything following the msg
// text except for the four characters at the
// end that indicate the message length is
// the encoded and encrypted version of the
// extended digital signature. He extracts
// it.
String bobExtractedSignature =
bobSignedMsg.substring(
MsgLen,bobSignedMsg.length() - 6);
System.out.println(
"\n14. Bob's extracted extended digital "
+ "signature: "
+ bobExtractedSignature);
byte[] strtodecrypt=bobExtractedSignature.getBytes();

byte[] decryptedCardNo = obj.rsaDecrypt(strtodecrypt,PbkeyPath);
String decryptedform = obj.byteArrayToHexStr(decryptedCardNo);
System.out.println("After Decryption: "+decryptedform);

在上面的代码行中

byte[] decryptedCardNo = obj.rsaDecrypt(strtodecrypt,PbkeyPath);

调用函数:

public byte[] rsaDecrypt(byte[] sampleText,String pbkeypath) {
PublicKey pubKey = null;
try {
pubKey = readKeyFromFile(pbkeypath);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Cipher cipher = null;
try {
cipher = Cipher.getInstance("RSA");
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
cipher.init(Cipher.DECRYPT_MODE, pubKey);
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
byte[] cipherData = null;
try {
cipherData = cipher.doFinal(sampleText);
// cipherData = cipher.
} catch (IllegalBlockSizeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (BadPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return cipherData;
}

但它给出了以下错误:

javax.crypto.IllegalBlockSizeException: Data must not be longer than 128 bytes
at com.sun.crypto.provider.RSACipher.a(DashoA13*..)
at com.sun.crypto.provider.RSACipher.engineDoFinal(DashoA13*..)
at javax.crypto.Cipher.doFinal(DashoA13*..)

我不明白如何解决 block 大小异常的错误......如果有人可以帮助我一些想法,这将对我的项目有很大帮助。

最佳答案

分组密码(例如 RSA)只能加密不超过 blockSize 字节。如果您想使用相同的 key 加密任意大量的数据,您可以将其拆分为blockSize的部分,并单独加密每个 block 。这同样适用于解密。

关于java - 解密加密消息摘要时出现非法 block 大小异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11558010/

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