iPhone开发技巧之数据篇(2)— iPhone程序中的加密处理
CCCrypt
在Objective-C语言中,加密时常用到CCCrypt(3cc) Mac OS X Manual Pagehttp://developer.apple.com/library/mac/#DOCUMENTATION/Darwin/Reference/ManPages/man3/CCCrypt.3cc.html 。下面的代码使用 CCCrypt() ,给 NSData 类增加了 AES256EncryptWithKey, AES256DecryptWithKey 方法。
1. #import <CommonCrypto/CommonCryptor.h>
2.
3. @implementation NSData (Additions)
4.
5. @class NSString;
6.
7. - (NSData *)AES256EncryptWithKey:(NSString *)key {
8. char keyPtr[kCCKeySizeAES256+1];
9. bzero(keyPtr, sizeof(keyPtr));
10.
11. [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
12.
13. NSUInteger dataLength = [self length];
14.
15. size_t bufferSize = dataLength + kCCBlockSizeAES128;
16. void *buffer = malloc(bufferSize);
17.
18. size_t numBytesEncrypted = 0;
19. CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128,
20. kCCOptionPKCS7Padding | kCCOptionECBMode,
21. keyPtr, kCCBlockSizeAES128,
22. NULL,
23. [self bytes], dataLength,
24. buffer, bufferSize,
25. &numBytesEncrypted);
26. if (cryptStatus == kCCSuccess) {
27. return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
28. }
29. free(buffer);
30. return nil;
31. }
32.
33. - (NSData *)AES256DecryptWithKey:(NSString *)key {
34. char keyPtr[kCCKeySizeAES256+1];
35. bzero(keyPtr, sizeof(keyPtr));
36.
37. [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
38.
39. NSUInteger dataLength = [self length];
40.
41. size_t bufferSize = dataLength + kCCBlockSizeAES128;
42. void *buffer = malloc(bufferSize);
43.
44. size_t numBytesDecrypted = 0;
45. CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES128,
46. kCCOptionPKCS7Padding | kCCOptionECBMode,
47. keyPtr, kCCBlockSizeAES128,
48. NULL,
49. [self bytes], dataLength,
50. buffer, bufferSize,
51. &numBytesDecrypted);
52.
53. if (cryptStatus == kCCSuccess) {
54. return [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted];
55. }
56. free(buffer);
57. return nil;
58. }
59. @end
使用的时候就像使用NSData一样。base64encoding有的时候需要向服务器上传递base64加密的数据,这个时候我们就可以像下面,给 NSData 类追加newStringInBase64FromData 方法。
1. - (NSString *)newStringInBase64FromData {
2. NSMutableString *dest = [[NSMutableString alloc] initWithString:@""];
3. unsigned char * working = (unsigned char *)[self bytes];
4. int srcLen = [self length];
5.
6. for (int i=0; i<srcLen; i += 3) {
7. for (int nib=0; nib<4; nib++) {
8. int byt = (nib == 0)?0:nib-1;
9. int ix = (nib+1)*2;
10.
11. if (i+byt >= srcLen) break;
12.
13. unsigned char curr = ((working[i+byt] << (8-ix)) & 0x3F);
14.
15. if (i+nib < srcLen) curr |= ((working[i+nib] >> ix) & 0x3F);
16.
17. [dest appendFormat:@"%c", base64[curr]];
18. }
19. }
20. return dest;
21. }
同样,使用的时候就像使用NSData一样。
最终的代码如下所示 :
1. //
2. // NSDataAdditions.h
3. //
4. // Copyright 2010 __MyCompanyName__. All rights reserved.
5. //
6.
7. #import <Foundation/Foundation.h>
8.
9. @class NSString;
10.
11. @interface NSData (Additions)
12.
13. - (NSData *)AES256EncryptWithKey:(NSString *)key;
14. - (NSData *)AES256DecryptWithKey:(NSString *)key;
15. - (NSString *)newStringInBase64FromData;
16.
17. @end
18.
19. // -----------------------------------------------------------------------------------
20.
21. //
22. // NSDataAdditions.m
23. //
24. // Copyright 2010 __MyCompanyName__. All rights reserved.
25. //
26.
27. #import "NSDataAdditions.h"
28. #import <CommonCrypto/CommonCryptor.h>
29.
30. static char base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
31.
32. @implementation NSData (Additions)
33.
34. @class NSString;
35.
36. - (NSData *)AES256EncryptWithKey:(NSString *)key {
37. char keyPtr[kCCKeySizeAES256+1];
38. bzero(keyPtr, sizeof(keyPtr));
39.
40. [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
41.
42. NSUInteger dataLength = [self length];
43.
44. size_t bufferSize = dataLength + kCCBlockSizeAES128;
45. void *buffer = malloc(bufferSize);
46.
47. size_t numBytesEncrypted = 0;
48. CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128,
49. kCCOptionPKCS7Padding | kCCOptionECBMode,
50. keyPtr, kCCBlockSizeAES128,
51. NULL,
52. [self bytes], dataLength,
53. buffer, bufferSize,
54. &numBytesEncrypted);
55. if (cryptStatus == kCCSuccess) {
56. return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
57. }
58.
59. free(buffer);
60. return nil;
61. }
62.
63. - (NSData *)AES256DecryptWithKey:(NSString *)key {
64. char keyPtr[kCCKeySizeAES256+1];
65. bzero(keyPtr, sizeof(keyPtr));
66.
67. [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
68.
69. NSUInteger dataLength = [self length];
70.
71. size_t bufferSize = dataLength + kCCBlockSizeAES128;
72. void *buffer = malloc(bufferSize);
73.
74. size_t numBytesDecrypted = 0;
75. CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES128,
76. kCCOptionPKCS7Padding | kCCOptionECBMode,
77. keyPtr, kCCBlockSizeAES128,
78. NULL,
79. [self bytes], dataLength,
80. buffer, bufferSize,
81. &numBytesDecrypted);
82.
83. if (cryptStatus == kCCSuccess) {
84. return [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted];
85. }
86.
87. free(buffer);
88. return nil;
89. }
90.
91. - (NSString *)newStringInBase64FromData {
92. NSMutableString *dest = [[NSMutableString alloc] initWithString:@""];
93. unsigned char * working = (unsigned char *)[self bytes];
94. int srcLen = [self length];
95.
96. for (int i=0; i<srcLen; i += 3) {
97. for (int nib=0; nib<4; nib++) {
98. int byt = (nib == 0)?0:nib-1;
99. int ix = (nib+1)*2;
100.
101. if (i+byt >= srcLen) break;
102.
103. unsigned char curr = ((working[i+byt] << (8-ix)) & 0x3F);
104.
105. if (i+nib < srcLen) curr |= ((working[i+nib] >> ix) & 0x3F);
106.
107. [dest appendFormat:@"%c", base64[curr]];
108. }
109. }
110.
111. return dest;
112. }
113.
114. @end
相关新闻>>
- 发表评论
-
- 最新评论 更多>>