簡體   English   中英

我可以訪問iPhone上的鑰匙串嗎?

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

這個問題討論使用crypt()函數在iPhone上加密數據 作為替代方案,iPhone上是否有鑰匙串,如果有,我會使用什么代碼來存儲登錄詳細信息,然后在應用程序中為我們檢索它們?

另外需要注意的一點是:當使用iPhone SDK的舊版本(2.x,3.x)時,鑰匙串API在模擬器中不起作用。 在測試時,這可以為您節省很多挫折!

您可以使用鑰匙鏈 - 對於代碼,最好的辦法是查看Apple的GenericKeychain示例應用程序:

GenericKeychain示例

我非常喜歡Buzz Anderson的Keychain抽象層 ,我熱切期待着Jens Alfke的MYCrypto達到一個可用的狀態。 后者可以使用相同的代碼在Mac OS X和iPhone上使用,但其功能僅模仿Keychain的一小部分。

這是我用來存儲鑰匙串中的鍵/值對。 確保將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;
    }
}

值得注意的是,如果用戶刪除您的應用,則不會刪除這些鍵/值。 如果用戶刪除了您的應用,然后重新安裝,則仍可以訪問鍵/值。

還要記住,在生成AppID時,如果您希望多個應用程序訪問相同的Keychain信息,則必須生成通配符AppID(#####。com.prefix。*)...

使用GenericKeychain樣本的最新版本1.2,Apple提供了一個也可以在iPhone模擬器上運行的鑰匙串包裝器。 有關詳細信息,請參閱此文章: 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