“javax.crypto.BadPaddingException: Data must start with zero” exception

前端 未结 4 1279
慢半拍i
慢半拍i 2020-12-17 08:31

I encountered the abovementioned exception while I was decrypting a string.

Below is my code:

import java.security.KeyPair;
import java.security.KeyP         


        
4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-17 08:42

    You already asked the same question in "javax.crypto.BadPaddingException: Data must start with zero" exception, and I gave you an answer: you're using two different keypairs : one to encrypt, and another one to decrypt. That can't work. I even gave you a code sample showing that everything ran fine if you used the same keypair.

    KeyPairGenerator.generateKeyPair() generates a keypair. Calling this method twice will get you two different keypairs: it uses a random number generator internally to generate always different keypairs.

    You must generate a keypair once, store it in a variable, and use this variable to encrypt and decrypt.

    You should read the documentation of the classes and methods you are using. The documentation of generateKeyPair says:

    This will generate a new key pair every time it is called.

提交回复
热议问题