Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RSA Encryption using public key

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

like image 624
macdev30 Avatar asked Sep 05 '11 08:09

macdev30


People also ask

Can you encrypt with RSA public key?

RSA(Rivest-Shamir-Adleman) is an Asymmetric encryption technique that uses two different keys as public and private keys to perform the encryption and decryption. With RSA, you can encrypt sensitive information with a public key and a matching private key is used to decrypt the encrypted message.

Can you decrypt RSA with public key?

Data encrypted with the public key can only be decrypted with the private key.


2 Answers

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.

like image 158
zsxwing Avatar answered Sep 28 '22 01:09

zsxwing


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

like image 22
xjunior Avatar answered Sep 28 '22 01:09

xjunior