java rsa oaep_Bouncy Castle vs Java默认RSA与OAEP

有人可以向我解释为什么这段代码抛出javax.crypto.BadPaddingException:解密密钥时最后一行的解密错误?

// Given an RSA key pair...

KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");

keyGen.initialize(2048);

KeyPair keyPair = keyGen.genKeyPair();

PrivateKey privateKey = keyPair.getPrivate();

PublicKey publicKey = keyPair.getPublic();

// ... and an AES key:

KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");

keyGenerator.init(256);

SecretKey aesKey = keyGenerator.generateKey();

// When I encrypt the key with this Bouncy Castle cipher:

Cipher encryptionCipher = Cipher.getInstance("RSA/NONE/OAEPWithSHA256AndMGF1Padding", "BC");

encryptionCipher.init(Cipher.ENCRYPT_MODE, publicKey);

byte[] encryptedKey = encryptionCipher.doFinal(aesKey.getEncoded());

// Then trying to decrypt the key with this cipher...

Cipher decryptionCipher = Cipher.getInstance("RSA/ECB/OAEPWITHSHA-256ANDMGF1PADDING");

decryptionCipher.init(Cipher.DECRYPT_MODE, privateKey);

// ... throws `javax.crypto.BadPaddingException: Decryption error` here:

decryptionCipher.doFinal(encryptedKey);

“RSA/ECB/PKCS1Padding” actually doesn’t implement ECB mode encryption.

It should have been called “RSA/None/PKCS1Padding” as it can only be

used to encrypt a single block of plaintext (or, indeed a secret key).

This is just a naming mistake of Sun/Oracle.

如果是这样,我希望这些转换是等价的,我的测试将通过.两者都指定了相同的填充,为什么BadPaddingException?

无论哪种方式,我都会感谢外行人对不同之处的解释.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值