關(guān)于AES的介紹請參考AES算法詳解墩划,這里我就不多介紹了,我主要介紹到我們項目中用到的AES加密和解密的使用方法海雪。
創(chuàng)建內(nèi)方法
//AES加密和解密
+(NSString*)AesEncrypt:(NSString*)str;
+(NSString*)AesDecrypt:(NSString*)str;
在.m文件里代碼如下
AES加密
+(NSString*)AesEncrypt:(NSString*)str{
NSString*key=@"X*Z_j2#3%z5&H+M4";//密鑰
NSData*data=[strdataUsingEncoding:NSUTF8StringEncoding];//待加密字符轉(zhuǎn)為NSData型
charkeyPtr[kCCKeySizeAES128+1];
memset(keyPtr,0,sizeof(keyPtr));
[key getCString:keyPtrmaxLength:sizeof(keyPtr)encoding:NSUTF8StringEncoding];
NSUIntegerdataLength = [datalength];
size_tbufferSize = dataLength +kCCBlockSizeAES128;
void*buffer =malloc(bufferSize);
size_tnumBytesCrypted =0;
CCCryptorStatuscryptStatus =CCCrypt(
? ? ? kCCEncrypt,? ? //? 加密? ? kCCDecrypt解密
? ? ? kCCAlgorithmAES128, ? ?//填充方式
? ?? kCCOptionPKCS7Padding|kCCOptionECBMode, ? ? ?//工作模式
? ? ? keyPtr,? //AES的密鑰長度有128字節(jié)番刊、192字節(jié)垄分、256字節(jié)幾種,這里舉出可能存在的最大長度
? ? ? kCCBlockSizeAES128, ? ? ?//密文長度+補位長度
? ? ? nil, ? ? //偏移量计福,由于是對稱加密跌捆,用不到
? ? ? [databytes], ? //字節(jié)大小
? ? ? dataLength, ? ?//字節(jié)長度
? ? ? buffer, ? ? ??
? ? ? bufferSize, ?
? ? ? &numBytesCrypted);
if(cryptStatus ==kCCSuccess) {
NSData*resultData=[NSDatadataWithBytesNoCopy:bufferlength:numBytesCrypted];
NSString*result =[selfbase64EncodedStringFrom:resultData];
returnresult;
}
free(buffer);
returnstr;
}
解密操作:
+(NSString*)AesDecrypt:(NSString*)str{
NSString*key=@"X*Z_j2#3%z5&H+M4";M ?//密鑰
NSData*data=[selfdataWithBase64EncodedString:str];// base4解碼
charkeyPtr[kCCKeySizeAES128+1];
memset(keyPtr,0,sizeof(keyPtr));
[keygetCString:keyPtrmaxLength:sizeof(keyPtr)encoding:NSUTF8StringEncoding];
NSUIntegerdataLength = [datalength];
size_tbufferSize = dataLength +kCCBlockSizeAES128;
void*buffer =malloc(bufferSize);
size_tnumBytesCrypted =0;
CCCryptorStatuscryptStatus =CCCrypt(kCCDecrypt,
kCCAlgorithmAES128,
kCCOptionPKCS7Padding|kCCOptionECBMode,
keyPtr,
kCCBlockSizeAES128,
nil,
[databytes],
dataLength,
buffer,
bufferSize,
&numBytesCrypted);
if(cryptStatus ==kCCSuccess) {
NSData*resultData=[NSDatadataWithBytesNoCopy:bufferlength:numBytesCrypted];
NSString*result =[[NSStringalloc]initWithData:resultDataencoding:NSUTF8StringEncoding];
returnresult;
}
free(buffer);
returnstr;
}
總結(jié)一下:
常見的字符編碼有:UTF-8、ASCII棒搜、Base64疹蛉、十六進(jìn)制等等,不要使用UTF-8力麸,加密過程是使用UTF-8完成的可款,但是加密完后的NSData無法通過UTF-8編碼格式轉(zhuǎn)出成NSString,推薦使用Base64編碼或者十六進(jìn)制編碼克蚂,取決于后臺闺鲸。另外加密的數(shù)據(jù)長度大于10位的情況下,工作模式不要只寫kCCOptionPKCS7Padding要把kCCOptionECBMode也加上埃叭,請測在密文在16位以上解密以后跟后臺不一致摸恍,加上kCCOptionECBMode會解決.
以下是base64的編碼和解碼
.m文件加上這個
#import? <CommonCrypto/CommonCryptor.h>
static constcharencodingTable[] ="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
#define LocalStr_None @""http://空字符串
+ (NSString*)base64StringFromText:(NSString*)text
{
if(text && ![textisEqualToString:LocalStr_None]) {
NSData*data = [textdataUsingEncoding:NSUTF8StringEncoding];
return[selfbase64EncodedStringFrom:data];
}
else{
returnLocalStr_None;
}
}
+ (NSString*)textFromBase64String:(NSString*)base64
{
if(base64 && ![base64isEqualToString:LocalStr_None]) {
NSData*data = [selfdataWithBase64EncodedString:base64];
return[[NSStringalloc]initWithData:dataencoding:NSUTF8StringEncoding];
}
else{
returnLocalStr_None;
}
}
+ (NSData*)dataWithBase64EncodedString:(NSString*)string
{
if(string ==nil)
[NSExceptionraise:NSInvalidArgumentExceptionformat:nil];
if([stringlength] ==0)
return[NSDatadata];
staticchar*decodingTable =NULL;
if(decodingTable ==NULL)
{
decodingTable =malloc(256);
if(decodingTable ==NULL)
returnnil;
memset(decodingTable,CHAR_MAX,256);
NSUIntegeri;
for(i =0; i <64; i++)
decodingTable[(short)encodingTable[i]] = i;
}
constchar*characters = [stringcStringUsingEncoding:NSASCIIStringEncoding];
if(characters ==NULL)//Not an ASCII string!
returnnil;
char*bytes =malloc((([stringlength] +3) /4) *3);
if(bytes ==NULL)
returnnil;
NSUIntegerlength =0;
NSUIntegeri =0;
while(YES)
{
charbuffer[4];
shortbufferLength;
for(bufferLength =0; bufferLength <4; i++)
{
if(characters[i] =='\0')
break;
if(isspace(characters[i]) || characters[i] =='=')
continue;
buffer[bufferLength] = decodingTable[(short)characters[i]];
if(buffer[bufferLength++] ==CHAR_MAX)//Illegal character!
{
free(bytes);
returnnil;
}
}
if(bufferLength ==0)
break;
if(bufferLength ==1)//At least two characters are needed to produce one byte!
{
free(bytes);
returnnil;
}
//Decode the characters in the buffer to bytes.
bytes[length++] = (buffer[0] <<2) | (buffer[1] >>4);
if(bufferLength >2)
bytes[length++] = (buffer[1] <<4) | (buffer[2] >>2);
if(bufferLength >3)
bytes[length++] = (buffer[2] <<6) | buffer[3];
}
bytes =realloc(bytes, length);
return[NSDatadataWithBytesNoCopy:byteslength:length];
}
+ (NSString*)base64EncodedStringFrom:(NSData*)data
{
if([datalength] ==0)
return@"";
char*characters =malloc((([datalength] +2) /3) *4);
if(characters ==NULL)
returnnil;
NSUIntegerlength =0;
NSUIntegeri =0;
while(i < [datalength])
{
charbuffer[3] = {0,0,0};
shortbufferLength =0;
while(bufferLength <3&& i < [datalength])
buffer[bufferLength++] = ((char*)[databytes])[i++];
//Encode the bytes in the buffer to four characters, including padding "=" characters if necessary.
characters[length++] =encodingTable[(buffer[0] &0xFC) >>2];
characters[length++] =encodingTable[((buffer[0] &0x03) <<4) | ((buffer[1] &0xF0) >>4)];
if(bufferLength >1)
characters[length++] =encodingTable[((buffer[1] &0x0F) <<2) | ((buffer[2] &0xC0) >>6)];
elsecharacters[length++] ='=';
if(bufferLength >2)
characters[length++] =encodingTable[buffer[2] &0x3F];
elsecharacters[length++] ='=';
}
?return ? [[NSStringalloc]initWithBytesNoCopy:characterslength:lengthencoding:NSASCIIStringEncodin ? gfreeWhenDone:YES];
}