java bouncycastle 是否支持 RSA PKCS1-OAEP 填充?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17110217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 01:01:50  来源:igfitidea点击:

Is RSA PKCS1-OAEP padding supported in bouncycastle?

javaandroidsecuritybouncycastle

提问by scottyab

I'm implementing encryption code in Java/Android to match iOS encryption. In iOS there are encrypting with RSA using the following padding scheme: PKCS1-OAEP

我正在 Java/Android 中实现加密代码以匹配 iOS 加密。在 iOS 中,使用以下填充方案使用 RSA 进行加密:PKCS1-OAEP

However when I try to create Cipher with PKCS1-OAEP.

但是,当我尝试使用 PKCS1-OAEP 创建密码时。

Cipher c = Cipher.getInstance("RSA/None/PKCS1-OAEP", "BC");

Below is the stacktrace

下面是堆栈跟踪

javax.crypto.NoSuchPaddingException: PKCS1-OAEP unavailable with RSA.
    at com.android.org.bouncycastle.jcajce.provider.asymmetric.rsa.CipherSpi.engineSetPadding(CipherSpi.java:240)
    at javax.crypto.Cipher.getCipher(Cipher.java:324)
    at javax.crypto.Cipher.getInstance(Cipher.java:237) 

Maybe this RSA/None/PKCS1-OAEPis incorrect? but can't find any definitive answer to say either PKCS1-OAEP is unsupported or the correct way to define it.

也许这RSA/None/PKCS1-OAEP是不正确的?但找不到任何明确的答案来说明 PKCS1-OAEP 不受支持或定义它的正确方法。

I'm using the spongycastle library so have full bouncycastle implementation.

我正在使用 spongycastle 库,所以有完整的 bouncycastle 实现。

回答by divanov

The code in the first answer does work, but it's not recommended as it uses BouncyCastle internal classes, instead of JCA generic interfaces, making the code BouncyCastle specific. For example, it will make it difficult to switch to SunJCE provider.

第一个答案中的代码确实有效,但不推荐使用,因为它使用 BouncyCastle 内部类,而不是 JCA 通用接口,从而使代码特定于 BouncyCastle。例如,切换到 SunJCE 提供程序将变得困难。

Bouncy Castle as of version 1.50 supports following OAEP padding names.

Bouncy Castle 自 1.50 版起支持以下 OAEP 填充名称。

  • RSA/NONE/OAEPWithMD5AndMGF1Padding
  • RSA/NONE/OAEPWithSHA1AndMGF1Padding
  • RSA/NONE/OAEPWithSHA224AndMGF1Padding
  • RSA/NONE/OAEPWithSHA256AndMGF1Padding
  • RSA/NONE/OAEPWithSHA384AndMGF1Padding
  • RSA/NONE/OAEPWithSHA512AndMGF1Padding
  • RSA/NONE/OAEPWithMD5AndMGF1Padding
  • RSA/NONE/OAEPWithSHA1AndMGF1Padding
  • RSA/NONE/OAEPWithSHA224AndMGF1Padding
  • RSA/NONE/OAEPWithSHA256AndMGF1Padding
  • RSA/NONE/OAEPWithSHA384AndMGF1Padding
  • RSA/NONE/OAEPWithSHA512AndMGF1Padding

Then proper RSA-OAEP cipher initializations would look like

然后正确的 RSA-OAEP 密码初始化看起来像

Cipher c = Cipher.getInstance("RSA/NONE/OAEPWithSHA1AndMGF1Padding", "BC");

回答by scottyab

The following code works, if anyone else is stuck with similar encryption encoding/padding issues

以下代码有效,如果其他人遇到类似的加密编码/填充问题

    SubjectPublicKeyInfo publicKeyInfo = new SubjectPublicKeyInfo(
            ASN1Sequence.getInstance(rsaPublicKey.getEncoded()));

    AsymmetricKeyParameter param = PublicKeyFactory
            .createKey(publicKeyInfo);
    AsymmetricBlockCipher cipher = new OAEPEncoding(new RSAEngine(),
            new SHA1Digest());
    cipher.init(true, param);

    return cipher.processBlock(stuffIWantEncrypted, 0, 32);