//
// AES.h
// AES
//
// Created by DBH on 16/7/21.
// Copyright © 2016年com.chunqiankeji. All rights reserved.
//
#import
@interfaceAES :NSObject
+ (NSString*) AES128Encrypt:(NSString*)plainText WithPassword:(NSString*)paw;
+ (NSString*) AES128Decrypt:(NSString*)encryptText WithPassword:(NSString*)paw;
@end
//
// AES.m
// AES
//
// Created by DBH on 16/7/21.
// Copyright © 2016年com.chunqiankeji. All rights reserved.
//
#import"AES.h"
#import
#import"GTMBase64.h"
//#define gkey@"uuPNMd/vVzqbDw/zsRbU9g==" //自行修改
#define gIv @"1234876512567348"//自行修改
@implementationAES
+(NSString *)AES128Encrypt:(NSString *)plainText WithPassword:(NSString *)paw
{
charkeyPtr[kCCKeySizeAES128+1];
memset(keyPtr,0, sizeof(keyPtr));
[paw getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
charivPtr[kCCBlockSizeAES128+1];
memset(ivPtr,0, sizeof(ivPtr));
[gIv getCString:ivPtr maxLength:sizeof(ivPtr) encoding:NSUTF8StringEncoding];
NSData* data = [plainText dataUsingEncoding:NSUTF8StringEncoding];
NSUInteger dataLength = [data length];
NSUInteger diff = kCCKeySizeAES128 - (dataLength % kCCKeySizeAES128);
NSUInteger newSize =0;
if(diff >0)
{
newSize = dataLength + diff;
}
chardataPtr[newSize];
memcpy(dataPtr, [databytes], [datalength]);
for(inti =0; i < diff; i++)
{
dataPtr[i + dataLength] =0x00;
}
size_tbufferSize = newSize +kCCBlockSizeAES128;
void*buffer = malloc(bufferSize);
memset(buffer, 0, bufferSize);
size_tnumBytesCrypted =0;
CCCryptorStatuscryptStatus =CCCrypt(kCCEncrypt,
kCCAlgorithmAES128,
kCCOptionPKCS7Padding,//No padding/kCCOptionPKCS7Padding
keyPtr,
kCCKeySizeAES128,
ivPtr,
dataPtr,
sizeof(dataPtr),
buffer,
bufferSize,
&numBytesCrypted);
if(cryptStatus ==kCCSuccess) {
NSData*resultData = [NSDatadataWithBytesNoCopy:bufferlength:numBytesCrypted];
return[GTMBase64stringByEncodingData:resultData];
}
free(buffer);
returnnil;
}
+(NSString*)AES128Decrypt:(NSString*)encryptText WithPassword:(NSString*)paw
{
charkeyPtr[kCCKeySizeAES128+1];
memset(keyPtr, 0,sizeof(keyPtr));
[pawgetCString:keyPtrmaxLength:sizeof(keyPtr)encoding:NSUTF8StringEncoding];
charivPtr[kCCBlockSizeAES128+1];
memset(ivPtr, 0,sizeof(ivPtr));
[gIv getCString:ivPtr maxLength:sizeof(ivPtr) encoding:NSUTF8StringEncoding];
NSData *data = [GTMBase64 decodeData:[encryptText dataUsingEncoding:NSUTF8StringEncoding]];
NSUInteger dataLength = [data length];
size_t bufferSize = dataLength + kCCBlockSizeAES128;
void*buffer = malloc(bufferSize);
size_t numBytesCrypted =0;
CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt,
kCCAlgorithmAES128,
kCCOptionPKCS7Padding,//
keyPtr,
kCCBlockSizeAES128,
ivPtr,
[data bytes],
dataLength,
buffer,
bufferSize,
&numBytesCrypted);
if(cryptStatus == kCCSuccess) {
NSData *resultData = [NSData dataWithBytesNoCopy:buffer length:numBytesCrypted];
return[[NSString alloc] initWithData:resultData encoding:NSUTF8StringEncoding];
}
free(buffer);
returnnil;
}
@end
在下载GTMBase64导入 就好 在我的资源里面有