簡(jiǎn)單 C語言 AES 加密:
.h
@interface AESTool : NSObject
/**NSData加密*/
NSData * aesCodeEncryptData(NSData *contentData);
/**NSData解密*/
NSData * aesCodeDecryptData(NSData *contentData);
@end
.m
//一定要記得倒入頭文件
#import <CommonCrypto/CommonCryptor.h>
size_t const kCodeKeySize = kCCKeySizeAES128;
@implementation AESTool
/*
data加密
*/
NSData * aesCodeEncryptData(NSData *contentData) {
NSCParameterAssert(contentData);
//進(jìn)行加密操作
return cipherCodeOperation(contentData, kCCEncrypt);
}
/*
data解密
*/
NSData * aesCodeDecryptData(NSData *contentData) {
NSCParameterAssert(contentData);
//進(jìn)行解密操作
return cipherCodeOperation(contentData, kCCDecrypt);
}
/*
加密解密最根本的方法,分配內(nèi)存赤嚼,進(jìn)行處理,返回結(jié)果膀藐,清空內(nèi)存
*/
NSData * cipherCodeOperation(NSData *contentData, CCOperation operation) {
/**{189,183,54,17,106,95,114,246,134,83,157,160,16,172,64,99}*/
/**189 183 54 17 106 95 114 246
134 83 157 160 16 172 64 99
*/
//加密key
Byte SECRET_KEY[16] = {};
SECRET_KEY[0] = 0x16;
SECRET_KEY[1] = 0xB1;
SECRET_KEY[2] = 0x71;
SECRET_KEY[3] = 0x62;
SECRET_KEY[4] = 0x2D;
SECRET_KEY[5] = 0x21;
SECRET_KEY[6] = 0x21;
SECRET_KEY[7] = 0x30;
SECRET_KEY[8] = 0x3C;
SECRET_KEY[9] = 0x22;
SECRET_KEY[10] = 0xA4;
SECRET_KEY[11] = 0x12;
SECRET_KEY[12] = 0xA3;
SECRET_KEY[13] = 0xAE;
SECRET_KEY[14] = 0xCD;
SECRET_KEY[15] = 0xC8;
char *keyPtr = (char*)[NSData dataWithBytes:SECRET_KEY length:sizeof(SECRET_KEY)].bytes;
//加密iv
Byte IVKEY[16] = {};
IVKEY[0] = 0;
IVKEY[1] = 1;
IVKEY[2] = 2;
IVKEY[3] = 3;
IVKEY[4] = 4;
IVKEY[5] = 5;
IVKEY[6] = 6;
IVKEY[7] = 7;
IVKEY[8] = 8;
IVKEY[9] = 9;
IVKEY[10] = 10;
IVKEY[11] = 11;
IVKEY[12] = 12;
IVKEY[13] = 13;
IVKEY[14] = 14;
IVKEY[15] = 15;
char *iv = (char*)[NSData dataWithBytes:IVKEY length:sizeof(IVKEY)].bytes;
//得到內(nèi)容的byte個(gè)數(shù)
void const *contentBytes = contentData.bytes;
//得到key的byte個(gè)數(shù)
//加密內(nèi)容的長(zhǎng)度
NSUInteger dataLength = contentData.length;
//本身長(zhǎng)度 +為了防止最后一個(gè)明(密)文塊不完整瀑踢,所以加上16bytes
size_t operationSize = dataLength + kCCBlockSizeAES128;
//內(nèi)存自動(dòng)分配函數(shù)讨永,最后得到的是一個(gè)自動(dòng)分配的存儲(chǔ)區(qū)秘案,operationBytes是地址答姥,此存儲(chǔ)區(qū)的初始內(nèi)容不確定漓糙,如果失敗返回NULL
void *operationBytes = malloc(operationSize);
if (operationBytes == NULL) {
return nil;
}
//初始化輸出尺寸
size_t actualOutSize = 0;
//執(zhí)行CCCrypt加密解密方法
//operation指的是加密解密的類型
//我們傳入的指針變量contentBytes實(shí)際上對(duì)應(yīng)的是存儲(chǔ)區(qū)的地址铣缠,當(dāng)然我們要在加密方法中,在地址中放入密文塊或者名文塊昆禽,方便后期獲取
//把a(bǔ)ctualOutSize的地址放到函數(shù)內(nèi)蝗蛙,在函數(shù)中對(duì)地址中對(duì)應(yīng)的數(shù)字進(jìn)行操作,最后我們得到的是輸出時(shí)候的長(zhǎng)度
CCCryptorStatus cryptStatus = CCCrypt(operation,
kCCAlgorithmAES128,
0x0000,
keyPtr,
kCodeKeySize,
iv,
contentBytes,
dataLength,
operationBytes,
operationSize,
&actualOutSize);
//如果判斷狀態(tài)是成功的話
if (cryptStatus == kCCSuccess) {
//從一個(gè)給定的給定字節(jié)數(shù)的存儲(chǔ)區(qū)地址,創(chuàng)建并返回一個(gè)數(shù)據(jù)對(duì)象醉鳖。從存儲(chǔ)區(qū)地址獲取存儲(chǔ)區(qū)內(nèi)容捡硅,內(nèi)容在方法中已經(jīng)放好了 operationBytes本身是一個(gè)地址
return [NSData dataWithBytesNoCopy:operationBytes length:actualOutSize];
}
//malloc()函數(shù)自動(dòng)分配內(nèi)存,最后是一定要釋放的
free(operationBytes);
operationBytes = NULL;
return nil;
}