簡體   English   中英

bouncycastle支持RSA PKCS1-OAEP填充嗎?

[英]Is RSA PKCS1-OAEP padding supported in bouncycastle?

我正在Java / Android中實現加密代碼以匹配iOS加密。 在iOS中,使用以下填充方案使用RSA加密:PKCS1-OAEP

但是當我嘗試用PKCS1-OAEP創建Cipher時。

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實現。

第一個答案中的代碼確實有效,但不推薦使用它,因為它使用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");

如果其他人遇到類似的加密編碼/填充問題,則以下代碼有效

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM