0

我正在做一个项目,我需要在 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() 将其转换为字节数组。如果我使用更新方法,它会给我一个 IllegalBlockException 错误,即数据不能大于模数。

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

4

1 回答 1

3

像 RSA 这样的非对称密码旨在加密短数据,通常是对称密钥,而大数据使用对称分组密码加密(对称密钥将与非对称密码交换)。StackOverflow 上确实有很多类似的问题和答案。是一个提供了很好的答案。

于 2014-05-06T08:56:05.070 回答