Search code examples
iosrsakeychain

How to save RSA publicKey/privateKey NOT in Keychain


i have a question about saving Keys within iOS applications.

Is there any way to save the public/private Key of an RSA-certificate within an app, not to save/store this in the iOS keychain. Maybe someone tried this out before?

I found something similar with username and password. Its called "Keychainwrapper". There the app saves the login credentials within the app. Does this work for me with private/public keys?

Hopefully someone can help me.

Best regards,

Andi


Solution

  • Since you have to store only public and private key and not the whole certificate, can not use the internal keychain and the keys are generated on the device it would be sufficient if you save your public and private key in the NSUserDefaults:

    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    //saving the data
    [prefs setObject"<your public key data>" forKey:@"PublicKey"];
    [prefs setObject"<your private key data>" forKey:@"PrivateKey"];
    
    //reading the data
    NSString *publicKey = [prefs stringForKey:@"PublicKey"];
    NSString *privateKey = [prefs stringForKey:@"PrivateKey"];
    

    This store is rather insecure, so I would suggest that you encrypt your data like this: iOS 5: Data encryption AES-256 EncryptWithKey: not found. Be aware, that symmetric encryption is not secure if you store the key in your application! You should use some user input to generate the key.

    Hope this helps.