Search code examples
objective-copenssh

How to create openssh keys on iOS via Objective-C code


We can get openssh keys by order 'ssh-keygen...', or get openssl keys by order 'openssl genrsa...'. But can I do this in my iOS app via Objective-C code? Any help will be appreciated.


Solution

  • You'll need to use a library like OpenSSL to generate the keys. This isn't Objective-C, but you can interact with C libraries since Objective-C is a superset. You will need to read the documentation to fully understand how the library works, but this is a quick example:

    RSA *myrsa;
    unsigned long e = RSA_3;
    BIO* out = NULL;
    myrsa = RSA_generate_key(2048,e,NULL,NULL);
    
    out=BIO_new(BIO_s_file());
    
    if (myrsa == NULL) {
        /* error handling here /*
    }
    

    You will want to write the key data out somewhere. Without a more specific example of what you are doing, this is the best I can offer.

    For information on using OpenSSL libraries within your project, see: https://github.com/krzyzanowskim/OpenSSL