Send Public key(generated as seckeyref in iPhone)

2019-04-05 12:45发布

问题:

I need to send my public key that has been generated by SecKeyGeneratePair as a SecKeyRef object. Now, to send this, i need this KeyRef object to be in a string format.

How do i convert the SecKeyRef object to nsstring object?

回答1:

// you have SecKeyRef keyref from somewhere 
size_t keySize = SecKeyGetBlockSize(keyref);
NSData* keyData = [NSData dataWithBytes:keyref length:keySize];

Then use this NSData category to encode the NSData object with base64 to a NSString.

NSString *keyStringB64 = [keyData base64EncodedString];


回答2:

[Cleanup of old question]

Saving SecKeyRef device generated public/private key pair on disk

Shows how to create an NSData object. As I'm no iOS developer, I'll leave the conversion to a string (e.g. using base 64 encoding) as an excercise.