2016-12-07 68 views
1

我需要在我的应用程序中实现离线登录。目前,我正在密钥链中存储密码,至少在应用程序在线时用于登录至少一次。现在我不检查用户名密码组合。如果我有一个设备的多个用户,只存储密码是不够的。那么你们谁都可以提出一些可以在没有安全漏洞的情况下完成的事情。我们如何以加密格式在钥匙串中存储用户名 - 密码组合

+0

待办事项阴要记住所有用户或刚刚过去的一个? – CZ54

+0

@bobby:需要记住所有的用户 – iOSManiac

+2

所以我建议你存储密码,使用登录作为密钥。如:[email protected] /密码。您可以编码密码的md5值以提高安全性 – CZ54

回答

1

我建议你存储的密码,使用登录的一个关键。如:[email protected]/password

您可以编码密码的md5值来提高安全性也

0

可以使用NSURLCredential依靠这个link

商店

NSURLCredential *credential; 

credential = [NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistencePermanent]; 
[[NSURLCredentialStorage sharedCredentialStorage] setCredential:credential forProtectionSpace:self.loginProtectionSpace]; 

获取存储数据

NSURLCredential *credential; 
NSDictionary *credentials; 

credentials = [[NSURLCredentialStorage sharedCredentialStorage] credentialsForProtectionSpace:self.loginProtectionSpace]; 
credential = [credentials.objectEnumerator nextObject]; 
NSLog(@"User %@ already connected with password %@", credential.user, credential.password); 
0

您可以将其保存在设计,节省设备钥匙扣敏感信息。从这个Ray Wenderlich tutorial与SHA512

#import "KeychainWrapper.h" 
#include <CommonCrypto/CommonDigest.h> 

-(void)createSHA512andSaveToKeychain:(NSString*)unencryptedPasswd { 
    const char *passwdBytes= [unencryptedPasswd cStringUsingEncoding:NSUTF8StringEncoding]; 
    NSData *passwordData = [NSData dataWithBytes:passwdBytes length:unencryptedPasswd.length]; 
    uint8_t digest[CC_SHA512_DIGEST_LENGTH]; 
    CC_SHA512(passwordData.bytes, passwordData.length, digest); 
    NSMutableString *encryptedPasswd= [NSMutableString stringWithCapacity:CC_SHA512_DIGEST_LENGTH * 2]; 
    for(int i = 0; i < CC_SHA512_DIGEST_LENGTH; i++) { 
     [encryptedPasswd appendFormat:@"%02x", digest[i]]; 
    } 

    // Save the password in the device keychain 
    KeychainWrapper *keychainWrapper = [[KeychainWrapper alloc] init]; 
    [keychainWrapper mySetObject:encryptedPasswd forKey:(__bridge id)kSecValueData]; 
    [keychainWrapper writeToKeychain]; 
} 

下载的包装和加密密码找回密码:

// Retrieve the pwd from the device keychain 
KeychainWrapper *keychainWrapper = [[KeychainWrapper alloc] init]; 
NSString *pwd = [keychainWrapper myObjectForKey:@"v_Data"]; 
+0

我们可以将检索到的密码与正确的用户名进行匹配吗?我有同一设备的多个用户 – iOSManiac

+0

是的。如果您需要存储多个密码(和用户名),则需要Apple提供不同的包装。请参阅此链接:http://devmonologue.com/ios/ios/storing-sensitive-information-in-the-iphones-keychain/基本上,您使用“initWithIdentifier:”方法使用特定于用户的初始化程序来初始化Keychain类。 – Alex

相关问题