@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;
#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;
}