I am able to create a public-private keypair using SecKeyGeneratePair
[Apple CryptoExercise]function.
Q1. The keys in the keychain appear as without displaying any name. How can we add a friendly name to the keys.
Q2. However how can i export public and private key that has been generated in the usable format:
-----BEGIN RSA PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqCWtYiGnhAv...
-----END RSA PUBLIC KEY-----
and:
-----BEGIN PRIVATE KEY-----
-----END PRIVATE KEY-----
Note that they can be manually exported from the keychain but how can this be achieved using objective C Apis.
Any help would be appreciable.
There is a similar question here but without any answer: iPhone: How do you export a SecKeyRef or an NSData containing public key bits to the PEM format? There is no need of using OpenSSL just for this purpose.
Open Encryption Desktop. Click "PGP Keys". Right-click the key to export then select Send To from the drop-down list. The option to send the public key to a key server listed in the drop-down list, a smart card, or to mail recipient can be chosen.
You cannot generate private key from public key but you can generate public key from the private key using puttygen. As @alfasin mentioned if you could generate the private key from public key then RSA would be useless and this would make you vulnerable to attack.
To export an OpenPGP key pair or a public key, simply go to your account Settings -> Messages -> Encryption: For exporting Key pair/Personal key: under My personal keys: Click on the Key pair that you would like to export. Click on Export.
Maybe you could refer to these documents from Apple:
Obtaining a SecKeyRef Object for Public Key Cryptography and Certificate, Key, and Trust Services Programmer’s Guide
Obtaining a SecKeyRef Object for Public Key Cryptography
Extracting Keys from the Keychain If you are using existing public and private keys from your keychain, read Certificate, Key, and Trust Services Programming Guide to learn how to retrieve a SecKeychainItemRef object for that key.
Once you have obtained a SecKeychainItemRef, you can cast it to a SecKeyRef for use with this API.
Importing Existing Public and Private Keys Importing and exporting public and private key pairs is somewhat more complicated than generating new keys because of the number of different key formats in common use.
This example describes how to import and export a key pair in PEM (Privacy Enhanced Mail) format.
To export keys to a CFDataRef object
OSStatus oserr = SecItemExport(publickey, externalFormat, // See SecExternalFormat for details flags, // See SecItemImportExportFlags for details ¶ms, (CFDataRef *)&pkdata); if (oserr) { fprintf(stderr, "SecItemExport failed (oserr=%d)\n", oserr); exit(-1); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With