6

I am writing iOS Application. Server sends RSA public key to application. Now application has to encrypt some information using RSA algorithm

Kindly provide me some reference. Thanks

2

3 Answers 3

4

iOS has no special API for RSA, but there are some APIs about Certificate. You can use these APIs to encrypt your data by RSA.

First, you must use openssl to generate your RSA private key and public key. The most important thing is that the public key must be signed. Here is a instruction to generate the keys.

openssl req -x509 -out public_key.der -outform der -new -newkey rsa:1024 -keyout private_key.pem -days 3650

However, if you already has a private key(.pem file), you can follow the instructions:

openssl req -new -out cert.csr -key private_key.pem
openssl x509 -req -in cert.csr -out public_key.der -outform der -signkey private_key.pem -days 3650

You can check the public_key.der by opening it in xcode.

When you get the correct public_key.der file, you can view the RSA.h and RSA.m here. I'm sorry that I have no time to rewrite this post by English again.

3
  • 1
    Perfect, thank you! Do you have the openssl code to retrieve the information encrypted with those keys?
    – xlarsx
    Jul 3, 2012 at 17:22
  • hello, you told to generate keys with the certificate, but i have public key that is provided by the JAVA developer, so i want to decrypt message using that key...so how can i do that ?
    – Apple
    Oct 10, 2013 at 12:42
  • I suppose your key is a string. Sorry that I don't know how to convert a string key to a file.
    – zsxwing
    Oct 11, 2013 at 1:46
1

This Pod encapsulates the encryption: https://github.com/xjunior/XRSA

0

I don't know much about iOS but the Certificate, Key, and Trust Services Reference seems to be what you need. It appears the SecKeyEncrypt will be used by you at some point.

2
  • 2
    yes, To use this API, i need SecKeyRef of RSA public ey, which i have as NSSTring. I am not able to get it's SecKeyRef. I am using iOS 4.2
    – macdev30
    Sep 5, 2011 at 15:32
  • @macdev30, I am struggling with the same issue: encrypting with a NSString RSA public key. Did you manage to solve this? Jun 11, 2014 at 17:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.