1

In iOS app, I need to encrypt NSString with RSA Algorithm, I have known public modulus and exponent key.On Android side they have used bouncy castle ApI for RSA Encryption.Can some one help me how to encrypt how can I encrypt NSString with these Public Modulus and Exponent key as could generate similar encryption as in Android.I have looked on many source code available but could not generate the correct encryption.Any help would be highly appreciated. Modulus key That I have is "117130940722358865944076735715016871148960803304334901248996815419815052552875336322790410991392433604701394608500231884113911915168625416296669114728862690539451024021812353340986348428958506523689933432584403548435474622224828221548841371083486321081622447517054022904372023020885356296462823306439795173749" Exponent is "65537" Please help me to encrypt NSString with RSA Encryption.

  • post what you have tried – Durgaprasad Apr 12 '13 at 11:08
  • I have tried it with the following code snippet but it gives weird results http://stackoverflow.com/questions/10072124/iphone-how-to-encrypt-nsdata-with-private-key-and-decrypt-with-public-key/10072378#10072378 – user2273864 Apr 12 '13 at 11:16

2 Answers2

0

You can use openSSL for this

https://www.openssl.org/docs/man1.1.0/crypto/RSA_public_encrypt.html

#include <openssl/rsa.h>
 int RSA_public_encrypt(int flen, unsigned char *from,
    unsigned char *to, RSA *rsa, int padding);
 int RSA_private_decrypt(int flen, unsigned char *from,
     unsigned char *to, RSA *rsa, int padding);

Be aware of export compliance though Using SSL in an iPhone App - Export Compliance

You may be better trying to get the Common Crypto equivalent to work

GolezTrol
  • 109,399
  • 12
  • 170
  • 196
gheese
  • 1,102
  • 1
  • 10
  • 17
  • I have Public Key Modulus "117130940722358865944076735715016871148960803304334901248996815419815052552875336322790410991392433604701394608500231884113911915168625416296669114728862690539451024021812353340986348428958506523689933432584403548435474622224828221548841371083486321081622447517054022904372023020885356296462823306439795173749" and exponent "65537", how can I use them to generate RSA Encrypted NSString? – user2273864 Apr 12 '13 at 11:35
0

Being in iOS, you probably need to work without extra libs like OpenSSL. But to do so, you must have some kind of "big numbers" library. I am not using iOS, but a search gives this mention of "huge numbers" library, discussed here: Store and perform operations with huge numbers in iOS With this library, you need only RSA formulas. Fast search gives this article http://www.linuxjournal.com/article/6695, you don't need to all of it, just scroll down to mention of ModExp - this is an operation you need to encrypt a message when you have key and modulus available.

Community
  • 1
  • 1
Jomu
  • 364
  • 2
  • 5