博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iphone AES加密
阅读量:5846 次
发布时间:2019-06-18

本文共 4263 字,大约阅读时间需要 14 分钟。

1 头文件   2   3 #import 
4 5 @interface NSData (AESTest) 6 -(NSData*)AES256EncryptWithKey:(NSString*)key; 7 -(NSData*)AES256DecryptWithKey:(NSString*)key; 8 9 @end 10 11 实现文件 12 13 #import
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

你可能感兴趣的文章
css 样式优先级计算
查看>>
英国公司利用人工智能酿啤酒 升华酿酒技巧
查看>>
区块链软件公司:区块链下新日子的遥想
查看>>
代理模式——结合SpringAOP讲解
查看>>
java版spring cloud+spring boot+redis多租户社交电子商务平(三)SpringBoot用JdbcTemplates访问Mysql...
查看>>
我怎样用Node.js自动完成工作的
查看>>
软RAID管理命令mdadm详解
查看>>
Bootstrap学习
查看>>
控制器 控制器view cell的关系
查看>>
Eclipse RCP 玩转 Spring
查看>>
我的友情链接
查看>>
Maven项目部署到tomcat(内嵌tomcat)
查看>>
Nginx的健康检查机制
查看>>
Nginx介绍及企业web服务软件选择
查看>>
计算机书籍备忘
查看>>
09-01-部署前端服务器-5-创建DNS记录
查看>>
esxi虚拟机中系统克隆及迁移的方法
查看>>
Linux必学的62条命令 (4)
查看>>
App_Offline.htm 功能
查看>>
maven-jetty-plugin配置
查看>>