代码之家  ›  专栏  ›  技术社区  ›  davidmytton

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

  •  29
  • davidmytton  · 技术社区  · 17 年前

    This question discusses encrypting data on the iPhone 使用crypt()函数。作为替代方案,iPhone上是否有钥匙链?如果有,我将使用什么代码来访问它,以便存储登录详细信息,然后在应用程序中为我们检索它们?

    7 回复  |  直到 9 年前
        1
  •  45
  •   Brad The App Guy    15 年前

    还有一点需要注意:当使用旧版本(2.x,3.x)的iPhone SDK时,钥匙链API在模拟器中不起作用。这可以在测试时为您节省很多沮丧!

        2
  •  34
  •   GeneralMike    12 年前

    你可以使用一个钥匙链——对于代码,最好的办法是查看苹果公司的GenericKeychain示例应用程序:

    GenericKeychain sample

        3
  •  8
  •   bbrown    17 年前

    我真的很喜欢 Buzz Anderson's Keychain abstraction layer 我热切地等待着 Jens Alfke's MYCrypto 以达到可用状态。后者在允许在Mac OS X和iPhone上使用相同的代码方面做得很好,尽管它的功能只模仿了Keychain的一小部分。

        4
  •  8
  •   AlBeebe    13 年前

    以下是我在钥匙链中存储键/值对的方法。确保将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;
        }
    }
    

    值得注意的是,如果用户删除您的应用程序,这些键/值将不会被删除。如果用户删除了您的应用程序,然后重新安装,则键/值仍然可以访问。

        5
  •  5
  •   whoisjake    17 年前

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

        6
  •  4
  •   joobik    16 年前

    在GenericKeychain示例的最新版本1.2中,苹果提供了一个也在iPhone模拟器上运行的钥匙链包装器。查看这篇文章了解详细信息: http://dev-metal.blogspot.com/2010/08/howto-use-keychain-in-iphone-sdk-to.html

        7
  •  0
  •   Bhavin Kansagara    12 年前

    这是Granoff先生提供的另一个很好的包装类 https://github.com/granoff/Lockbox 谢谢