2

I am new to the subject, and would appreciate any help / direction on how to implement public / private key encryption for iOS Applications. Looks like the Security Framework will do what I need but I want to make sure I have the best advise.

Basically, User A and User B want to communicate securely. User A will have User B public key, and use that to encrypt the message. User B then gets the message and decrypts it with the private key.

So I guess the questions are:

  1. What is the best way to generate the public / private keys. Would that be OpenSSL?

  2. If it is OpenSSL, is there a way to do that within the iOS device, or do I need to rely on a server / outside generation for the private / public keys?

  3. If generating the private / public keys outside iOS device is the only option, is this a simple matter of making the iOS device request the key files through HTTP, like a normal download?

  4. When finally users have the publica and private keys, is there a simple tutorial / example showing how to encrypt and decrypt the message using RSA (Is RSA the option btw)?

marcelorocks
  • 2,005
  • 3
  • 18
  • 28

1 Answers1

0
  1. Avoid OpenSSL if possible, use the Apple crypto methods on OSX/iOS.
  2. On the server side OpenSSL may be your best bet.
  3. You can use https to transfer keys safely.
  4. The following links provide good example code:

To generate the keys use SecKeyGeneratePair() See SO Question for example code.

To get the keys for transport use SecItemCopyMatching () See SO Answer for example code.

For example code to perform RSA encryption see this SO Answer

Notes:

The private/public key are generated together on the same machine. The public key can be easily and openly shared but the issue is to authenticate the public key. https is a viable way to transfer the keys.

Generally data is not encrypted/decrypted with pubic/private keys. This is for a couple of reasons: it is very slow, it has a limited data size (limited by the key size). Generally the data is encrypted with symmetric methods such as AES and a random key and the key is encrypted with the public key, sent and decrypted with the private key. Then the that key is used to decrypt the AES encrypted actual data.

OpenSSL is no longer provided by Apple, you would need to get a version of the source you like and build it yourself. The stated reason for not providing OpenSSL is that minor versions are not backward compatible and that created issues when Apple supplies the current version and developers have apps using an incompatible older version.

For a tutorial: Google it.

Community
  • 1
  • 1
zaph
  • 111,848
  • 21
  • 189
  • 228
  • what would you suggest to encrypt data and decrypt it on the other side then? Thanks! – marcelorocks Jun 18 '14 at 20:02
  • I avoid backends these days. Personally if at all possible I just use SSL/Open Transport, https if possible. The RSA component should provide little interoperability between code bases. AES is more of a problem, there are more option and issues and many server-side solutions don't adequately document what they are doing at a lower level. Apple provides the individual components to fit together, if the server-side code sufficiently describes the arraignment Apple crypto can with few exceptions be interoperable. – zaph Jun 18 '14 at 20:16
  • OK, Bottom line. Getting the encryption code to work is the easy part of providing security. If you do not have at least several hundred hours of crypto experience and you want a **secure system** then get a crypto domain expert involved. At a minimum have the crypto scheme and code reviewed. Also a great idea have that person involved in the base crypto system design. One "bug" in security code and it is not secure: goto fail. It took my wife 5+ years full-time to move from being reviewed to being reviewer—lots or red-ink in the process. P.S. I don't do that. – zaph Jun 18 '14 at 20:28
  • "You can use https to transfer keys safely" -- worth noting this is not true if you're trying to protect the secrets from users of the app itself. It is easy to man-in-the-middle an https connection if you own one end of it, like app owners do. – carillonator May 22 '20 at 16:52