我还使用您引用的相同包装器在应用程序长 Back 中实现了钥匙串,但是,当然进行了很多修改。
基本上钥匙串是相当安全的。根据苹果公司的说法,它是一个加密容器,保存多个应用程序的安全信息,这意味着当钥匙串被锁定时,没有人可以访问其受保护的内容。
在 iOS 中,只有创建钥匙串的应用程序才能访问它。
根据Apple的文档,iOS可以选择Memory-Cache或Disk Cache。
但从 iOS 4.xx++ 开始,它只是磁盘缓存(不知道为什么),因此总是创建一个
sqlite DB ,其中钥匙串中的所有数据都存储与特定标识符相对应。
Sqlite DB 可以在 root 或越狱设备上被黑客攻击。
确保钥匙串安全
1 添加安全关键字“kSecAttrAccessibleWhenUnlockedThisDeviceOnly
“同时添加或
更新方法中钥匙串中的数据“SecItemUpdate
“&”SecItemAdd
".
就像是 :-
- (void)writeToKeychain
{
NSDictionary *attributes = NULL;
NSMutableDictionary *updateItem = NULL;
OSStatus result;
if (SecItemCopyMatching((CFDictionaryRef)genericPasswordQuery, (CFTypeRef *)&attributes) == noErr)
{
updateItem = [NSMutableDictionary dictionaryWithDictionary:attributes];
[updateItem setObject:[genericPasswordQuery objectForKey:(id)kSecClass] forKey:(id)kSecClass];
NSMutableDictionary *tempCheck = [self dictionaryToSecItemFormat:keychainItemData];
[tempCheck removeObjectForKey:(id)kSecClass];
#if TARGET_IPHONE_SIMULATOR
[tempCheck removeObjectForKey:(id)kSecAttrAccessGroup];
#endif
[updateItem setObject:(id)kSecAttrAccessibleWhenUnlockedThisDeviceOnly forKey:(id)kSecAttrAccessible];
result = SecItemUpdate((CFDictionaryRef)updateItem, (CFDictionaryRef)tempCheck);
NSAssert( result == noErr, @"Couldn't update the Keychain Item." );
CFRelease(attributes);
}
else
{
[keychainItemData setObject:(id)kSecAttrAccessibleWhenUnlockedThisDeviceOnly forKey:(id)kSecAttrAccessible];
result = SecItemAdd((CFDictionaryRef)[self dictionaryToSecItemFormat:keychainItemData], NULL);
NSAssert( result == noErr, @"Couldn't add the Keychain Item." );
}
}
2 在添加到钥匙串之前对数据进行加密。我使用了 AES-128 加密。
还要确保用于加密的密钥是 RSA 密钥。(由 SSL Web 服务发送)。
注意:-钥匙串数据存储在/private/var/Keychains/keychain-2.db
iPhone 上的文件。
希望对您有帮助。