RSA encryption using a external generated public key on iPhone
<p>I want to encrypt my data using the public key provided by the server.
I am using SecKeyWrapper file to add the public key to keychain and get the SecKeyRef object.
I am calling the addPeerPublicKey method with following parameters,</p>

<pre><code>NSData* data=[@"YTozOntpOjA7czoxMjg6IrfSfkjXC//JhZIQzuZyi0kN99TC2C/hTIL1k096GIrvFtR/hbAd5WbCxo98rfBhGC6nsWYmEePjmJj83VXESbJ+z3C0JKknibO+Wm/imoFmjI5UxcZAGmyMx4O4TCZfpuPLrz1d9exb8pcy3ENQ6vdfCdu6wqHoYmPSlpe7GmWCIjtpOjE7czozOiIBAAEiO2k6MjtzOjY6InB1YmxpYyI7fQ==" dataUsingEncoding:NSUTF8StringEncoding];

//Remove the 1st available key
[[SecKeyWrapper sharedWrapper] removePeerPublicKey:@"XYZ"];

oPublicKey = [[SecKeyWrapper sharedWrapper] addPeerPublicKey:@"XYZ" 
                                                       keyBits:data];
</code></pre>

<p>/<strong><em>*</em>**<em>*</em>**<em>*</em>**<em>*</em>**<em>*</em>**<em>*</em>**<em>*</em>**<em>*</em>**<em>*</em>**<em>*</em>**<em>*</em>**<em>*</em>**<em>*</em>**<em>*</em>**<em>*</em></strong>/</p>

<pre><code>- (SecKeyRef)addPeerPublicKey:(NSString *)peerName keyBits:(NSData *)publicKey

    [peerPublicKeyAttr setObject:(id)kSecClassKey forKey:(id)kSecClass];
    [peerPublicKeyAttr setObject:(id)kSecAttrKeyTypeRSA forKey:(id)kSecAttrKeyType];
    [peerPublicKeyAttr setObject:peerTag forKey:(id)kSecAttrApplicationTag];
    [peerPublicKeyAttr setObject:publicKey forKey:(id)kSecValueData];
    [peerPublicKeyAttr setObject:[NSNumber numberWithBool:YES] forKey:(id)kSecReturnPersistentRef];

    sanityCheck = SecItemAdd((CFDictionaryRef) peerPublicKeyAttr, (CFTypeRef *)&amp;persistPeer);
</code></pre>

<p><code>SecItemAdd</code> return the <code>sanityCheck = 0</code> that is noerror.</p>

<p>But </p>

<pre><code>[self getKeyRefWithPersistentKeyRef:persistPeer];
</code></pre>

<p>return the nil and so its not working for me.</p>

<p>Please let me know i am missing something.</p>

<p>Thanks,
K</p>
