gpt4 book ai didi

javax.crypto.BadPaddingException : Message is larger than modulus

转载 作者:行者123 更新时间:2023-12-01 23:00:09 25 4
gpt4 key购买 nike

我正在开发一个项目,需要通过 RMI 网络显示一些加密解密。我为此使用 RSA 系统。解密时,我的代码出现以下错误:

javax.crypto.BadPaddingException: Message is larger than modulus
at sun.security.rsa.RSACore.parseMsg(RSACore.java:182)
at sun.security.rsa.RSACore.crypt(RSACore.java:112)
at sun.security.rsa.RSACore.rsa(RSACore.java:103)
at com.sun.crypto.provider.RSACipher.doFinal(RSACipher.java:355)
at com.sun.crypto.provider.RSACipher.engineDoFinal(RSACipher.java:382)
at javax.crypto.Cipher.doFinal(Cipher.java:2087)
at Node.decryptData(Node.java:463)
at Node.receiveMsgL(Node.java:451)
at MiniServer.callLeaderR(MiniServer.java:89)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:322)
at sun.rmi.transport.Transport$1.run(Transport.java:177)
at sun.rmi.transport.Transport$1.run(Transport.java:174)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:173)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:556)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:811)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:670)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)

这是我的解密代码:

private void decryptData(String PrivateK,byte[] data) throws IOException {
System.out.println("\n-------DECRYPTION STARTED----");
byte[] descryptedData = null;

try {
PrivateKey privateKey = readPrivateKeyFromFile(PrivateK);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
descryptedData = cipher.doFinal(data);
System.out.println("Decrypted Data: " + new String(descryptedData));

} catch (Exception e) {
e.printStackTrace();
}

System.out.println("------DECRYPTION COMPLETED-----");
}

我在加密时尝试使用 cipher.update(byte[] data) 。我有字符串数据,并在加密时使用 string.getByte() 将其转换为字节数组。如果我使用 update 方法,它会给我一个 IllegalBlockException 错误,即数据不能大于模数。

请帮我解决这个问题。我无法在我的代码中找到错误。

最佳答案

诸如 RSA 之类的非对称密码旨在加密短数据(通常是对称 key ),而大数据则使用对称 block 密码进行加密(对称 key 将与非对称密码进行交换)。 StackOverflow 上确实有很多类似的问题和答案。 This one这是一个提供了很好的答案。

关于javax.crypto.BadPaddingException : Message is larger than modulus,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23486896/

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