【问题标题】:Working with AES encryption in IOS在 IOS 中使用 AES 加密
【发布时间】: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

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

顺便说一句,服务器端没有问题(我们测试过)。

我想知道我正在做的错误。或者有没有其他的方法来实现它

【问题讨论】:

    标签: ios json encryption base64 aes


    【解决方案1】:

    没有对 AES 加密数据进行编码的通用标准方法。您需要双方就如何编码所需的各种元数据达成一致。

    RNCryptor 未能解密,因为它希望数据位于 RNCryptor format 中。

    CCCrypto 和 AES256DecryptWithKey 对密钥的解释可能与您的预期不同。两者都无法验证您传递的密钥是否正确,因此您可能会收到垃圾。如果是这种情况,我希望对于某些消息,您会收到垃圾邮件,而对于其他消息,您什么也得不到,或者是错误。

    如果您需要跨平台解决方案,RNCryptor 有多种语言的实现。如果您需要符合服务器格式(从代码的外观来看,这可能不安全加密),那么您需要提供服务器代码以确定正确的客户端代码。

    【讨论】:

      【解决方案2】:

      尝试用 kNilOptions 替换 NSJSONReadingMutableLeaves。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-06-25
        • 1970-01-01
        • 1970-01-01
        • 2015-11-01
        • 2014-07-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多