iOS base64 加密解密 通用类实例代码
程序员文章站
2023-12-19 21:53:58
在使用过程中,直接将被类引入到项目中即可,不需要其它辅助类。
使用示例: 将此通用类的头文件引入到目标类后,直接使用类名进行调用即可。
nsstri...
在使用过程中,直接将被类引入到项目中即可,不需要其它辅助类。
使用示例: 将此通用类的头文件引入到目标类后,直接使用类名进行调用即可。
nsstring *str = [nsstring stringwithformat:@"ywe="]; nsstring *str1 = [nsstring stringwithformat:@"aa"]; nslog(@"resultstr========%@",[commonfunc textfrombase64string:str]); //使用类名进行调用 nslog(@"resultstr=========%@",[commonfunc base64stringfromtext:str1]); //使用类名进行调用
源码下载:
在项目中遇到字符串的base64编解码,分享一下工具类:
jkcommonfunc.h
#define __base64( text ) [commonfunc base64stringfromtext:text] #define __text( base64 ) [commonfunc textfrombase64string:base64] #import <foundation/foundation.h> @interface jkcommonfunc : nsobject /****************************************************************************** 函数名称 : + (nsstring *)base64stringfromtext:(nsstring *)text 函数描述 : 将文本转换为base64格式字符串 输入参数 : (nsstring *)text 文本 输出参数 : n/a 返回参数 : (nsstring *) base64格式字符串 备注信息 : ******************************************************************************/ + (nsstring *)base64stringfromtext:(nsstring *)text; /****************************************************************************** 函数名称 : + (nsstring *)textfrombase64string:(nsstring *)base64 函数描述 : 将base64格式字符串转换为文本 输入参数 : (nsstring *)base64 base64格式字符串 输出参数 : n/a 返回参数 : (nsstring *) 文本 备注信息 : ******************************************************************************/ + (nsstring *)textfrombase64string:(nsstring *)base64; @end
jkcommonfunc.m
#import "commonfunc.h" //引入ios自带密码库 #import <commoncrypto/commoncryptor.h> //空字符串 #define localstr_none @"" static const char encodingtable[] = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/"; @implementation commonfunc + (nsstring *)base64stringfromtext:(nsstring *)text { if (text && ![text isequaltostring:localstr_none]) { //取项目的bundleidentifier作为key 改动了此处 //nsstring *key = [[nsbundle mainbundle] bundleidentifier]; nsdata *data = [text datausingencoding:nsutf8stringencoding]; //ios 自带des加密 begin 改动了此处 //data = [self desencrypt:data withkey:key]; //ios 自带des加密 end return [self base64encodedstringfrom:data]; } else { return localstr_none; } } + (nsstring *)textfrombase64string:(nsstring *)base64 { if (base64 && ![base64 isequaltostring:localstr_none]) { //取项目的bundleidentifier作为key 改动了此处 //nsstring *key = [[nsbundle mainbundle] bundleidentifier]; nsdata *data = [self datawithbase64encodedstring:base64]; //ios 自带des解密 begin 改动了此处 //data = [self desdecrypt:data withkey:key]; //ios 自带des加密 end return [[nsstring alloc] initwithdata:data encoding:nsutf8stringencoding]; } else { return localstr_none; } } /****************************************************************************** 函数名称 : + (nsdata *)desencrypt:(nsdata *)data withkey:(nsstring *)key 函数描述 : 文本数据进行des加密 输入参数 : (nsdata *)data (nsstring *)key 输出参数 : n/a 返回参数 : (nsdata *) 备注信息 : 此函数不可用于过长文本 ******************************************************************************/ + (nsdata *)desencrypt:(nsdata *)data withkey:(nsstring *)key { char keyptr[kcckeysizeaes256+1]; bzero(keyptr, sizeof(keyptr)); [key getcstring:keyptr maxlength:sizeof(keyptr) encoding:nsutf8stringencoding]; nsuinteger datalength = [data length]; size_t buffersize = datalength + kccblocksizeaes128; voidvoid *buffer = malloc(buffersize); size_t numbytesencrypted = 0; cccryptorstatus cryptstatus = cccrypt(kccencrypt, kccalgorithmdes, kccoptionpkcs7padding | kccoptionecbmode, keyptr, kccblocksizedes, null, [data bytes], datalength, buffer, buffersize, &numbytesencrypted); if (cryptstatus == kccsuccess) { return [nsdata datawithbytesnocopy:buffer length:numbytesencrypted]; } free(buffer); return nil; } /****************************************************************************** 函数名称 : + (nsdata *)desencrypt:(nsdata *)data withkey:(nsstring *)key 函数描述 : 文本数据进行des解密 输入参数 : (nsdata *)data (nsstring *)key 输出参数 : n/a 返回参数 : (nsdata *) 备注信息 : 此函数不可用于过长文本 ******************************************************************************/ + (nsdata *)desdecrypt:(nsdata *)data withkey:(nsstring *)key { char keyptr[kcckeysizeaes256+1]; bzero(keyptr, sizeof(keyptr)); [key getcstring:keyptr maxlength:sizeof(keyptr) encoding:nsutf8stringencoding]; nsuinteger datalength = [data length]; size_t buffersize = datalength + kccblocksizeaes128; voidvoid *buffer = malloc(buffersize); size_t numbytesdecrypted = 0; cccryptorstatus cryptstatus = cccrypt(kccdecrypt, kccalgorithmdes, kccoptionpkcs7padding | kccoptionecbmode, keyptr, kccblocksizedes, null, [data bytes], datalength, buffer, buffersize, &numbytesdecrypted); if (cryptstatus == kccsuccess) { return [nsdata datawithbytesnocopy:buffer length:numbytesdecrypted]; } free(buffer); return nil; } /****************************************************************************** 函数名称 : + (nsdata *)datawithbase64encodedstring:(nsstring *)string 函数描述 : base64格式字符串转换为文本数据 输入参数 : (nsstring *)string 输出参数 : n/a 返回参数 : (nsdata *) 备注信息 : ******************************************************************************/ + (nsdata *)datawithbase64encodedstring:(nsstring *)string { if (string == nil) [nsexception raise:nsinvalidargumentexception format:nil]; if ([string length] == 0) return [nsdata data]; static charchar *decodingtable = null; if (decodingtable == null) { decodingtable = malloc(256); if (decodingtable == null) return nil; memset(decodingtable, char_max, 256); nsuinteger i; for (i = 0; i < 64; i++) decodingtable[(short)encodingtable[i]] = i; } const charchar *characters = [string cstringusingencoding:nsasciistringencoding]; if (characters == null) // not an ascii string! return nil; charchar *bytes = malloc((([string length] + 3) / 4) * 3); if (bytes == null) return nil; nsuinteger length = 0; nsuinteger i = 0; while (yes) { char buffer[4]; short bufferlength; 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); return nil; } } if (bufferlength == 0) break; if (bufferlength == 1) // at least two characters are needed to produce one byte! { free(bytes); return nil; } // 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 [nsdata datawithbytesnocopy:bytes length:length]; } /****************************************************************************** 函数名称 : + (nsstring *)base64encodedstringfrom:(nsdata *)data 函数描述 : 文本数据转换为base64格式字符串 输入参数 : (nsdata *)data 输出参数 : n/a 返回参数 : (nsstring *) 备注信息 : ******************************************************************************/ + (nsstring *)base64encodedstringfrom:(nsdata *)data { if ([data length] == 0) return @""; charchar *characters = malloc((([data length] + 2) / 3) * 4); if (characters == null) return nil; nsuinteger length = 0; nsuinteger i = 0; while (i < [data length]) { char buffer[3] = {0,0,0}; short bufferlength = 0; while (bufferlength < 3 && i < [data length]) buffer[bufferlength++] = ((charchar *)[data bytes])[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)]; else characters[length++] = '='; if (bufferlength > 2) characters[length++] = encodingtable[buffer[2] & 0x3f]; else characters[length++] = '='; } return [[nsstring alloc] initwithbytesnocopy:characters length:length encoding:nsasciistringencoding freewhendone:yes]; } @end