我必须对NSDictionary
进行加密和加密,然后在另一种方法中解密和解密。我已按照Securely storing keys in iOS Application 的说明进行操作,但表示我未使用PLIST文件来填充NSDictionary
值,但是{{ 1}}在程序中输入的值,我执行此定位 OSX
该计划可分为三个部分:
NSString
NSString
我的问题出在最后一次解密中。解密工作正常,但解密似乎产生NSString
,无法转换为原始NSData
我为加密所做的是:
NSDictionary
NSDictionary
将其转换为NSData
并格式化NSPropertyListSerialization
NSPropertyListXMLFormat_v1_0
的 base64 表示作为NSData
这部分的代码是:
NSString
该过程的第二部分是在数据字符中对 base64 中的密钥进行加密,我是这样做的:
+(NSString *)encrypt:(NSString *)firstValue secondValue:(NSString *)secondValue andKey:(NSString *)key;
{
NSDictionary *dictionary = [[NSDictionary alloc] initWithObjects:[[NSArray alloc] initWithObjects:firstValue, secondValue, nil] forKeys:[[NSArray alloc] initWithObjects:@"first", @"second", nil]];
NSData *data = [NSPropertyListSerialization dataFromPropertyList:dictionary format:NSPropertyListXMLFormat_v1_0 errorDescription:nil];
NSData *encryptData = [RNEncryptor encryptData:data withSettings:kRNCryptorAES256Settings password:key error:nil];
return [encryptData base64EncodedString];
}
NSData
-(NSData *)dataUsingEncoding:
转换为base64encoded NSData
NSString
的{{1}}插入char
NSString
返回NSMutadedString
以便稍后解密以下是争夺部分的代码:
NSString
第三部分包括测试我是否可以将其解密并解密为原始NSMutadedString
和+(NSString *)scrambleStrings:(NSString *)firstValue secondValue:(NSString *)secondValue andKey:(NSString *)key;
{
NSData *keyData = [key dataUsingEncoding:NSUTF8StringEncoding];
key = [keyData base64EncodedString];
// String from the first method
NSString *encrypt = [self encrypt:firstValue secondValue:secondValue andKey:key];
NSArray *myOrder = [self mySortAlgorithm:key];
NSMutableString *mutableEncrypt = [NSMutableString stringWithString:encrypt];
for(int i=0; i<[myOrder count];i++)
{
unichar c = [key characterAtIndex:i];
int index = [[myOrder objectAtIndex:i] intValue];
[mutableEncrypt insertString:[NSString stringWithCharacters:&c length:1] atIndex:index];
}
return [NSString stringWithString:mutableEncrypt];
}
密钥。它基本上包括:
NSDictionary
NSString
NSString
分开
NSData
解密以前的数据
NSString
RNCryptor
NSDictionary
值
以下是最后一部分的代码:
NSDictionary
问题是,最后NSLog
输出+(BOOL)testScrambleWithFirstValue:(NSString *)firstValue secondValue:(NSString *)secondValue andKey:(NSString *)key;
{
NSString *scrambledAndEncryptedKeys = [self scrambleFirstValue:firstValue secondValue:SecondValue andKey:key];
NSArray *myOrder = [self mySortAlgorithm:key];
NSMutableString *encryptedDictionary = [[NSMutableString alloc] init];
NSMutableString *encryptedKey = [[NSMutableString alloc] init];
for (int i=0; i<scrambledAndEncryptedKeys.length; i++) {
char c = [scrambledAndEncryptedKeys characterAtIndex:i];
if ([myOrder doesContain:[NSNumber numberWithInt:i]]) {
[encryptedKey appendFormat:@"%c", c];
} else {
[encryptedDictionary appendFormat:@"%c",c];
}
}
NSString *decryptedKey = [[NSString alloc] initWithData:[NSData dataFromBase64String:[NSString stringWithString:encryptedKey]] encoding:NSUTF8StringEncoding];
NSString *base64DataStr = [[NSString alloc] initWithData:[NSData dataFromBase64String:[NSString stringWithString:encryptedDictionary]] encoding:NSUTF8StringEncoding];
NSData *decryptedData = [RNDecryptor decryptData:[base64DataStr dataUsingEncoding:NSUTF8StringEncoding] withPassword:decryptedKey error:nil];
NSPropertyListFormat xmlFormat;
NSDictionary *decryptedDictionary = [NSPropertyListSerialization propertyListWithData:decryptedData options:NSPropertyListImmutable format:&xmlFormat error:nil];
//Outputs NULL
NSLog(@"decryptedDictionary:%@", decryptedDictionary);
//Just to simplify I'm returning YES here
return YES;
}
值。这让我觉得NSLog
没有被解密。