iphone AES加密

  1 头文件
2
3 #import <Foundation/Foundation.h>
4
5 @interface NSData (AESTest)
6 -(NSData*)AES256EncryptWithKey:(NSString*)key;
7 -(NSData*)AES256DecryptWithKey:(NSString*)key;
8
9 @end
10
11 实现文件
12
13 #import <CommonCrypto/CommonCryptor.h>
14 #import "NSData.h"
15
16
17 @implementation NSData(AESAdditions)
18
19 -(NSData*)AES256EncryptWithKey:(NSString *)key{
20
21 // 'key' should be 32 bytes for AES256, will be null-padded otherwise
22 char keyPtr[kCCKeySizeAES256 + 1]; // room for terminator (unused)
23 bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding)
24
25 // fetch key data
26 [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];

27
28 NSUInteger dataLength = [self length];
29
30 //See the doc: For block ciphers, the output size will always be less than or
31 //equal to the input size plus the size of one block.
32 //That's why we need to add the size of one block here
33 size_t bufferSize = dataLength + kCCBlockSizeAES128;

34 void* buffer = malloc(bufferSize);
35
36 size_t numBytesEncrypted = 0;
37 CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
38 keyPtr, kCCKeySizeAES256,
39 NULL /* initialization vector (optional) */,
40 [self bytes], dataLength, /* input */
41 buffer, bufferSize, /* output */
42 &numBytesEncrypted);
43
44 if (cryptStatus == kCCSuccess)
45 {
46 //the returned NSData takes ownership of the buffer and will free it on deallocation
47 return [NSMutableData dataWithBytesNoCopy:buffer length:numBytesEncrypted];

48 }
49
50 free(buffer); //free the buffer;
51 return nil;

52 }
53
54
55
56
57 - (NSData*)AES256DecryptWithKey:(NSString*)key {
58 // 'key' should be 32 bytes for AES256, will be null-padded otherwise
59 char keyPtr[kCCKeySizeAES256 + 1]; // room for terminator (unused)
60 bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding)
61
62 // fetch key data
63 [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];

64
65 NSUInteger dataLength = [self length];
66
67 //See the doc: For block ciphers, the output size will always be less than or
68 //equal to the input size plus the size of one block.
69 //That's why we need to add the size of one block here
70 size_t bufferSize = dataLength + kCCBlockSizeAES128;

71 void* buffer = malloc(bufferSize);
72
73 size_t numBytesDecrypted = 0;
74 CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
75 keyPtr, kCCKeySizeAES256,
76 NULL /* initialization vector (optional) */,
77 [self bytes], dataLength, /* input */
78 buffer, bufferSize, /* output */
79 &numBytesDecrypted);
80
81 if (cryptStatus == kCCSuccess)
82 {
83 //the returned NSData takes ownership of the buffer and will free it on deallocation
84 return [NSMutableData dataWithBytesNoCopy:buffer length:numBytesDecrypted];

85 }
86
87 free(buffer); //free the buffer;
88 return nil;

89 }
90 @end
91
92
93
94 调用
95 - (void)loadView {
96
97
98 NSString *key = @"my password";
99 NSString *secret = @"text to encrypt";
100
101 NSData *plain = [secret dataUsingEncoding:NSUTF8StringEncoding];
102 NSData *cipher = [plain AES256EncryptWithKey:key];
103 printf("............%s\n", [[cipher description] UTF8String]);
104
105 plain = [cipher AES256DecryptWithKey:key];
106 printf("............%s\n", [[plain description] UTF8String]);
107 printf("............%s\n", [[[NSString alloc] initWithData:plain encoding:NSUTF8StringEncoding] UTF8String]);
108
109

转载于:https://www.cnblogs.com/pengyingh/articles/2341920.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值