Compatibility of .rsaEncryptionOAEPSHA256 algorithm on iOS with Java

Viewed 1009

I am trying to communicate from an iOS application to a Java backend. When using the .rsaEncryptionOAEPSHA256 algorithm in SecKeyCreateEncryptedData for encryption, the backend shows the following stacktrace:

Caused by: java.lang.SecurityException: Exception thrown while invoking doFinal(byte[]).
    at  ... 101 common frames omitted
Caused by: javax.crypto.BadPaddingException: Decryption error
    at java.base/sun.security.rsa.RSAPadding.unpadOAEP(RSAPadding.java:497)
    at java.base/sun.security.rsa.RSAPadding.unpad(RSAPadding.java:292)
    at java.base/com.sun.crypto.provider.RSACipher.doFinal(RSACipher.java:366)
    at java.base/com.sun.crypto.provider.RSACipher.engineDoFinal(RSACipher.java:392)
    at java.base/javax.crypto.Cipher.doFinal(Cipher.java:2207)
    ... 103 common frames omitted

The Java backend is using the RSA/ECB/OAEPWithSHA-256AndMGF1Padding algorithm from the SunJCE provider. This provider uses SHA-1 for the MGF1 digest. However the default for iOS is to use SHA-256.

  • How can I change this in the code for iOS to make this compatible?

Note: To be clear, the exact same interaction is currently working fine with .rsaEncryptionPKCS1 on iOS where the backend uses RSA/ECB/PKCS1Padding in Java.

1 Answers

Faced the same issue, from what I've researched & experimented with, it's practically impossible to support RSA/ECB/OAEPWithSHA-256AndMGF1Padding on iOS.

Similiar issue: Encrypt RSA/ECB/OAEPWithSHA-256AndMGF1Padding Swift

But this only addressed the algorithm but not the padding issue.

Related