AES加密?
之前公司項(xiàng)目有需要用到這個(gè)加密,但是由于網(wǎng)絡(luò)上找到的都是沒有設(shè)置向量的,所以后來進(jìn)行整理之后倦炒,自己寫了個(gè)(雖然大部分是抄襲,請多指正)
1.下載GTM64积蜻,自己寫太麻煩,本著騰訊精神彻消,直接用別人的竿拆。
2.新建一個(gè)類別
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>
下面就是方法的實(shí)現(xiàn),key和向量都作為參數(shù)傳入了宾尚,傳入一個(gè)plainText丙笋,用一個(gè)字符串接返回值就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;
}