Is RSA PKCS1-OAEP padding supported in bouncycastle?

后端 未结 2 765
感动是毒
感动是毒 2020-12-29 12:55

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

Howev

相关标签:
2条回答
  • 2020-12-29 13:35

    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);
    
    0 讨论(0)
  • 2020-12-29 13:48

    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");
    
    0 讨论(0)
提交回复
热议问题