关于AES的介绍请参考AES算法详解,这里我就不多介绍了,我主要介绍到我们项目中用到的AES加密和解密的使用方法。
创建内方法
//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];//待加密字符转为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字节、192字节、256字节几种,这里举出可能存在的最大长度
kCCBlockSizeAES128, //密文长度+补位长度
nil, //偏移量,由于是对称加密,用不到
[databytes], //字节大小
dataLength, //字节长度
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;
}
总结一下:
常见的字符编码有:UTF-8、ASCII、Base64、十六进制等等,不要使用UTF-8,加密过程是使用UTF-8完成的,但是加密完后的NSData无法通过UTF-8编码格式转出成NSString,推荐使用Base64编码或者十六进制编码,取决于后台。另外加密的数据长度大于10位的情况下,工作模式不要只写kCCOptionPKCS7Padding要把kCCOptionECBMode也加上,请测在密文在16位以上解密以后跟后台不一致,加上kCCOptionECBMode会解决.
以下是base64的编码和解码
.m文件加上这个
#import <CommonCrypto/CommonCryptor.h>
static constcharencodingTable[] ="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
#define LocalStr_None @""//空字符串
+ (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];
}