【发布时间】:2014-01-01 06:26:06
【问题描述】:
我在加密时遇到问题。
服务器正在发送经过 aes256 加密然后 base64 编码的 json 数据。 而在 ios 客户端我能够获得响应并使用 base64 对其进行解码。 AES256 解密适用于某些库(第 3 方或 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 解码
顺便说一句,服务器端没有问题(我们测试过)。
我想知道我正在做的错误。或者有没有其他的方法来实现它
【问题讨论】:
标签: ios json encryption base64 aes