2012-11-04 44 views
0

如何编码密码字符串并将其保存到Mac上的objective-c文件中? 是这样的:编码密码objective-c mac

NSString *myPasswordString = [NSString stringWithFormat:@"mypassword"]; 

//Encode 

//Save to Preferences file 
+0

,不对其进行编码(加密无法逆转,编码可以)。我想最好的选择是使用'bcrypt' C库。 – Joost

+0

@Joost是'bcrypt' for objective-c mac可可吗? – atomikpanda

+2

@Joost:加密*可以颠倒;哈希不能。 – mipadi

回答

1
#import <CommonCrypto/CommonDigest.h> 
... 
const char *cStr = [myPasswordString UTF8String]; 
unsigned char result[16]; 
CC_MD5(cStr, strlen(cStr), result); // This is the md5 call 
NSString *passwordAfterEncrypt = [NSString stringWithFormat: 
@"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", 
result[0], result[1], result[2], result[3], 
result[4], result[5], result[6], result[7], 
result[8], result[9], result[10], result[11], 
result[12], result[13], result[14], result[15]]; 
+1

非常感谢!它会编码,但我怎么解码? – atomikpanda

+0

对于MD5,您无法解码。您可以编码密码用户输入并比较两个编码密码,以查看用户是否输入了正确的密码。 – sunkehappy

0

你可以写一个首选项文件如下:

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; 
[settings setObject : myPasswordString forKey : @"mypassword"]; 
[settings synchronize]; 

我不知道你所说的“编码”的意思。如果你的意思是 '加密' 有许多问题上的SO:

即使是如上所述,您仍然需要仔细考虑安全影响。如果您存储的密码是使用应用程序中嵌入的密钥加密的,那么攻击者可以恢复该密码。

2

存储密码的更好方法是使用钥匙串。

有类,使它更容易 - 你可能要加密的密码,如SSKeyChain在Github上

+0

尽管SSKeychain有缺陷。在当前版本下,它会从底层的Sec框架产生一个无效的用户名响应,并基本上与任何启用了断点的项目一起使用。 – CodaFi