简体   繁体   English

javax.crypto.BadPaddingException:消息大于模数

[英]javax.crypto.BadPaddingException: Message is larger than modulus

I am working on a project where I need to show some encryption decryption over the RMI network. 我正在开展一个项目,我需要在RMI网络上显示一些加密解密。 I am using RSA system for this. 我正在使用RSA系统。 On decryption, my code is giving me the following error : 在解密时,我的代码给了我以下错误:

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)

This is my decryption code : 这是我的解密代码:

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-----");      
    }

I have tried using cipher.update(byte[] data) while encrypting. 我在加密时尝试使用cipher.update(byte [] data)。 I have String data and I convert it to byte array using string.getByte() while encrypting. 我有String数据,并在加密时使用string.getByte()将其转换为字节数组。 If I use update method, it gives me an error of IllegalBlockException that data cannot be larger than modulus. 如果我使用update方法,它会给我一个IllegalBlockException错误,即数据不能大于模数。

Please help me out in this. 请帮我解决这个问题。 I am unable to find the bug in my code. 我无法在代码中找到错误。

Asymmetric ciphers like RSA are designed to encrypt short data, typically an symmetric key, while large data is encrypted with symmetric block ciphers (which symmetric key will be exchanged with an asymmetric ciphers). 像RSA这样的非对称密码被设计用于加密短数据,通常是对称密钥,而大数据用对称分组密码加密(对称密钥将与非对称密码交换)。 There are really a lot of similar questions and answers on StackOverflow. StackOverflow上有很多类似的问题和答案。 This one is one provides quite good answer. 这个是一个提供了很好的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM