如何編碼密碼字符串並將其保存到Mac上的objective-c文件中? 是這樣的:編碼密碼objective-c mac
NSString *myPasswordString = [NSString stringWithFormat:@"mypassword"];
//Encode
//Save to Preferences file
如何編碼密碼字符串並將其保存到Mac上的objective-c文件中? 是這樣的:編碼密碼objective-c mac
NSString *myPasswordString = [NSString stringWithFormat:@"mypassword"];
//Encode
//Save to Preferences file
#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]];
非常感謝!它會編碼,但我怎麼解碼? – atomikpanda
對於MD5,您無法解碼。您可以編碼密碼用戶輸入並比較兩個編碼密碼,以查看用戶是否輸入了正確的密碼。 – sunkehappy
你可以寫一個首選項文件如下:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[settings setObject : myPasswordString forKey : @"mypassword"];
[settings synchronize];
我不知道你所說的「編碼」的意思。如果你的意思是 '加密' 有許多問題上的SO:
即使是如上所述,您仍然需要仔細考慮安全影響。如果您存儲的密碼是使用應用程序中嵌入的密鑰加密的,那麼攻擊者可以恢復該密碼。
存儲密碼的更好方法是使用鑰匙串。
有類,使它更容易 - 你可能要加密的密碼,如SSKeyChain在Github上
儘管SSKeychain有缺陷。在當前版本下,它會從底層的Sec框架產生一個無效的用戶名響應,並基本上與任何啓用了斷點的項目一起使用。 – CodaFi
,不對其進行編碼(加密無法逆轉,編碼可以)。我想最好的選擇是使用'bcrypt' C庫。 – Joost
@Joost是'bcrypt' for objective-c mac可可嗎? – atomikpanda
@Joost:加密*可以顛倒;哈希不能。 – mipadi