iOS base64 加密解密 通用类实例代码
在使用过程中,直接将被类引入到项目中即可,不需要其它辅助类。
使用示例: 将此通用类的头文件引入到目标类后,直接使用类名进行调用即可。
NSString*str=[NSStringstringWithFormat:@"YWE="]; NSString*str1=[NSStringstringWithFormat:@"aa"]; NSLog(@"resultStr========%@",[CommonFunctextFromBase64String:str]);//使用类名进行调用 NSLog(@"resultStr=========%@",[CommonFuncbase64StringFromText:str1]);//使用类名进行调用
源码下载:
在项目中遇到字符串的base64编解码,分享一下工具类:
JKCommonFunc.h
#define__BASE64(text)[CommonFuncbase64StringFromText:text] #define__TEXT(base64)[CommonFunctextFromBase64String:base64] #import@interfaceJKCommonFunc:NSObject /****************************************************************************** 函数名称:+(NSString*)base64StringFromText:(NSString*)text 函数描述:将文本转换为base64格式字符串 输入参数:(NSString*)text文本 输出参数:N/A 返回参数:(NSString*)base64格式字符串 备注信息: ******************************************************************************/ +(NSString*)base64StringFromText:(NSString*)text; /****************************************************************************** 函数名称:+(NSString*)textFromBase64String:(NSString*)base64 函数描述:将base64格式字符串转换为文本 输入参数:(NSString*)base64base64格式字符串 输出参数:N/A 返回参数:(NSString*)文本 备注信息: ******************************************************************************/ +(NSString*)textFromBase64String:(NSString*)base64; @end
JKCommonFunc.m
#import"CommonFunc.h" //引入IOS自带密码库 #import//空字符串 #defineLocalStr_None@"" staticconstcharencodingTable[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; @implementationCommonFunc +(NSString*)base64StringFromText:(NSString*)text { if(text&&![textisEqualToString:LocalStr_None]){ //取项目的bundleIdentifier作为KEY改动了此处 //NSString*key=[[NSBundlemainBundle]bundleIdentifier]; NSData*data=[textdataUsingEncoding:NSUTF8StringEncoding]; //IOS自带DES加密Begin改动了此处 //data=[selfDESEncrypt:dataWithKey:key]; //IOS自带DES加密End return[selfbase64EncodedStringFrom:data]; } else{ returnLocalStr_None; } } +(NSString*)textFromBase64String:(NSString*)base64 { if(base64&&![base64isEqualToString:LocalStr_None]){ //取项目的bundleIdentifier作为KEY改动了此处 //NSString*key=[[NSBundlemainBundle]bundleIdentifier]; NSData*data=[selfdataWithBase64EncodedString:base64]; //IOS自带DES解密Begin改动了此处 //data=[selfDESDecrypt:dataWithKey:key]; //IOS自带DES加密End return[[NSStringalloc]initWithData:dataencoding:NSUTF8StringEncoding]; } else{ returnLocalStr_None; } } /****************************************************************************** 函数名称:+(NSData*)DESEncrypt:(NSData*)dataWithKey:(NSString*)key 函数描述:文本数据进行DES加密 输入参数:(NSData*)data (NSString*)key 输出参数:N/A 返回参数:(NSData*) 备注信息:此函数不可用于过长文本 ******************************************************************************/ +(NSData*)DESEncrypt:(NSData*)dataWithKey:(NSString*)key { charkeyPtr[kCCKeySizeAES256+1]; bzero(keyPtr,sizeof(keyPtr)); [keygetCString:keyPtrmaxLength:sizeof(keyPtr)encoding:NSUTF8StringEncoding]; NSUIntegerdataLength=[datalength]; size_tbufferSize=dataLength+kCCBlockSizeAES128; voidvoid*buffer=malloc(bufferSize); size_tnumBytesEncrypted=0; CCCryptorStatuscryptStatus=CCCrypt(kCCEncrypt,kCCAlgorithmDES, kCCOptionPKCS7Padding|kCCOptionECBMode, keyPtr,kCCBlockSizeDES, NULL, [databytes],dataLength, buffer,bufferSize, &numBytesEncrypted); if(cryptStatus==kCCSuccess){ return[NSDatadataWithBytesNoCopy:bufferlength:numBytesEncrypted]; } free(buffer); returnnil; } /****************************************************************************** 函数名称:+(NSData*)DESEncrypt:(NSData*)dataWithKey:(NSString*)key 函数描述:文本数据进行DES解密 输入参数:(NSData*)data (NSString*)key 输出参数:N/A 返回参数:(NSData*) 备注信息:此函数不可用于过长文本 ******************************************************************************/ +(NSData*)DESDecrypt:(NSData*)dataWithKey:(NSString*)key { charkeyPtr[kCCKeySizeAES256+1]; bzero(keyPtr,sizeof(keyPtr)); [keygetCString:keyPtrmaxLength:sizeof(keyPtr)encoding:NSUTF8StringEncoding]; NSUIntegerdataLength=[datalength]; size_tbufferSize=dataLength+kCCBlockSizeAES128; voidvoid*buffer=malloc(bufferSize); size_tnumBytesDecrypted=0; CCCryptorStatuscryptStatus=CCCrypt(kCCDecrypt,kCCAlgorithmDES, kCCOptionPKCS7Padding|kCCOptionECBMode, keyPtr,kCCBlockSizeDES, NULL, [databytes],dataLength, buffer,bufferSize, &numBytesDecrypted); if(cryptStatus==kCCSuccess){ return[NSDatadataWithBytesNoCopy:bufferlength:numBytesDecrypted]; } free(buffer); returnnil; } /****************************************************************************** 函数名称:+(NSData*)dataWithBase64EncodedString:(NSString*)string 函数描述:base64格式字符串转换为文本数据 输入参数:(NSString*)string 输出参数:N/A 返回参数:(NSData*) 备注信息: ******************************************************************************/ +(NSData*)dataWithBase64EncodedString:(NSString*)string { if(string==nil) [NSExceptionraise:NSInvalidArgumentExceptionformat:nil]; if([stringlength]==0) return[NSDatadata]; staticcharchar*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; } constcharchar*characters=[stringcStringUsingEncoding:NSASCIIStringEncoding]; if(characters==NULL)//NotanASCIIstring! returnnil; charchar*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)//Illegalcharacter! { free(bytes); returnnil; } } if(bufferLength==0) break; if(bufferLength==1)//Atleasttwocharactersareneededtoproduceonebyte! { free(bytes); returnnil; } //Decodethecharactersinthebuffertobytes. 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 函数描述:文本数据转换为base64格式字符串 输入参数:(NSData*)data 输出参数:N/A 返回参数:(NSString*) 备注信息: ******************************************************************************/ +(NSString*)base64EncodedStringFrom:(NSData*)data { if([datalength]==0) return@""; charchar*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++]=((charchar*)[databytes])[i++]; //Encodethebytesinthebuffertofourcharacters,includingpadding"="charactersifnecessary. 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:NSASCIIStringEncodingfreeWhenDone:YES]; } @end