Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8591937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:38:08+00:00 2026-06-11T23:38:08+00:00

At run time, my iOS application receives a file with a public-private RSA key-pair,

  • 0

At run time, my iOS application receives a file with a public-private RSA key-pair, generated by someone else’s Java:

KeyPairGenerator keygenerator;
keygenerator = KeyPairGenerator.getInstance("RSA");
keygenerator.initialize(4096);

KeyPair keypair = keygenerator.generateKeyPair();
PrivateKey privateKey = keypair.getPrivate().getEncoded();
PublicKey publicKey = keypair.getPublic().getEncoded();

I have successfully read and used the public key, using this method, which strips some preamble from the key.

I now want to use the private key. The same method doesn’t work, I assumed the preamble is different somehow. The blog suggested that it was importing PKCS#1 PEM keys, but then says they’re binary, so I think they just mean Base64-encoded DER keys. I also found that maybe the keys I have are PKCS#8 encoded instead.

Certainly I can use

openssl pkcs8 -nocrypt -inform der < pk8.der > pvt.pem

on a sample private key and openssl doesn’t complain.

Would it make sense that the public key was PKCS#1 and the private PKCS#8?

But I really would like to use CommonCrypto and the Security Framework rather than linking against OpenSSL if I possibly can. On Mac OS there are functions in libsecurity to read PKCS#8, but this hasn’t made it to iOS yet. I did, honestly, try reading the source but I can’t work out where they actually strip the key.

[TL;DR] How can I strip the version and algorithm PKCS#8 fields from the DER private key, and just get the plain key, using either CommonCrypto or some C/C++/ObjC?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T23:38:09+00:00Added an answer on June 11, 2026 at 11:38 pm

    I couldn’t solve the problem without OpenSSL. So here is a solution that does use OpenSSL.

    Assuming you have a NSData called privateKey with the key, and another called signableData which you want to sign.

    #import <openssl/x509.h>
    #import <openssl/pem.h>
    
    NSURL *cacheDir = [[[NSFileManager defaultManager] URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask] lastObject];
    NSString *infile = [[cacheDir URLByAppendingPathComponent:@"privkey.der"] path];
    
    NSError *error;
    [privateKey writeToFile:infile options:NSDataWritingFileProtectionComplete error:&error];
    if (error) {
        NSLog(@"%@", error);
    } else {
        BIO *in = BIO_new_file([infile cStringUsingEncoding:NSUTF8StringEncoding], "rb");
        PKCS8_PRIV_KEY_INFO *p8inf = d2i_PKCS8_PRIV_KEY_INFO_bio(in, NULL);
        NSLog(@"%i", p8inf->broken);
        EVP_PKEY *pkey = EVP_PKCS82PKEY(p8inf);
        PKCS8_PRIV_KEY_INFO_free(p8inf);
        BIO_free(in);
    
        uint8_t * cipherBuffer = NULL;
    
        // Calculate the buffer sizes.
        unsigned int cipherBufferSize = RSA_size(pkey->pkey.rsa);
        unsigned int signatureLength;
    
        // Allocate some buffer space. I don't trust calloc.
        cipherBuffer = malloc(cipherBufferSize);
        memset((void *)cipherBuffer, 0x0, cipherBufferSize);
    
        unsigned char *openSSLHash = SHA1(signableData.bytes, signableData.length, NULL);
        int success = RSA_sign(NID_sha1, openSSLHash, 20, cipherBuffer, &signatureLength, pkey->pkey.rsa);
        if (success) NSLog(@"WIN");
    
    
        NSData *signed = [NSData dataWithBytes:(const void*)cipherBuffer length:signatureLength];    
    
        EVP_PKEY_free(pkey);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Change icon on of an App in iOS 4 in run-time Is
Hi im createing a iOS application and i would like it to run on
std::ifstream sr(path.c_str()); if (!sr) throw runtime_error(Could not open file '+path+\'); sr.seekg(0, ios::end); streampos lastPos
I am Getting a run time Exception while using this code Runtime rt =
I keep getting a Run Time error that says I am having an Exception
I am trying to run Time Tracker plugin with Redmine 2.0.1 on Bitnami Stack.
I am getting this run time error when using IID_IWebBrowser2 interface of CLSID_InternetExplorer. Run-Time
Just wondering what the run time of lookup for set() is? O(1) or O(n)?
Many times i run time consuming PHP scripts that echo status updates like 'batch
How can I inspect at run time what Hibernate has setup for object mappings?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.