1 /*
2 *.h文件
3 */
4 #import <Foundation/Foundation.h>
5
6 @interface Security : NSObject
7 +(NSString*)AesEncrypt:(NSString*)str;
8 +(NSString*)AesDecrypt:(NSString*)str;
9 @end
10
11 --------------------------------------------------------------------------------
12
13 /*
14 *.m文件
15 */
16 #import "Security.h"
17 #import <CommonCrypto/CommonCryptor.h>
18 static const char encodingTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
19 #define LocalStr_None @"" //空字符串
20
21 @implementation Security
22
23 /*
24 * AES加密
25 *
26 */
27 +(NSString*)AesEncrypt:(NSString*)str{
28 NSString *key=@"43hr8fhu34b58123"; // 密鑰
29 NSData *data=[str dataUsingEncoding:NSUTF8StringEncoding]; // 待加密字符轉爲NSData型
30 char keyPtr[kCCKeySizeAES128 + 1];
31 memset(keyPtr, 0, sizeof(keyPtr));
32 [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
33
34 NSUInteger dataLength = [data length];
35 size_t bufferSize = dataLength + kCCBlockSizeAES128;
36 voidvoid *buffer = malloc(bufferSize);
37
38 size_t numBytesCrypted = 0;
39 CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt,
40 kCCAlgorithmAES128,
41 kCCOptionPKCS7Padding|kCCOptionECBMode,
42 keyPtr,
43 kCCBlockSizeAES128,
44 nil,
45 [data bytes],
46 dataLength,
47 buffer,
48 bufferSize,
49 &numBytesCrypted);
50 if (cryptStatus == kCCSuccess) {
51 NSData *resultData=[NSData dataWithBytesNoCopy:buffer length:numBytesCrypted];
52 NSString *result =[self base64EncodedStringFrom:resultData];
53 return result;
54 }
55 free(buffer);
56 return str;
57 }
58
59 /*
60 * AES解密
61 *
62 */
63 +(NSString*)AesDecrypt:(NSString*)str{
64 NSString *key=@"Q*1_3@c!4kd^j&g%"; // 密鑰
65 NSData *data=[self dataWithBase64EncodedString:str]; // base4解碼
66 char keyPtr[kCCKeySizeAES128 + 1];
67 memset(keyPtr, 0, sizeof(keyPtr));
68 [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
69
70 NSUInteger dataLength = [data length];
71 size_t bufferSize = dataLength + kCCBlockSizeAES128;
72 voidvoid *buffer = malloc(bufferSize);
73
74 size_t numBytesCrypted = 0;
75 CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt,
76 kCCAlgorithmAES128,
77 kCCOptionPKCS7Padding|kCCOptionECBMode,
78 keyPtr,
79 kCCBlockSizeAES128,
80 nil,
81 [data bytes],
82 dataLength,
83 buffer,
84 bufferSize,
85 &numBytesCrypted);
86 if (cryptStatus == kCCSuccess) {
87 NSData *resultData=[NSData dataWithBytesNoCopy:buffer length:numBytesCrypted];
88 NSString *result =[[NSString alloc]initWithData:resultData encoding:NSUTF8StringEncoding];
89 return result;
90 }
91 free(buffer);
92 return str;
93 }
94
95
96 + (NSString *)base64StringFromText:(NSString *)text
97 {
98 if (text && ![text isEqualToString:LocalStr_None]) {
99 NSData *data = [text dataUsingEncoding:NSUTF8StringEncoding];
100 return [self base64EncodedStringFrom:data];
101 }
102 else {
103 return LocalStr_None;
104 }
105 }
106
107 + (NSString *)textFromBase64String:(NSString *)base64
108 {
109 if (base64 && ![base64 isEqualToString:LocalStr_None]) {
110 NSData *data = [self dataWithBase64EncodedString:base64];
111 return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
112 }
113 else {
114 return LocalStr_None;
115 }
116 }
117
118 + (NSData *)dataWithBase64EncodedString:(NSString *)string
119 {
120 if (string == nil)
121 [NSException raise:NSInvalidArgumentException format:nil];
122 if ([string length] == 0)
123 return [NSData data];
124
125 static charchar *decodingTable = NULL;
126 if (decodingTable == NULL)
127 {
128 decodingTable = malloc(256);
129 if (decodingTable == NULL)
130 return nil;
131 memset(decodingTable, CHAR_MAX, 256);
132 NSUInteger i;
133 for (i = 0; i < 64; i++)
134 decodingTable[(short)encodingTable[i]] = i;
135 }
136
137 const charchar *characters = [string cStringUsingEncoding:NSASCIIStringEncoding];
138 if (characters == NULL) // Not an ASCII string!
139 return nil;
140 charchar *bytes = malloc((([string length] + 3) / 4) * 3);
141 if (bytes == NULL)
142 return nil;
143 NSUInteger length = 0;
144
145 NSUInteger i = 0;
146 while (YES)
147 {
148 char buffer[4];
149 short bufferLength;
150 for (bufferLength = 0; bufferLength < 4; i++)
151 {
152 if (characters[i] == '\0')
153 break;
154 if (isspace(characters[i]) || characters[i] == '=')
155 continue;
156 buffer[bufferLength] = decodingTable[(short)characters[i]];
157 if (buffer[bufferLength++] == CHAR_MAX) // Illegal character!
158 {
159 free(bytes);
160 return nil;
161 }
162 }
163
164 if (bufferLength == 0)
165 break;
166 if (bufferLength == 1) // At least two characters are needed to produce one byte!
167 {
168 free(bytes);
169 return nil;
170 }
171
172 // Decode the characters in the buffer to bytes.
173 bytes[length++] = (buffer[0] << 2) | (buffer[1] >> 4);
174 if (bufferLength > 2)
175 bytes[length++] = (buffer[1] << 4) | (buffer[2] >> 2);
176 if (bufferLength > 3)
177 bytes[length++] = (buffer[2] << 6) | buffer[3];
178 }
179
180 bytes = realloc(bytes, length);
181 return [NSData dataWithBytesNoCopy:bytes length:length];
182 }
183
184 + (NSString *)base64EncodedStringFrom:(NSData *)data
185 {
186 if ([data length] == 0)
187 return @"";
188
189 charchar *characters = malloc((([data length] + 2) / 3) * 4);
190 if (characters == NULL)
191 return nil;
192 NSUInteger length = 0;
193
194 NSUInteger i = 0;
195 while (i < [data length])
196 {
197 char buffer[3] = {0,0,0};
198 short bufferLength = 0;
199 while (bufferLength < 3 && i < [data length])
200 buffer[bufferLength++] = ((charchar *)[data bytes])[i++];
201
202 // Encode the bytes in the buffer to four characters, including padding "=" characters if necessary.
203 characters[length++] = encodingTable[(buffer[0] & 0xFC) >> 2];
204 characters[length++] = encodingTable[((buffer[0] & 0x03) << 4) | ((buffer[1] & 0xF0) >> 4)];
205 if (bufferLength > 1)
206 characters[length++] = encodingTable[((buffer[1] & 0x0F) << 2) | ((buffer[2] & 0xC0) >> 6)];
207 else characters[length++] = '=';
208 if (bufferLength > 2)
209 characters[length++] = encodingTable[buffer[2] & 0x3F];
210 else characters[length++] = '=';
211 }
212
213 return [[NSString alloc] initWithBytesNoCopy:characters length:length encoding:NSASCIIStringEncoding freeWhenDone:YES];
214 }
215
216 @end