How to decrypt with RSA and padding OAEP

Hi


Here is my situation :

- I generate a RSA key pair with SecKeyGeneratePair

- I send the public key to my server

- Server uses it to encrypt data/string with RSA algorithm and padding OAEP

- I want to decrypt the data/string it returns to me with my private key but I can't


I would like to know if it is possible to decrypt a data/string given by the server encrypted with RSA and padding OAEP?


I looked for an answer with SecKeyDecrypt() but it seems impossible to use it for OAEP, so is there any other option for me to do it?


Thanks in advance


EDIT : I forgot to precise i'm looking for a solution on iOS min 8.0

You should take a look at the CryptoCompatibility sample code, which shows how to do RSA crypto using our three different RSA APIs [1] in a way that’s compatible with common third-party crypto toolkits.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

[1] iOS’s legacy raw API, macOS’s legacy transforms API, and the new unified API we introduced in the 2016 OS releases.

Thank you for the answer.


I already saw that CryptoCompatibily stuff in one of your other answer, but I can't manage to make it work.


According to CryptoCompatibility sample of code, there is SecKeyCreateDecryptedData available on iOS 10.0 (in QCCRSASmallCryptor) so I can't use this and there is secKeyDecrypt, which I already tried, that leads me to several problems :

  • When I tried with padding OAEP or PKCS1, I'm unable to have any other return code than "-9809", which basically means "something went wrong but no idea why" (the data I'm trying to decrypt is changed according to the padding each time of course)
  • So when I try with no padding, because it's the only option I see, the decrypt method works but when I want to compare the value decrypted, with the value that the server encrypts (don't know if it is the right thing to do), I have two problems :
    • The value does not match (I can't cast it to nsstring, so I try to compare the data)
    • The buffer I obtain is entirely filled, whether the data the server encrypts is not that long (I set the size of the buffer according to the few examples I found that tell to put it at the size of the key)


There're too many things not working that I don't know where to start at resolving them :/

Hi Gerth,


Did you manage to find a solution for your problem? I am in the same situation, whatever padding I try, decryption fails or without padding, data can not be transformed into string. Please let me know if you found a working solution.


Thank you,

Levente

One trick here is to decrypt the data in raw mode (that is,

kSecPaddingNone
) and then look at the decrypted data to see what’s going on with the padding.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

for fun, i would design a code breaker based around qubits and test it at IBM's qc portal. i'd guess 20 qubits would break it fairly quickly.

How to decrypt with RSA and padding OAEP
 
 
Q