title: Convert pem public key to ssh-rsa public keys using "ssh-keygen" in iOS
body: <p>I want to pem public key and I want to generate ssh-rsa public keys using "ssh-keygen". I can achieve this using ssh-keygen in terminal (as show below)</p>
  
  <pre><code>ssh-keygen -i -f publicKey.pem -m PKCS8 -i &gt; pubkey-ssh.pub
  </code></pre>
  
  <p>"ssh-rsa" public key form above command (expected):</p>
  
  <pre><code>ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC6qU2kfO17CC8iI8ZQdv+1ajM9AInm4RR6k1mBuGHXfvq87Y9ly/NihzQxWmkUvMKm8RVg4s+2Dj+vO1P06Q3/lTgDGMUHajPzljcVvqYkJU3hxDbH4h+FRgR/Lf/6q6b2M70qHSwSIAW3kK+iTedPximOOZeAOISa50wqE/Tr6hy5lxbAFMt54OSCgxiUSXql3C+e2zPRwihTcpk1eZZE1LVvw09gOOM4cNi4Xc3xkJZL/1LOA3aoZdjxkCIa1fdVFZ4aJRtZyoYAXPTGtVJcLiJlaxD4JKp/Dgoy9N88nuyYLtrNyuxq5i0Qs8Spx/jZQ+5Bu47ozeX5kAGPDUo9
  </code></pre>
  
  <p>I want to achieve the above unix command in iOS app using Objective-C (or c). 
  I found some similar post <a href="https://stackoverflow.com/questions/40863651/how-to-create-openssh-keys-on-ios-via-objective-c-code">here</a>, but doesn't work for me.
  I also found <a href="https://github.com/openssh/openssh-portable" rel="nofollow noreferrer">openSSH c library</a>, but can't figure out due to lack of documentation.</p>
  
  <p>Please help me any any one having any idea on this. Thanks in advance.</p>
  
  <p>Update: My Implementation</p>
  
  <pre><code>NSData *data = nil;
  const unsigned char *pp = (const unsigned char *)pkcs1Data.bytes;
  RSA *rsa = d2i_RSAPublicKey(NULL, &amp;pp, pkcs1Data.length);
  if (rsa) {
      unsigned char *pOut = NULL;
      int length = i2d_RSA_PUBKEY(rsa, &amp;pOut);
      if (pOut) {
          if (length &gt; 0) {
              data = [NSData dataWithBytes:pOut length:length];
          }
          free(pOut);
      }
      RSA_free(rsa);
  }
  </code></pre>