1

我以下面的代码为例。如果我错了,请帮助我。

- (void)storeToKeychain {

kPassword = [self computeSHA256DigestForString:[NSString stringWithFormat:@"%@%i%@", [username stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding], pinHash, SALT_HASH];


NSData *data = [@"Data" dataUsingEncoding:NSUTF8StringEncoding];
NSError *error;
NSData *encryptedData = [RNEncryptor encryptData:data
                                withSettings:kRNCryptorAES256Settings
                                      password:kPassword
                                         error:&error];
}

- (NSString*)computeSHA256DigestForString:(NSString*)input {

const char *cstr = [input cStringUsingEncoding:NSUTF8StringEncoding];
NSData *data = [NSData dataWithBytes:cstr length:input.length];
uint8_t digest[CC_SHA256_DIGEST_LENGTH];

// This is an iOS5-specific method.
// It takes in the data, how much data, and then output format, which in this case is an int array.
CC_SHA256(data.bytes, data.length, digest);

// Setup our Objective-C output.
NSMutableString* output = [NSMutableString stringWithCapacity:CC_SHA256_DIGEST_LENGTH * 2];

// Parse through the CC_SHA256 results (stored inside of digest[]).
for(int i = 0; i < CC_SHA256_DIGEST_LENGTH; i++) {
    [output appendFormat:@"%02x", digest[i]];
}

return output;
}

我的应用程序有一个设置和登录页面。每当用户创建帐户时,我希望将密码存储在加密的钥匙串中,我可能应该使用用户名来增加安全性。所以我想在用户创建帐户时使用RNCryptor在钥匙串中加密用户的用户名和密码。

我不确定kPassword在 RNEcryptor 方法中使用什么密码。我最初想使用由 组成的 SHA256 哈希UUID, username and a random 40 character salt hash将密码加密到钥匙串中。后来我找到了 RNCryptor,我想知道是否可以改用SHA256 hashaskPassword还是有其他选择?

4

1 回答 1

3

尝试这个

从https://github.com/Gurpartap/AESCrypt-ObjC添加 AESCrypt-ObjC-master

现在将这些添加到顶部

#import "AESCrypt.h"

然后添加这些

NSString *message = @"yourStringtobeencrypted";
NSString *password = @"yourpassword";
NSString *encryptedData = [AESCrypt encrypt:message password:password];

你去吧..解密

NSString *message1 = [AESCrypt decrypt:encryptedData password:password];

:-)

于 2015-02-04T06:14:48.913 回答