iOS AES加密
AES加密
之前公司项目有需要用到这个加密,但是由于网络上找到的都是没有设置向量的,所以后来进行整理之后,自己写了个(虽然大部分是抄袭,请多指正)
1.下载GTM64,自己写太麻烦,本着腾讯精神,直接用别人的。
2.新建一个类别
类别3.然后就是给里面添加方法了
.h文件中添加这几种方法
+(NSString *)AES128Encrypt:(NSString *)plainText withKey:(NSString *)key withIvStr:(NSString *)ivStr;
+(NSString *)processDecodedString:(NSString *)decoded;
+(NSString *)AES128Decrypt:(NSString *)encryptText withKey:(NSString *)key withIvStr:(NSString *)ivStr;
+(BOOL)validKey:(NSString*)key;
-(NSString *)processDecodedString:(NSString *)decoded;
.m文件中需要引入一下头文件
#import "GTMBase64.h"
#import <CommonCrypto/CommonDigest.h>
#import <CommonCrypto/CommonCryptor.h>
下面就是方法的实现,key和向量都作为参数传入了,传入一个plainText,用一个字符串接返回值就OK啦,用起来十分方便。
+(NSString *)AES128Encrypt:(NSString *)plainText withKey:(NSString *)key withIvStr:(NSString *)ivStr
{
if(![self validKey:key] ){
return nil;
}
char keyPtr[kCCKeySizeAES128+1];
memset(keyPtr,0,sizeof(keyPtr));
[key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
char ivPtr[kCCBlockSizeAES128+1];
memset(ivPtr,0,sizeof(ivPtr));
[key getCString:ivPtr maxLength:sizeof(ivPtr)encoding:NSUTF8StringEncoding];
NSData* data = [plainText dataUsingEncoding:NSUTF8StringEncoding];
NSUInteger dataLength = [data length];
int diff = kCCKeySizeAES128-(dataLength%kCCKeySizeAES128);
unsigned long newSize = 0;
if(diff > 0)
{
newSize=dataLength+diff;
NSLog(@"diff is %d",diff);
}
char dataPtr[newSize];
memcpy(dataPtr,[data bytes], [data length]);
for(int i = 0; i < diff;i++)
{
dataPtr[i+dataLength] =0x0000;
}
size_t bufferSize =newSize+kCCBlockSizeAES128;
void *buffer = malloc(bufferSize);
memset(buffer,0,bufferSize);
size_t numBytesCrypted = 0;
// NSString * new =[NSString stringWithFormat:@"n8fh3nFD955fe39J"]ini;
CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt,
kCCAlgorithmAES128,
kCCOptionPKCS7Padding,
[key UTF8String],
kCCKeySizeAES128,
[ivStr UTF8String],
dataPtr,
sizeof(dataPtr),
buffer,
bufferSize,
&numBytesCrypted);
if (cryptStatus == kCCSuccess){
NSData *resultData = [NSData dataWithBytesNoCopy:buffer length:numBytesCrypted];
return [GTMBase64 stringByEncodingData:resultData];
}
free(buffer);
return nil;
}
+(NSString *)processDecodedString:(NSString *)decoded
{
if( decoded==nil || decoded.length==0 ){
return nil;
}
const char *tmpStr=[decoded UTF8String];
int i=0;
while( tmpStr[i]!='\0' )
{
i++;
}
NSString *final=[[NSString alloc]initWithBytes:tmpStr length:i encoding:NSUTF8StringEncoding];
return final;
}
+(NSString *)AES128Decrypt:(NSString *)encryptText withKey:(NSString *)key withIvStr:(NSString *)ivStr
{
if( ![self validKey:key] ){
return nil;
}
char keyPtr[kCCKeySizeAES128 + 1];
memset(keyPtr, 0, sizeof(keyPtr));
[key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
char ivPtr[kCCBlockSizeAES128 + 1];
memset(ivPtr, 0, sizeof(ivPtr));
[key 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,
[key UTF8String],
kCCBlockSizeAES128,
[ivStr UTF8String],
[data bytes],
dataLength,
buffer,
bufferSize,
&numBytesCrypted);
if (cryptStatus == kCCSuccess) {
NSData *resultData = [NSData dataWithBytesNoCopy:buffer length:numBytesCrypted];
NSString *decoded=[[NSString alloc] initWithData:resultData encoding:NSUTF8StringEncoding];
return [self processDecodedString:decoded];
}
free(buffer);
return nil;
}
+(BOOL)validKey:(NSString*)key
{
if( key==nil || key.length !=16 ){
return NO;
}
return YES;
}
-(NSString *)processDecodedString:(NSString *)decoded
{
if( decoded==nil || decoded.length==0 ){
return nil;
}
const char *tmpStr=[decoded UTF8String];
int i=0;
while( tmpStr[i]!='\0' )
{
i++;
}
NSString *final=[[NSString alloc]initWithBytes:tmpStr length:i encoding:NSUTF8StringEncoding];
return final;
}