简体   繁体   English

Java RSA 解密 - 错误填充异常

[英]Java RSA Decryption - Bad Padding Exception

This is my code so far.到目前为止,这是我的代码。 Every time I run it, I receive a bad padding exception only while trying to decrypt.每次运行它时,我只会在尝试解密时收到错误的填充异常。 If I swap the use of my private and public keys it still throws the same exception in decrypt.如果我交换我的私钥和公钥的使用,它仍然会在解密时抛出相同的异常。 This makes me believe that it is an issue with the Decrypt itself.这让我相信这是 Decrypt 本身的问题。 Any thoughts on what I am doing wrong?对我做错了什么有任何想法吗?

try {
            Cipher Bob_Message_Verify = Cipher.getInstance("RSA");    
            Bob_Message_Verify.init(Cipher.DECRYPT_MODE, Alice_Pair_I.getPublic());
            Bob_Verification_Message_Decrypt = (Bob_Message_Verify.doFinal(Bob_Verification_Message_Encrypt));

        }catch(NoSuchAlgorithmException e)
        {
            throw new OTPException("Not RSA");
        }
        catch(IllegalBlockSizeException e)
        {
            throw new OTPException("Wrong Block Size");
        }
        catch(NoSuchPaddingException e)
        {
            throw new OTPException("No Padding");
        }
        catch(InvalidKeyException e)
        {
            throw new OTPException("Invalid Key");
        }
        catch(BadPaddingException e)
        {
            throw new OTPException("Bad Padding");
        }

The reason that you are getting an error is that you are trying to verify with a Cipher .您收到错误的原因是您正在尝试使用Cipher进行验证。 You need to use a Signature instance of that.您需要使用它的Signature实例。 Note that the specifics of signature generation are rather different from a cipher .请注意,签名生成的细节与密码有很大不同 Signature verification conversely is not the same as decryption with the public key.相反,签名验证与使用公钥解密不同。

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

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