我在加密时遇到了问题。
服务器发送的JSON数据是经过AES256加密并进行Base64编码后的。 在iOS客户端中,我能够获取响应并使用Base64进行解码。 AES256解密可用于某些库(第三方或CommonCryptor.h包装器),但在另一个库中无法工作。 当解密有效时,解析不起作用。
以下是包装器库及其相应的代码。
RNCryptor
(https://github.com/rnapier/RNCryptor)
NSData *decodedData = [Util decode:data];
NSData *RNDecryptedData = [RNDecryptor decryptData:decodedData withPassword:randomString error:&error];
if (error == nil) {
NSLog(@"RNDecryptedData - %@",[Util hexStringFromData:RNDecryptedData]);
response = [NSJSONSerialization JSONObjectWithData:RNDecryptedData options:NSJSONReadingMutableContainers error:&error];
NSLog(@"response - %@",response);
NSLog(@"error1 - %@",error);
} else
NSLog(@"error2 - %@",error);
我在解密时遇到了以下错误。
EncryptedParsing[4402:70b] error2 - Error Domain=net.robnapier.RNCryptManager Code=2 "Unknown header" UserInfo=0x8c6bd60 {NSLocalizedDescription=Unknown header}
CCrypto
(https://github.com/Gurpartap/AESCrypt-ObjC)
NSData *decodedData = [Util decode:data];
NSData *CCDecryptedData = [decodedData decryptedAES256DataUsingKey:randomString error:&error];
if (error == nil) {
response = [NSJSONSerialization JSONObjectWithData:CCDecryptedData options:NSJSONReadingMutableContainers error:&error];
NSLog(@"response - %@",response);
NSLog(@"error1 - %@",error);
} else
NSLog(@"error2 - %@",error);
我这里获取到了解密后的数据,但在解析时出现了以下错误:
EncryptedParsing[4469:70b] error1 - Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x8a51520 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}
NSData+AES256
NSData *decodedData = [Util decode:data];
NSData *AES256DecryptedData = [decodedData AES256DecryptWithKey:randomString];
response = [NSJSONSerialization JSONObjectWithData:AES256DecryptedData options:NSJSONReadingMutableLeaves error:&error];
NSLog(@"error - %@",error);
我正在获取解密数据,但在解析时出现以下错误
EncryptedParsing[4646:70b] error - Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x8a710c0 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}
除此之外,我还使用了CocoaSecurity (https://github.com/kelp404/CocoaSecurity),但它并没有起作用。
我正在使用NSData+Base64 (https://github.com/l4u/NSData-Base64) 进行base64解码。
顺便说一下,从服务器端来看并没有问题(我们已经测试过了)。
我想知道我做错了什么导致出现错误。或者是否有其他方法可以实现它。