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

How to decrypt nsstring using RNCryptor after conversion from NSData

是否有使用从 NSData 转换而来的 RNcryptor 解密 NSString 的方法?我不断收到未知的 header 错误。这是我正在做的加密操作:

    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];

有办法吗?还是我必须始终加密/解密文件?

你错过了解密的一个步骤。 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];