简体   繁体   English

我可以访问iPhone上的钥匙串吗?

[英]Can I access the keychain on the iPhone?

This question discusses encrypting data on the iPhone using the crypt() function. 这个问题讨论使用crypt()函数在iPhone上加密数据 As an alternative, is there a keychain on the iPhone and if so, what code would I use to access it in order to store login details and then retrieve them for us in an application? 作为替代方案,iPhone上是否有钥匙串,如果有,我会使用什么代码来存储登录详细信息,然后在应用程序中为我们检索它们?

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. 另外需要注意的一点是:当使用iPhone SDK的旧版本(2.x,3.x)时,钥匙串API在模拟器中不起作用。 This could save you a lot of frustration when testing! 在测试时,这可以为您节省很多挫折!

There is a keychain you can use - for code, the best bet is to check out the GenericKeychain sample application from Apple: 您可以使用钥匙链 - 对于代码,最好的办法是查看Apple的GenericKeychain示例应用程序:

GenericKeychain sample GenericKeychain示例

I really like Buzz Anderson's Keychain abstraction layer and I eagerly await Jens Alfke's MYCrypto to reach a usable state. 我非常喜欢Buzz Anderson的Keychain抽象层 ,我热切期待着Jens Alfke的MYCrypto达到一个可用的状态。 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. 后者可以使用相同的代码在Mac OS X和iPhone上使用,但其功能仅模仿Keychain的一小部分。

Here is what i use to store Key/Value pairs in the keychain. 这是我用来存储钥匙串中的键/值对。 Make sure to add Security.framework to your project 确保将Security.framework添加到项目中

#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. 如果用户删除了您的应用,然后重新安装,则仍可以访问键/值。

还要记住,在生成AppID时,如果您希望多个应用程序访问相同的Keychain信息,则必须生成通配符AppID(#####。com.prefix。*)...

With the latest version 1.2 of the GenericKeychain sample Apple provides a keychain wrapper that also runs on the iPhone Simulator. 使用GenericKeychain样本的最新版本1.2,Apple提供了一个也可以在iPhone模拟器上运行的钥匙串包装器。 Check out at this article for details: http://dev-metal.blogspot.com/2010/08/howto-use-keychain-in-iphone-sdk-to.html 有关详细信息,请参阅此文章: http//dev-metal.blogspot.com/2010/08/howto-use-keychain-in-iphone-sdk-to.html

这是Mr.Granoff的一个更好的包装类https://github.com/granoff/Lockbox谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM