1

Implementing RSA encryption on kotlin is so simple first of all I used transformation "RSA/ECB/OAEPWithSHA-1AndMGF1Padding" then we changed it to support SHA-256 and changing the above code to "RSA/ECB/OAEPWithSHA-256AndMGF1Padding" everything worked perfectly.

The the swift code, to match "RSA/ECB/OAEPWithSHA-1AndMGF1Padding" using SwiftyRSA you need to set the padding to OAEP.

And after changing it in kotlin to support SHA-256 I tried all the values inside SecKey with no luck.

Here is my code how it used to be and it was working fine:

 do {
        let publicKey = try PublicKey(pemNamed: "pubkey")
        let clear = try ClearMessage(string: self, using: .utf8)
        
        let encrypted = try clear.encrypted(with: publicKey, padding: .OAEP)
        
        let base64String = encrypted.base64String
        
        //print(base64String)
        return base64String
    } catch {
        print(error)
        return self
    }

And now I need to change the code to support "RSA/ECB/OAEPWithSHA-256AndMGF1Padding"

Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
4kr4m
  • 69
  • 2
  • 11
  • Take a look [at my answer here](https://stackoverflow.com/a/32166210/589259). I suspect Swift uses SHA-256 for MGF1. I would recommend to you to configure Java / Kotlin to use MGF1 with SHA-256 as well using `OAEPParameterSpec`. – Maarten Bodewes Apr 05 '22 at 13:49
  • 1
    A quick look into the abominable API documentation (read: .swift and .h files) shows that only one hash algorithm can be configured. It doesn't say for what purpose, but I assume it is for both the label and MGF1. Apple documentation for crypto is 10 x as bad as for Microsoft, and that's saying something. – Maarten Bodewes Apr 05 '22 at 13:57

0 Answers0