Почему я получаю исключение BadPaddingException при расшифровке RSA?

Я использовал алгоритм RSA для шифрования и дешифрования. Когда я шифрую строку, она работает правильно. Когда я расшифровываю, я получаю ошибку. Ниже я публикую свой код.

public final String modulusString ="..............";
public final String publicExponentString = "AQAB";

/* Encryption */
byte[] modulebytes = Base64.decode(modulusString);
byte[] exponentbytes = Base64.decode(publicExponentString);
BigInteger module = new BigInteger(1,modulebytes);
BigInteger publicexponent = new BigInteger(1,exponentbytes);
RSAPublicKeySpec rsaPubKey = new RSAPublicKeySpec(module, publicexponent);
KeyFactory fact = KeyFactory.getInstance("RSA");
PublicKey pubKey = fact.generatePublic(rsaPubKey);

Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE, pubKey);

byte[] plainBytes = EncryptionValue.getBytes("UTF-8");
byte[] cipherData = cipher.doFinal( plainBytes );
String encryptedString = Base64.encode(cipherData);

return encryptedString;

/* Decryption */
byte[] modulebytes = Base64.decode(modulusString);
byte[] exponentbytes = Base64.decode(publicExponentString);

BigInteger modulus = new BigInteger(1, modulebytes );
BigInteger exponent = new BigInteger(1, exponentbytes);

RSAPrivateKeySpec rsaPrivKey = new RSAPrivateKeySpec(modulus, exponent);
KeyFactory fact = KeyFactory.getInstance("RSA");
PrivateKey privKey = fact.generatePrivate(rsaPrivKey);

Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.DECRYPT_MODE, privKey);

byte[] base64String = Base64.decode(DecryptionValue);
byte[] plainBytes = new String(base64String).getBytes("UTF-8");
plainBytes = cipher.update(plainBytes);
byte[] values = cipher.doFinal(plainBytes);

return new String(values, "UTF-8");
Исключение в потоке "main" javax.crypto.BadPaddingException: ошибка дешифрования
  at sun.security.rsa.RSAPadding.unpadV15(RSAPadding.java:380)
  at sun.security.rsa.RSAPadding.unpad(RSAPadding.java:291)
  на com.sun.crypto.provider.RSACipher.doFinal(RSACipher.java:363)
  на com.sun.crypto.provider.RSACipher.engineDoFinal(RSACipher.java:389)
  на javax.crypto.Cipher.doFinal(Cipher.java:2121)
  at cryptocodefinal.CryptoCodeFinal.DecryptionValue(CryptoCodeFinal.java:79)
  at cryptocodefinal.CryptoCodeFinal.main(CryptoCodeFinal.java:148)

1 ответ

Вы, кажется, расшифровываете с открытым ключом. Это не сработает. Вам необходимо расшифровать с частным показателем, который идет вместе с общедоступным показателем, используемым для шифрования.

Вы не можете расшифровать без закрытого ключа. В этом смысл асимметричной криптографии.

Другие вопросы по тегам