0

无论如何使用从 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];

有没有办法做到这一点?还是我必须始终加密/解密文件?

4

1 回答 1

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];
于 2015-05-17T00:27:07.383 回答