在iOS中使用AES加密

3

我在加密时遇到了问题。

服务器发送的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

(http://pastie.org/426530)

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解码。

顺便说一下,从服务器端来看并没有问题(我们已经测试过了)。

我想知道我做错了什么导致出现错误。或者是否有其他方法可以实现它。

2个回答

1
没有通用的标准方法来编码AES加密数据。您需要双方就编码所需的各种元数据达成协议。
RNCryptor无法解密,因为它期望数据采用RNCryptor格式
CCCrypto和AES256DecryptWithKey可能会以与您意图不同的方式解释密钥。两者都不验证您传递的密钥是否正确,因此您可能会得到垃圾数据。如果是这种情况,我预计您会收到一些消息的垃圾数据,而对于其他消息,则不会收到任何响应或出现错误。 RNCryptor在多种语言中都有实现,如果您需要跨平台解决方案。如果您需要符合服务器格式(从您的代码看,这可能不是安全的加密),则需要提供服务器代码才能确定正确的客户端代码。

0

尝试将NSJSONReadingMutableLeaves替换为kNilOptions。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接