如何在从NSData转换后使用RNCryptor解密nsstring

时间:2015-05-16 23:34:08

标签: ios objective-c encryption nsstring rncryptor

无论如何使用已从NSData转换的RNcryptor解密NSString?我一直收到未知的标题错误。这是我正在做的加密:

    NSData *data = [@"FeedMeMorePizzaPlease" dataUsingEncoding:NSUTF8StringEncoding];
    NSData *encryptedData = [RNEncryptor encryptData:data
                                  withSettings:kRNCryptorAES256Settings
                                        password:@"pizzaHutIsTheWorst"
                                           error:&error];
    NSString *encString = [encryptedData base64EncodedStringWithOptions:0];

然后我接受encString,将其放在我服务器上的文本文件中,然后:

    NSURL  *gUrl = [NSURL URLWithString:@"https://myurlissupersecret.com/cheese.txt"];
    NSString *sillyString = [NSString stringWithContentsOfURL:gUrl encoding:(NSUTF8StringEncoding) error:nil];
    NSData *blindData  = [sillyString dataUsingEncoding:0];
    NSData *decryptedData = [RNDecryptor decryptData:blindData withSettings:kRNCryptorAES256Settings password:@"pizzaHutIsTheWorst" error:&error];

有没有这样做?或者我是否必须始终加密/解密文件?

1 个答案:

答案 0 :(得分:1)

你错过了解密的一步。 sillyString是base64编码的字符串。您需要将base64编码的字符串转换为NSData。以下是您需要解密的更新代码:

NSURL  *gUrl = [NSURL URLWithString:@"https://myurlissupersecret.com/cheese.txt"];
NSString *encString = [NSString stringWithContentsOfURL:gUrl encoding:NSUTF8StringEncoding error:nil];
NSData *encryptedData = [[NSData alloc] initWithBase64EncodedString:encString options:0];
NSData *decryptedData = [RNDecryptor decryptData:encryptedData withSettings:kRNCryptorAES256Settings password:@"pizzaHutIsTheWorst" error:&error];