title: How to generate an RSA asymmetric Key Pair in Swift for IOS?
body: <p>I need a way to generate an RSA asymmetrical key pair in Swift. I don't need to store it in the keychain or anything. I just need to generate a key pair and shove both keys into String variables. </p>
  
  <p>The keys do need to be compatible with PHP on the other end. I will use symmetrical encryption to secure the private key and store it on the phone. I'll be sending the public key to a web service that is implemented in PHP, and the web service will store the public key in a database. </p>
  
  <p>That public key will be used later by the web service to encrypt values like one-time passwords and other sensitive values destined for the IOS app. I'll implement a similar scheme for small pieces of data flowing from the IOS app to the web service.</p>
  
  <p>The only documented API declaration I could find for generating a key pair in Swift is shown on developer.apple.com: </p>
  
  <pre><code>func SecKeyGeneratePairAsync(_ parameters: CFDictionary!,
                             _ deliveryQueue: dispatch_queue_t!,
                             _ result: SecKeyGeneratePairBlock!)
  </code></pre>
  
  <p>I tried to figure out how to use this, but XCode doesn't like the underscores, and I'm not sure what I am actually supposed to do with this or how to use it.</p>
  
  <p>Even if XCode would accept it, I am not sure how I would call the function and what values to pass it, etc. I included "import Security" at the top, so that's not the problem. </p>
  
  <p>It's ridiculous that this should be so hard. All I want to do is generate an asymmetric key pair. </p>
  
  <p>It's a piece of cake to do this in PHP or .NET or Java or any other language, but I can't find any clear documentation on it for Swift. I have to use Swift for this app. I don't want to use OpenSSL because it's deprecated. </p>
  
  <p>I am using Swift because I bloody hate objective C. Swift is the reason I am finally jumping into IOS development. </p>
  
  <p>I don't know how to integrate an Objective C class with Swift, and I'd really rather have a pure Swift solution if there is one. If not, I'd appreciate some pointers on how to integrate an Objective C solution and make it work.</p>
  
  <p>The above snippet is the only function call Apple provides, and naturally it's  incomplete, doesn't make sense, and doesn't work.</p>