使用 NSData 分类实现,对 NSData 数据类型进行 AES 加密

一般对NSData的数据类型进行加密,这里就将 .h .m 文件分享出来,有需要的可以直接粘贴使用。
 
 
下面是 .h 文件

 
#import <Foundation/Foundation.h>

@interface NSData (AES)

/**
 *  加密,使用字符串作为key
 */
- (NSData *)AES128EncryptWithKey:(NSString *)key;

/**
 *  加密,使用NSData作为key
 */
- (NSData *)AES128EncryptWithKeyData:(NSData *)keyData;

/**
 *  解密,使用字符串作为key
 */
- (NSData *)AES128DecodeWithKey:(NSString *)key;

/**
 *  解密,使用NSData作为key
 */
- (NSData *)AES128DecodeWithKeyData:(NSData *)keyData;

@end
 
 
 
下面是 .m 文件

 
 
#import "NSData+AES.h"
#import <CommonCrypto/CommonCrypto.h>

@implementation NSData (AES)

// 加密,使用字符串作为key
- (NSData *)AES128EncryptWithKey:(NSString *)key {
    char keyPtr[kCCKeySizeAES256+1];
    bzero(keyPtr, sizeof(keyPtr));
    [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
    NSUInteger dataLength = [selflength];
    size_t bufferSize = dataLength + kCCBlockSizeAES128;
    void *buffer = malloc(bufferSize);
    size_t numBytesEncrypted = 0;
    CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128,
                                          kCCOptionPKCS7Padding | kCCOptionECBMode,
                                          keyPtr, kCCBlockSizeAES128,
                                          NULL,
                                          [selfbytes], dataLength,
                                          buffer, bufferSize,
                                          &numBytesEncrypted);
    if (cryptStatus == kCCSuccess) {
        return [NSDatadataWithBytesNoCopy:buffer length:numBytesEncrypted];
    }
    free(buffer);
    returnnil;
}

// 加密,使用NSData作为key
- (NSData *)AES128EncryptWithKeyData:(NSData *)keyData {
    char keyPtr[kCCKeySizeAES256+1];
    bzero(keyPtr, sizeof(keyPtr));
    NSUInteger dataLength = [selflength];
    size_t bufferSize = dataLength + kCCBlockSizeAES128;
    void *buffer = malloc(bufferSize);
    size_t numBytesEncrypted = 0;
    CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128,
                                          kCCOptionPKCS7Padding | kCCOptionECBMode,
                                          keyData.bytes, kCCBlockSizeAES128,
                                          NULL,
                                          [selfbytes], dataLength,
                                          buffer, bufferSize,
                                          &numBytesEncrypted);
    if (cryptStatus == kCCSuccess) {
        return [NSDatadataWithBytesNoCopy:buffer length:numBytesEncrypted];
    }
    free(buffer);
    returnnil;
}

//解密,使用字符串作为key
- (NSData *)AES128DecodeWithKey:(NSString *)key {
    char keyPtr[kCCKeySizeAES256+1];
    bzero(keyPtr, sizeof(keyPtr));
    [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
    NSUInteger dataLength = [selflength];
    size_t bufferSize = dataLength + kCCBlockSizeAES128;
    void *buffer = malloc(bufferSize);
    size_t numBytesDecrypted = 0;
    CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES128,
                                          kCCOptionPKCS7Padding | kCCOptionECBMode,
                                          keyPtr, kCCBlockSizeAES128,
                                          NULL,
                                          [selfbytes], dataLength,
                                          buffer, bufferSize,
                                          &numBytesDecrypted);
    if (cryptStatus == kCCSuccess) {
        return [NSDatadataWithBytesNoCopy:buffer length:numBytesDecrypted];
    }
    free(buffer);
    returnnil;
}

//解密,使用NSData作为key
- (NSData *)AES128DecodeWithKeyData:(NSData *)keyData {
    char keyPtr[kCCKeySizeAES256+1];
    bzero(keyPtr, sizeof(keyPtr));
//    [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
    NSUInteger dataLength = [selflength];
    size_t bufferSize = dataLength + kCCBlockSizeAES128;
    void *buffer = malloc(bufferSize);
    size_t numBytesDecrypted = 0;
    CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES128,
                                          kCCOptionPKCS7Padding | kCCOptionECBMode,
                                          keyData.bytes, kCCBlockSizeAES128,
                                          NULL,
                                          [selfbytes], dataLength,
                                          buffer, bufferSize,
                                          &numBytesDecrypted);
    if (cryptStatus == kCCSuccess) {
        return [NSDatadataWithBytesNoCopy:buffer length:numBytesDecrypted];
    }
    free(buffer);
    returnnil;
}

@end
上一篇:PHP安装memcache扩展接口步骤


下一篇:面试官又整新活,居然问我for循环用i++和++i哪个效率高?