Is RSA PKCS1-OAEP padding supported in bouncycastle?

匿名 (未验证) 提交于 2019-12-03 02:20:02

问题:

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

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)  

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.

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

回答1:

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.

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

  • 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

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


回答2:

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


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!