Can I access the keychain on the iPhone?

后端 未结 7 1834
抹茶落季
抹茶落季 2020-11-28 21:55

This question discusses encrypting data on the iPhone using the crypt() function. As an alternative, is there a keychain on the iPhone and if so, what code would I use to ac

相关标签:
7条回答
  • 2020-11-28 22:01

    One other thing to note: the keychain APIs don't work in the simulator when using older versions (2.x, 3.x) of the iPhone SDK. This could save you a lot of frustration when testing!

    0 讨论(0)
  • 2020-11-28 22:09

    There is a keychain you can use - for code, the best bet is to check out the GenericKeychain sample application from Apple:

    GenericKeychain sample

    0 讨论(0)
  • 2020-11-28 22:10

    Here is what i use to store Key/Value pairs in the keychain. Make sure to add Security.framework to your project

    #import <Security/Security.h>
    
    // -------------------------------------------------------------------------
    -(NSString *)getSecureValueForKey:(NSString *)key {
        /*
    
         Return a value from the keychain
    
         */
    
        // Retrieve a value from the keychain
        NSDictionary *result;
        NSArray *keys = [[[NSArray alloc] initWithObjects: (NSString *) kSecClass, kSecAttrAccount, kSecReturnAttributes, nil] autorelease];
        NSArray *objects = [[[NSArray alloc] initWithObjects: (NSString *) kSecClassGenericPassword, key, kCFBooleanTrue, nil] autorelease];
        NSDictionary *query = [[NSDictionary alloc] initWithObjects: objects forKeys: keys];
    
        // Check if the value was found
        OSStatus status = SecItemCopyMatching((CFDictionaryRef) query, (CFTypeRef *) &result);
        [query release];
        if (status != noErr) {
            // Value not found
            return nil;
        } else {
            // Value was found so return it
            NSString *value = (NSString *) [result objectForKey: (NSString *) kSecAttrGeneric];
            return value;
        }
    }
    
    
    
    
    // -------------------------------------------------------------------------
    -(bool)storeSecureValue:(NSString *)value forKey:(NSString *)key {
        /*
    
         Store a value in the keychain
    
         */
    
        // Get the existing value for the key
        NSString *existingValue = [self getSecureValueForKey:key];
    
        // Check if a value already exists for this key
        OSStatus status;
        if (existingValue) {
            // Value already exists, so update it
            NSArray *keys = [[[NSArray alloc] initWithObjects: (NSString *) kSecClass, kSecAttrAccount, nil] autorelease];
            NSArray *objects = [[[NSArray alloc] initWithObjects: (NSString *) kSecClassGenericPassword, key, nil] autorelease];
            NSDictionary *query = [[[NSDictionary alloc] initWithObjects: objects forKeys: keys] autorelease];
            status = SecItemUpdate((CFDictionaryRef) query, (CFDictionaryRef) [NSDictionary dictionaryWithObject:value forKey: (NSString *) kSecAttrGeneric]);
        } else {
            // Value does not exist, so add it
            NSArray *keys = [[[NSArray alloc] initWithObjects: (NSString *) kSecClass, kSecAttrAccount, kSecAttrGeneric, nil] autorelease];
            NSArray *objects = [[[NSArray alloc] initWithObjects: (NSString *) kSecClassGenericPassword, key, value, nil] autorelease];
            NSDictionary *query = [[[NSDictionary alloc] initWithObjects: objects forKeys: keys] autorelease];
            status = SecItemAdd((CFDictionaryRef) query, NULL);
        }
    
        // Check if the value was stored
        if (status != noErr) {
            // Value was not stored
            return false;
        } else {
            // Value was stored
            return true;
        }
    }
    

    It is worth noting that these key/values will not get deleted if the user deletes your app. If a user deletes your app, then reinstalls it, the key/values will still be accessible.

    0 讨论(0)
  • 2020-11-28 22:10

    Also remember that when generating an AppID, if you want more than one application to access the same Keychain information, you have to generate a wild card AppID (#####.com.prefix.*)...

    0 讨论(0)
  • 2020-11-28 22:20

    I really like Buzz Anderson's Keychain abstraction layer and I eagerly await Jens Alfke's MYCrypto to reach a usable state. The latter does a competent job of allowing use on Mac OS X and the iPhone using the same code, though its features only mimic a small subset of the Keychain's.

    0 讨论(0)
  • 2020-11-28 22:20

    Here is one more good wrapper class from Mr.Granoff https://github.com/granoff/Lockbox Thanks

    0 讨论(0)
提交回复
热议问题