不恢复 iOS 钥匙串中插入的密码问题
我正在为 iPad2 开发一个应用程序,需要在钥匙串中写入一些项目,但我不希望它在我插入的每台计算机中进行复制,从而对设备进行备份/恢复。我使用 kSecAttrAccessible 键来选择我想要的可访问性类型,并使用 kSecAttrAccessibleWhenUnlockedThisDeviceOnly 值来确保如果我对设备中的所有内容进行备份,则钥匙串不会出现在该备份中。
所以我以这种方式进行:我重置钥匙串,在钥匙串中插入一个项目并转储钥匙串的所有内容,所以我看到该项目在那里。然后我对 iPad 进行备份。我重置了钥匙串并恢复了备份,因此只要恢复过程不处理钥匙串,钥匙串中就不应该有任何钥匙。下次运行该应用程序时,我会转储钥匙串的内容,并且密钥就在那里,因此它无法正常工作。我正在使用 iphone-lib (http://code.google.com/p/iphone-lib/) 转储和重置 iPad 中的凭据。我的SDK版本是4.3。
我用来在钥匙串中插入项目的代码如下:
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
NSData* identifier = [@"mypassword" dataUsingEncoding: NSASCIIStringEncoding];
[dic setObject:(id)kSecAttrAccessibleWhenUnlockedThisDeviceOnly forKey:(id)kSecAttrAccessible];
[dic setObject:identifier forKey:(id)kSecAttrGeneric];
[dic setObject:@"myaccount" forKey:(id)kSecAttrAccount];
[dic setObject:@"myservice" forKey:(id)kSecAttrService];
[dic setObject:(id)kSecClassGenericPassword forKey:(id)kSecClass];
[dic setObject:identifier forKey:(id)kSecValueData];
OSStatus error = SecItemAdd((CFDictionaryRef)dic, NULL);
谢谢!
I'm developing an application for an iPad2 that needs to write some items in Keychain but I don't want it replicates in every computer I plug, doing a backup/restore of the device. I'm using kSecAttrAccessible key to select the kind of accesibility I want with kSecAttrAccessibleWhenUnlockedThisDeviceOnly value to be sure that if I do a backup of all things that are in the device, the Keychain is not going to be present in that backup.
So I proceed in this way: I reset the Keychain, insert a item in Keychain and dump all the content of Keychain, so I see that the item is there. Then I do a backup of the iPad. I reset the Keychain and restore the backup so no key should be in the Keychain as long as the restore procedure doesn't deal with the Keychain. Next time I run the application, I dump the contents of the Keychain and the key is there, so it's not working as it should. I'm using iphone-lib (http://code.google.com/p/iphone-lib/) to dump and reset credentials in my iPad. My SDK version is 4.3.
The code I use to insert the item in the Keychain is the following:
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
NSData* identifier = [@"mypassword" dataUsingEncoding: NSASCIIStringEncoding];
[dic setObject:(id)kSecAttrAccessibleWhenUnlockedThisDeviceOnly forKey:(id)kSecAttrAccessible];
[dic setObject:identifier forKey:(id)kSecAttrGeneric];
[dic setObject:@"myaccount" forKey:(id)kSecAttrAccount];
[dic setObject:@"myservice" forKey:(id)kSecAttrService];
[dic setObject:(id)kSecClassGenericPassword forKey:(id)kSecClass];
[dic setObject:identifier forKey:(id)kSecValueData];
OSStatus error = SecItemAdd((CFDictionaryRef)dic, NULL);
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Apple 有两个很酷的示例(带有工作示例代码),帮助我了解钥匙串服务在 iOS 上的工作原理。我建议您查看它们,并希望它们能帮助您解决您的问题:
There two cool examples (with working sample code) from Apple, that helped me to understand how keychain service works on iOS. I suggest you to look at them, and hope they will help you to resolve your issue:
kSecAttrAccessibleWhenUnlockedThisDeviceOnly 可能是原因。
你可以尝试别的吗?
例如。 kSecAttrAccessibleWhenUnlocked
kSecAttrAccessibleWhenUnlockedThisDeviceOnly maybe the reason.
Can you try something else?
eg. kSecAttrAccessibleWhenUnlocked