iOS开发中AES加密浅解

作者: 天蓝色的海岸 | 来源:发表于2016-09-22 15:15 被阅读135次

    前段时间做了AES256加密解密,怕过段时间忘了,现在赶紧过来补一下。

    1.先来说说解密,因为我们项目后台人员做的加密,他返回的数据是加密的数据,所以我主要做了解密。

    NSData *base = [[NSData alloc]initWithBase64EncodedString:str options:0];

    NSData*abc = [base AES256DecryptWithKey:de];

    这两句代码就是核心代码了,第一句是base64编码(我会在另一篇文章里面单独介绍的)

    - (NSData *)AES256DecryptWithKey:(NSString *)key {

           // 'key' should be 32 bytes for AES256, will be null-padded otherwise

    // 中文翻译(翻译的不太好勿怪)意思是AES256 它解密的key应该是占32个字节不够的话用0填充

    //  创建一个数组 kCCKeySizeAES256代表一个数值32 意思是此数组能够存33个字符

                      char keyPtr[kCCKeySizeAES256+1]; // room for terminator (unused)

    //  下面这句意思不够 用0填充

                      bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding)

    // fetch key data (获取关键数据)

    [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];

    // 这里的self 代表调用这个方法的NSData 对象 计算大小

    NSUInteger dataLength = [self length];

    // 块加密,输出的大小总是小于或等于输入大小加上一个块的大小.这就是为什么我们需要在这里添加一个块的大小

    //See the doc: For block ciphers, the output size will always be less than or

    //equal to the input size plus the size of one block.

    //That's why we need to add the size of one block here

    size_t bufferSize = dataLength + kCCBlockSizeAES128;

    void *buffer = malloc(bufferSize);

    size_t numBytesDecrypted = 0;

    CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding | kCCOptionECBMode,

    keyPtr, kCCKeySizeAES128,

    NULL /* initialization vector (optional) */,

    [self bytes], dataLength, /* input */

    buffer, bufferSize, /* output */

    &numBytesDecrypted);

    if (cryptStatus == kCCSuccess) {

    //  返回的NSData拥有自己的缓冲区并将在delloac时释放掉

    //the returned NSData takes ownership of the buffer and will free it on deallocation

    return [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted];

    }

    free(buffer); //free the buffer;

    return nil;

    }

    重点就是介绍加粗的字体了,其实我也就是翻译一下

    2.接下来就是加密,加密与解密代码上面大同小异,估计是因为AES256是属于对称加密的原因

    NSData *data2 = [data AES256EncryptWithKey:@"此处是16个字符(也就是32个字节)"];

    NSString *base64Encoded = [data2 base64EncodedStringWithOptions:0];

    因为我们解密的时候是先追加base64编码然后解的密,所以当我们加密的时候,方向就要反过来,先加密然后base64编码。代码附上:

    - (NSData *)AES256EncryptWithKey:(NSString *)key {

    // 'key' should be 32 bytes for AES256, will be null-padded otherwise

    char keyPtr[kCCKeySizeAES128+1]; // room for terminator (unused)

    bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding)

    // fetch key data

    [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];

    NSUInteger dataLength = [self length];

    //See the doc: For block ciphers, the output size will always be less than or

    //equal to the input size plus the size of one block.

    //That's why we need to add the size of one block here

    size_t bufferSize = dataLength + kCCBlockSizeAES128;

    void *buffer = malloc(bufferSize);

    size_t numBytesEncrypted = 0;

    CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding | kCCOptionECBMode,

    keyPtr, kCCKeySizeAES128,

    NULL /* initialization vector (optional) */,

    [self bytes], dataLength, /* input */

    buffer, bufferSize, /* output */

    &numBytesEncrypted);

    if (cryptStatus == kCCSuccess) {

    //the returned NSData takes ownership of the buffer and will free it on deallocation

    return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];

    }

    free(buffer); //free the buffer;

    return nil;

    }

    最后你只需要把方法名写到.h中就可以愉快的使用了

    相关文章

      网友评论

        本文标题:iOS开发中AES加密浅解

        本文链接:https://www.haomeiwen.com/subject/hnszettx.html