gpt4 book ai didi

iOS 钥匙串(keychain) :strange behavior while setting and getting data in/out the keychain

转载 作者:行者123 更新时间:2023-11-29 03:26:05 27 4
gpt4 key购买 nike

这一切都发生在我的模拟器中。在练习使用钥匙串(keychain)包装时,我发现自己处于一种奇怪的情况。我试图学习如何从钥匙串(keychain)插入和检索数据,为了做到这一点,我开始了一个新项目,其中我所做的唯一一件事就是在 “(void)viewDidLoad” 中写入我的 ViewController 方法默认用户名,默认密码,存储并尝试在我的应用程序第二次运行时检索它们.

这本来应该是小菜一碟,但我发现我的应用程序崩溃了。这是我使用的代码:

- (void)viewDidLoad{

[super viewDidLoad];

//Keychain wrapper definition
KeychainItemWrapper *keychainItem = [[KeychainItemWrapper alloc] initWithIdentifier:@"YourAppLogin" accessGroup:nil];

//Getting DATA:
NSLog(@"Retrieving User DATA");
password = [keychainItem objectForKey:(__bridge id)(kSecValueData)];
username = [keychainItem objectForKey:(__bridge id)(kSecAttrAccount)];
NSLog(@"pwd = %@",password);
NSLog(@"user = %@",username);

//Setting DATA:
[keychainItem setObject:@"password you are saving" forKey:(__bridge id)(kSecValueData)];
[keychainItem setObject:@"username you are saving" forKey:(__bridge id)(kSecAttrAccount)];
}

Xcode 给了我一个未捕获的 NSInternalInconsistencyException 。玩弄代码,我意识到只有在设置新密码时(第一次运行我的应用程序或重置钥匙串(keychain)后)才会出现问题,并且奇怪的部分来了,只有当我在用户名之前设置了密码。因此重写代码交换

//Setting DATA
[keychainItem setObject:@"username you are saving" forKey:(__bridge id)(kSecAttrAccount)];
[keychainItem setObject:@"password you are saving" forKey:(__bridge id)(kSecValueData)];
}

解决了我的问题。谁能解释一下为什么?

最后,我想问一下,在第二次运行我的应用程序时检索我的密码是否会得到它的加密版本。这是因为我尝试通过两种方式获取密码:

1)存储后我就得到了

//saving DATA
[keychainItem setObject:@"username you are saving" forKey:(__bridge id)(kSecAttrAccount)];
[keychainItem setObject:@"password you are saving" forKey:(__bridge id)(kSecValueData)];

//getting DATA:
username = [keychainItem objectForKey:(__bridge id)(kSecAttrAccount)];
password = [keychainItem objectForKey:(__bridge id)(kSecValueData)];
NSLog(@"\nUser: %@ \nPassword: %@",username,password);

结果是

User: username you are saving
Password: password you are saving

2)我在第二次运行中获得了密码,将设置部分放在评论下

//saving DATA
//[keychainItem setObject:@"username you are saving" forKey:(__bridge id)(kSecAttrAccount)];
//[keychainItem setObject:@"password you are saving" forKey:(__bridge id)(kSecValueData)];

//getting DATA:
username = [keychainItem objectForKey:(__bridge id)(kSecAttrAccount)];
password = [keychainItem objectForKey:(__bridge id)(kSecValueData)];
NSLog(@"\nUser: %@ \nPassword: %@",username,password);

结果是

User: username you are saving
Password: <70617373 776f7264 20796f75 20617265 20736176 696e67>

这是我的密码加密版本还是什么???

谢谢

最佳答案

我不明白为什么在 用户名 之前将 密码 放入钥匙串(keychain)中会使应用程序崩溃,但我了解到,为了获得 钥匙串(keychain)返回密码,您必须首先将其存储在NSData中,您应该这样做:

NSData *pwd = [keychainItem objectForKey:(__bridge id)(kSecValueData)]; 
password = [[NSString alloc] initWithData:pwd encoding:NSUTF8StringEncoding];

关于iOS 钥匙串(keychain) :strange behavior while setting and getting data in/out the keychain,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20442847/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com