在BouncyCastle的RSA PKCS1-OAEP冗余支持?冗余、RSA、BouncyCastle、OAEP

2023-09-06 09:37:43 作者:ご二度↘犯罪彡

我实施加密code中的Java / Android的匹配iOS的加密。在iOS系统中有使用RSA加密使用以下填充方案:PKCS1-OAEP

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

然而,当我尝试创建密码与PKCS1-OAEP。

However when I try to create Cipher with 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) 

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

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

我使用spongycastle库,以便有充分的BouncyCastle的实施。

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

推荐答案

在code。在第一个答案做的工作,但我们不推荐,因为它使用的,而不是JCA通用接口BouncyCastle的内部类,使得code BouncyCastle的具体。例如,它会使得难以切换到SunJCE提供

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.

充气城堡为1.50版本支持以下OAEP填充名称。

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

在RSA /无/ OAEPWithMD5AndMGF1Padding 在RSA /无/ OAEPWithSHA1AndMGF1Padding 在RSA /无/ OAEPWithSHA224AndMGF1Padding 在RSA /无/ OAEPWithSHA256AndMGF1Padding 在RSA /无/ OAEPWithSHA384AndMGF1Padding 在RSA /无/ OAEPWithSHA512AndMGF1Padding

然后适当的RSA-OAEP密码初始化看起来像

Then proper RSA-OAEP cipher initializations would look like

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