gpt4 book ai didi

java - RSA数据解密错误Java : javax. crypto.BadPaddingException: Decryption error

转载 作者:行者123 更新时间:2023-11-29 05:18:27 27 4
gpt4 key购买 nike

我在使用 Java 中的 RSA 解密数据(或者也可能是错误的加密)时遇到了问题。我想用字符串中的更多信息加密公钥,然后解密这个公钥并用它加密一些东西(我使用 2048 RSA):

加密:

public void saveExportToFile(String fileName, BigInteger mod, BigInteger exp, String info, PublicKey puk) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oout = new ObjectOutputStream(new BufferedOutputStream(baos));
try {
oout.writeObject(mod);
oout.writeObject(exp);
oout.writeChars(info);
oout.close();
baos.close();
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, puk);

FileOutputStream fos = new FileOutputStream(new File(fileName));
BufferedOutputStream bos = new BufferedOutputStream(fos);

byte[] data = baos.toByteArray();

int i = 0;
byte[] buffer = new byte[128];
byte[] cipherData = null;
while (i < data.length) {
if (i+128 >= data.length) {
buffer = new byte[data.length - i];
System.arraycopy(data, i, buffer, 0, data.length - i);
cipherData = cipher.doFinal(buffer);
bos.write(cipherData);
} else {
System.arraycopy(data, i, buffer, 0, 128);
cipherData = cipher.doFinal(buffer);
bos.write(cipherData);
}
i += 128;
}

bos.close();
} catch (Exception e) {
throw new IOException("Unexpected error", e);
}
}

解密:

public void getDataFromRSA(String sendname, PrivateKey privateKey) {
try {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File(sendname)));

Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);

int length = 0;
int allLength = 0;
byte[] buffer = new byte[128];
byte[] bufferAC = null;
byte[] outData = null;
byte[] allData = null;
byte[] tmpData = null;
while ( (length = bis.read(buffer)) != -1) {
if (length < 128) {
bufferAC = new byte[length];
System.arraycopy(buffer, 0, bufferAC, 0, length);
outData = cipher.doFinal(bufferAC);
} else {
outData = cipher.doFinal(buffer); // HERE IS THE ERROR
}
allLength += outData.length;
tmpData = allData;
allData = new byte[allLength];
System.arraycopy(tmpData, 0, allData, 0, tmpData.length);
System.arraycopy(outData, 0, allData, tmpData.length, outData.length);
}
} catch (IOException | NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException | ClassNotFoundException | InvalidKeySpecException e) {
e.printStackTrace();
}
}

编辑好吧,看来我对加密的了解并没有我想象的那么多。我只想使用 RSA(如果可能的话),因为我不需要多次传输信息(信息大小各不相同)。我已经像这样编辑了加密:

int i = 0;
byte[] buffer = new byte[245];
byte[] cipherData = null;
while (i < data.length) {
if (i+245 >= data.length) {
buffer = new byte[data.length - i];
System.arraycopy(data, i, buffer, 0, data.length - i);
} else {
System.arraycopy(data, i, buffer, 0, 245);
}
cipherData = cipher.update(buffer);
bos.write(cipherData);
i += 245;
}
bos.write(cipher.doFinal()); // HERE IS THE ERROR
bos.close();

现在我得到 javax.crypto.IllegalBlockSizeException: Data must not be longer than 245 bytes(尝试了几个较低的缓冲区大小值)。是因为数据长度不是 block 大小的倍数吗?这可以修复吗?感谢您的回答。

最佳答案

首先,您应该使用混合加密,即首先使用对称密码加密数据,然后使用 RSA key 加密随机 secret - 将两者发送到接收方。

其次,您永远不必在循环中为单个消息执行 doFinal。请改用 update 和一个 doFinal

第三,2048 位是 256 字节。只要您继续尝试解密 128 字节而不是 256 字节,就会出现此异常。通常我使用 2048/Byte.SIZE 代替,它使代码更具可读性并且会避免错误。

关于java - RSA数据解密错误Java : javax. crypto.BadPaddingException: Decryption error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25698921/

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