在使用过程中,直接将被类引入到项目中即可,不需要其它辅助类。
使用示例: 将此通用类的头文件引入到目标类后,直接使用类名进行调用即可。
1
2
3
4
|
NSString *str = [NSString stringWithFormat:@ "YWE=" ]; NSString *str1 = [NSString stringWithFormat:@ "aa" ]; NSLog(@ "resultStr========%@" ,[CommonFunc textFromBase64String:str]); //使用类名进行调用 |
源码下载:
在项目中遇到字符串的base64编解码,分享一下工具类:
JKCommonFunc.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
#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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
|
# 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 |
原文链接:http://blog.csdn.net/u010960265/article/details/78779133