所有加密代码都在这里
AES加密方法
/**
* AES加密
* aesKey 加密Key
*/
+(NSData *)AESParmEncryptWithKey:(NSString *)AES_Key andEncryptData:(NSData *)encryptData //加密
{
NSString * kInitVector = [AES_Key copy]; //16位偏移,CBC模式才有
NSData *initVector = [kInitVector dataUsingEncoding:NSUTF8StringEncoding];
//公钥
char keyPtr[kCCKeySizeAES128+1];
memset(keyPtr, 0, sizeof(keyPtr));
[AES_Key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
//数据长度
NSUInteger dataLength = encryptData.length;
//加密输出缓冲区大小
size_t bufferSize = dataLength + kCCBlockSizeAES128;
//加密输出缓冲区
void *buffer = malloc(bufferSize);
//实际输出大小
size_t numBytesEncrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt,//kCCEncrypt 代表加密 kCCDecrypt代表解密
kCCAlgorithmAES,//加密算法
kCCOptionPKCS7Padding,//CBC -> PKCS7Padding,ECB -> kCCOptionPKCS7Padding|kCCOptionECBMode
keyPtr,
kCCBlockSizeAES128,//密钥长度128
initVector.bytes,//偏移字符串, ECB模式传NULL
encryptData.bytes,//编码内容
dataLength,
buffer,
bufferSize,
&numBytesEncrypted);
if (cryptStatus == kCCSuccess) {
return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
}
free(buffer);
return nil;
}
/**
* AES加密
* aesKey 加密Key
*/
+ (NSString *)AES_EncryptWithKey:(NSString *)AES_Key andEncryptText:(NSString *)encryptText
{
AES_Key = [AESUtils checkAESKey:AES_Key];
const char *cstr = [encryptText cStringUsingEncoding:NSUTF8StringEncoding];
NSData *data = [NSData dataWithBytes:cstr length:encryptText.length];
//对数据进行加密
NSData *result = [AESUtils AESParmEncryptWithKey:AES_Key andEncryptData:data];
//转换为2进制字符串
if (result && result.length > 0) {
Byte *datas = (Byte*)[result bytes];
NSMutableString *output = [NSMutableString stringWithCapacity:result.length * 2];
for(int i = 0; i < result.length; i++){
[output appendFormat:@"%02x", datas[i]];
}
return output;
}
return nil;
}
AES解密方法
/**
* AES解密
* aesKey 加密Key
*/
+ (NSData *)AESParmDecryptWithKey:(NSString *)AES_Key andDecryptData:(NSData *)decryptData //解密
{
NSString * kInitVector = [AES_Key copy]; //16位偏移,CBC模式才有
NSData *initVector = [kInitVector dataUsingEncoding:NSUTF8StringEncoding];
//公钥
char keyPtr[kCCKeySizeAES128+1];
memset(keyPtr, 0, sizeof(keyPtr));
[AES_Key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
//数据长度
NSUInteger dataLength = decryptData.length;
//加密输出缓冲区大小
size_t bufferSize = dataLength + kCCBlockSizeAES128;
//加密输出缓冲区
void *buffer = malloc(bufferSize);
//实际输出大小
size_t numBytesDecrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt,//kCCEncrypt 代表加密 kCCDecrypt代表解密
kCCAlgorithmAES,//加密算法
kCCOptionPKCS7Padding,//CBC -> PKCS7Padding,ECB -> kCCOptionPKCS7Padding|kCCOptionECBMode
keyPtr,
kCCBlockSizeAES128,//密钥长度128
initVector.bytes,//偏移字符串,, ECB模式传NULL
decryptData.bytes,//编码内容
dataLength,
buffer,
bufferSize,
&numBytesDecrypted);
if (cryptStatus == kCCSuccess) {
return [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted];
}
free(buffer);
return nil;
}
/**
* AES解密
* aesKey 加密Key
*/
+ (NSString *)AES_DecryptWithKey:(NSString *)AES_Key andDecryptText:(NSString *)decryptText
{
AES_Key = [AESUtils checkAESKey:AES_Key];
//转换为2进制Data
NSMutableData *data = [NSMutableData dataWithCapacity:decryptText.length / 2];
unsigned char whole_byte;
char byte_chars[3] = {'\0','\0','\0'};
int i;
for (i=0; i < [decryptText length] / 2; i++) {
byte_chars[0] = [decryptText characterAtIndex:i*2];
byte_chars[1] = [decryptText characterAtIndex:i*2+1];
whole_byte = strtol(byte_chars, NULL, 16);
[data appendBytes:&whole_byte length:1];
}
//对数据进行解密
NSData* result = [AESUtils AESParmDecryptWithKey:AES_Key andDecryptData:data];
if (result && result.length > 0) {
return [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];
}
return nil;
}
/**
* 加密key检查
* aesKey 加密Key
*/
+ (NSString *)checkAESKey:(NSString *)AES_Key
{
if (AES_Key.length < 16) {
NSMutableArray *array = [NSMutableArray arrayWithArray:@[AES_Key]];
while (array.count <= 16 - AES_Key.length) {
[array insertObject:@"0" atIndex:0];
}
AES_Key = [array componentsJoinedByString:@""];
}
return AES_Key;
}
Demo地址Demo
如有错误请指出。