objective c - Import RSA keys to iPhone keychain? -


I have some NSString objects that represent RSA public-private keypayers (not generated by SecKeyCreatePair, but external crypto By the library). How do I create SecKeyRef objects (which are required for SecKeyDecrypt / Encrypt methods) from these NSString objects?

Do I need to import them first in key Sheehab? if so, how?

Thank you!

Then in the IOS, the keychain sandboxed, AFAIK means that whatever you want to bunch Putting it in is only accessible by your app and the app only unless you specify otherwise. In Project Settings, you must enable Keychain sharing under Capabilities .

Now that you are out of the way, you can definitely import the data since they are NSString objects, so you must first call it NSData The objects most likely to be changed to import correctly, they are encoded in base 64, so you have to reverse it:

  NSData * decodedData = [[ NSData alloc] initWithBase64EncodedString: base64String Option: 0];  

Now that you have done this, you can use this method to save your key to the keychain and get SecKeyRef:

  / ** * key: Data that you are importing * keySize: key length (512, 1024, 2048) * isPrivate: Is this a private key or public key? * / - (SecureFf) SaveKeToChen: (NSDTA *) Key Key Size: (NSUNTEGER) Keys Private: (BOL) is Private {Ossatatus Sanity Cheki = Noir; NSDT * tags; ID key class; If (IsPrivate) {tag = privateTag; KeyClass = (__ Bridge ID) kSecAttrKeyClassPrivate; } Other {tag = public tag; Main class = (__brid id) kSecAttrKeyClassPublic; } NSDictionary * saveDict = @ {(Bridge ID) kSecClass: (__Bridge ID) kSecClassKey, (__ Bridge ID) kSecAttrKeyType: (__bridge id) kSecAttrKeyTypeRSA, (__bridge id) kSecAttrApplicationTag: tag, (__bridge id) kSecAttrKeyClass: keyClass, ( __Bridge ID) KSecValueData: Key, (__Bridge ID) kSecAttrKeySizeInBits: [USSignedInteger: keySize with NSNumber number], (__Bridge ID) kSecAttrEffectiveKeySize: [USSignedInteger: keySize] with NSNumber number, (__Bridge ID) kSecAttrCanDerive: (__Bridge ID) kCFBooleanFalse , (__bridge id) kSecAttrCanEncrypt: (__Bridge ID) KCFBolienTre, (__Bridge ID) KSACAT ARTConfication: (__B (__Bridge ID) kSecAttrCanWrap: (__bridge id) KCFBoni True, (__Bridge ID) kSecAttrCanUnwrap (__Bridge ID) KCFBooleanFalse (__Bridge ID) kSecAttrCanSign: (__Bridge ID) kCFBooleanFalse, (__bridge id) kSecAttrCanWrap: (__bridge id) kSecAttrCanUnwrap : (__bridge id) kCFBooleanFalse}; SecKeyRef saved karf = null; Check the discretion = Add the command (__ bridge CFFFFRRF) SAVE, (CFTYPRF *) and saved carefraig; if (discretion check! = ErrSecSuccess) {LOGGING_FACILITY1 (check sanitie! = NOARR, @ "Trouble saving key key, OSSTATUS ==% D. ", SANITI CHECK);} Return saved CARP;}  

Later, if you want to recover SecKeyRef from Keychain, you can use it Are:

  - (SecKeyRef) getKeyRef: (BOOL) isP Rivate {OSStatus sanityCheck = noErr; NSDTA tags; ID key class; If (isPrivate) {if (privateKeyRef! = NULL) {// is already present in memory, return personal key;} tag = personal tag; KeyClass = ( __Bridge ID) kSecAttrKeyClassPrivate;} Else {if (already publicKeyRef! = NULL) {// is already present in the memory, return public care;} tag = public tag; main class = (__brid id) kSecAttrKeyClassPublic;} NSDictionary * queryDict = @ {{Bridge ID} kSecClass: (__ Bridge ID) kSecClassKey, (__ Bridge ID) kSecAttrKeyType: (__bridge id) kSecAttrKeyTypeRSA, (__bridge id) kSecAttrApplicationTag: t , (__bridge ID) kSecAttrKeyClass: keyClass, (__Bridge ID) KSecReturnRef: (__bridge ID) Kesifboni true}; SecKeyRef keyReference = NULL; Check discretion = SecItemCopyMatching ((__ Bridge CFDictionaryRef) queryDict, (CFTypeRef *) and key reference; If (check the discretion! = ErrSecSuccess) {NSLog (@ "Error in retrieving key from server. IsPrivate:% D. Check Vivek:% li", is private, discreet check); } If (Ipivate) {privateKeyRef = keyreference; } And {publicKeyRef = keyreference; } Return key reference; }  

Comments