【问题标题】:Is RSA PKCS1-OAEP padding supported in bouncycastle?bouncycastle 是否支持 RSA PKCS1-OAEP 填充?
【发布时间】:2013-06-11 04:55:15
【问题描述】:

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

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

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

下面是堆栈跟踪

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) 

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

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

【问题讨论】:

  • 没有更多细节很难说,但它可能类似于RSA/None/OAEPWithSHA1AndMGF1Padding,例如。
  • @vcsjones 我在下面的bouncycastle.org/wiki/display/JA1/Frequently+Asked+Questions 中看到了这一点,但是虽然这确实超过了 NoSuchPaddingException,但它与“PKCS1-OAEP”的填充不同。哪些额外的细节会有所帮助?
  • OAEP 使用 some 类型的散列函数,它是 SHA1 还是其他取决于实现。我们需要更多地了解您的 iOS 实现。例如,如果您在 RSA_public_encrypt 函数中使用了RSA_PKCS1_OAEP_PADDING,即 SHA1 和 MGF1。 developer.apple.com/library/ios/#documentation/System/…。你的 iOS 代码是什么样的?

标签: java android security bouncycastle


【解决方案1】:

第一个答案中的代码确实有效,但不建议这样做,因为它使用 BouncyCastle 内部类,而不是 JCA 通用接口,使代码 BouncyCastle 特定。例如,切换到 SunJCE 提供商会很困难。

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

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

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

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

【讨论】:

  • 来自 BouncyCastle 文档或通过迭代 java.security.Provider.getServices() 返回的集合的其他选项。
【解决方案2】:

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

    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);

【讨论】:

    猜你喜欢
    • 2014-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-14
    • 1970-01-01
    相关资源
    最近更新 更多