RSA BadPaddingException : data must start with zero

后端 未结 2 1422
忘掉有多难
忘掉有多难 2021-01-01 06:55

I try to implement an RSA algorithm in a Java program. I am facing the \"BadPaddingException : data must start with zero\". Here are the methods used to encrypt and decrypt

相关标签:
2条回答
  • 2021-01-01 07:52

    So far, I've tracked this down to CipherSpi.engineDoFinal(). The explanation provided for BadPaddingException is

    [Thrown when the] cipher is in decryption mode, and (un)padding has been requested, but the decrypted data is not bounded by the appropriate padding bytes

    More specific behavior seems to be Spi-dependent. Do you know what Spi you're using? Just the default?

    EDIT: Unfortunately, I'm having a hard time tracking down what the default Spi is. It looks like it's chosen in Cipher.java's chooseFirstProvider() method, lines 644 to 721 here. If you have a debugger handy, you might want to find out what Spi you have, then check the documentation for it... but I still think it's more likely that something's going on with the input, not the logic (I asked about that earlier, in a comment).

    0 讨论(0)
  • 2021-01-01 07:57

    In principle the code looks OK -- I would put some logging in to make sure the key you're generating is genuinely the one that is then read from the file (you're not doing something silly like generating data with a new key, then trying to read it with an old one, for example?)

    0 讨论(0)
提交回复
热议问题