4

I need to write a method in Swift which uses a particular PRIVATE KEY to encrypt a timestamp using RSA. This is NOT used for authentication (rather it validates the client app to the server), I know that you would normally encrypt with a public key to ensure security.

I have a key:

-----BEGIN RSA PRIVATE KEY----- MIICXAIBAAKBgQDIg+wteSjhalc1hSHEiUnz9X1pkrObCjaXMHqeSdfFQ/h5Q1Uh
...
o7wjoqFNxFnQMAYvkLzQZ7Y2jjfSJkaTVnhzJIZOfQ0=
-----END RSA PRIVATE KEY-----

And I need to encrypt a string using this particular key. I have written the Android application version of this app, but if I need to re-generate the keys specifically for iOS it's not a problem, but I would need this file for the Android version as well.

I have looked at the following web sites:

  1. http://jslim.net/blog/2013/01/05/rsa-encryption-in-ios-and-decrypt-it-using-php/ Seems useful but it insists that the key be in a .der format - plus it's in Objective-C. Is it possible to generate a Key pair in both DER and PEM format, or convert between the two (using something like https://www.sslshopper.com/ssl-converter.html)?

  2. https://github.com/henrinormak/Heimdall - but you cannot import a custom private key

  3. https://github.com/ideawu/Objective-C-RSA/blob/master/RSA.m - seems long winded and is also in objective-c

One requirement is that all apps use the SAME key - it cannot be generated by each installation of the app. Also my Android app must be able to use the key as well (I am open to rethinking the Android version as leaving the key as a RAW file is not preferred for me).

Any help will be appreciated.

Other references checked:
How to encrypt a string with private key and decrypt with public key?
Using RSA public key to encrypt a string

Community
  • 1
  • 1
Quintin Balsdon
  • 5,484
  • 10
  • 54
  • 95
  • Did you manage to do this? Going through the same dilemma right now. – Jay Mayu Oct 13 '16 at 11:12
  • I ended up using this library: https://github.com/jslim89/RSA-objc - my original answer was deleted because I did not regurgitate the readme file. Please note that this library does have issues – Quintin Balsdon Oct 13 '16 at 11:15
  • Thanks and if you don't mind please update it in the answer. It might help another user come searching for answer. Thanks again. :) – Jay Mayu Oct 13 '16 at 11:40

0 Answers0