URL加解密

背景介绍

加密方式

  • 加密:首先对字符串记性AES128加密,然后进行base64加密(主要是为了去除特殊字符)
  • 解密:先base64解密,然后在AES128解密即可还原数据
  • 其中base64加解密使用 GTMBase64添加两个方法
+ (NSString*)encodeBase64Data:(NSData *)data {
data = [GTMBase64 encodeData:data];
NSString *base64String = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
return base64String;
} + (NSData*)decodeBase64String:(NSString * )input {
NSData *data = [input dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
data = [GTMBase64 decodeData:data];
return data;
}

AES128使用系统CommonCrypto/CommonCryptor.h实现 //用于AES

添加NSData分类,增加两个方法

#pragma mark - AES128位加解密
- (NSData *)AES128EncryptWithKey:(NSString *)key { char keyPtr[kCCKeySizeAES128 + 1];
memset(keyPtr, 0, sizeof(keyPtr));
[key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding]; NSUInteger dataLength = [self length]; size_t bufferSize = dataLength + kCCBlockSizeAES128;
void *buffer = malloc(bufferSize); size_t numBytesEncrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt,
kCCAlgorithmAES,
kCCOptionPKCS7Padding|kCCOptionECBMode,
keyPtr,
kCCBlockSizeAES128,
NULL /* initialization vector (optional) */,
[self bytes],
dataLength, /* input */
buffer,
bufferSize, /* output */
&numBytesEncrypted);
if (cryptStatus == kCCSuccess) {
//the returned NSData takes ownership of the buffer and will free it on deallocation
return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
} free(buffer); //free the buffer;
return nil;
} - (NSData *)AES128DecryptWithKey:(NSString *)key {
// 'key' should be 32 bytes for AES256, will be null-padded otherwise
char keyPtr[kCCKeySizeAES128+1]; // room for terminator (unused)
bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding) // fetch key data
[key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding]; NSUInteger dataLength = [self length]; //See the doc: For block ciphers, the output size will always be less than or
//equal to the input size plus the size of one block.
//That's why we need to add the size of one block here
size_t bufferSize = dataLength + kCCBlockSizeAES128;
void *buffer = malloc(bufferSize); size_t numBytesDecrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt,
kCCAlgorithmAES128,
kCCOptionPKCS7Padding|kCCOptionECBMode,
keyPtr,
kCCBlockSizeAES128,
NULL /* initialization vector (optional) */,
[self bytes],
dataLength, /* input */
buffer,
bufferSize, /* output */
&numBytesDecrypted); if (cryptStatus == kCCSuccess) {
//the returned NSData takes ownership of the buffer and will free it on deallocation
return [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted];
} free(buffer); //free the buffer;
return nil;
}

直奔主题

  • AFNetworking 的post请求如下
- (AFHTTPRequestOperation *)POST:(NSString *)URLString
parameters:(id)parameters
constructingBodyWithBlock:(void (^)(id <AFMultipartFormData> formData))block
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
  • 其中URLString可以作为最基础的,不需要加密

    parameters 就是我们需要加密的地方,这是一个字典,因为AFN会对这个parameters进行解析,所以对这个参数集合进行一次包装,拼接成一个字符串。然后对字符串进行加密。

原来的代码是这样请求数据的

NSMutableDictionary *para = [NSMutableDictionary dictionary];
para[@"method"] = @"securityAdd";
para[@"userId"] = userId;
para[@"userPsw"] = userPsw;
para[@"content"] = @"ddddd123891237"; NSString *url = [NSString stringWithFormat:@"https://%@:82/frame/webInteface.do?", NHBaseURL];
AFHTTPRequestOperation *operation = [NetWorkInst POST:url parameters:dict constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
} success:^(AFHTTPRequestOperation *operation, id responseObject) { }];

加密后代码是这样的

NSMutableDictionary *para = [NSMutableDictionary dictionary];
para[@"method"] = @"securityAdd";
para[@"userId"] = userId;
para[@"userPsw"] = userPsw;
para[@"content"] = @"ddddd123891237"; // 开始加密,格式化数据****************************
NSString *str = [NSString stringWithFormat:@"'method':'securityAdd','userId':'%@','userPsw':'%@','content':'%@'",userId,userPsw,content];
NSLog(@"原始数据:%@",str);
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
NSData *aaa = [data AES128EncryptWithKey:@"song.com"]; // aes加密
NSLog(@"加密AES128后:%@",aaa);
NSString *bbb = [PublicMethod encodeBase64Data:aaa];//base64加密
NSLog(@"base64加密后:%@",bbb); NSMutableDictionary *dict = [NSMutableDictionary dictionary];
dict[@"info"] = bbb; // 开始解密****************************
NSData *da = [PublicMethod decodeBase64String:bbb]; //base64解密
NSString *ccc = [[NSString alloc] initWithData:da encoding:NSUTF8StringEncoding];
NSLog(@"base64解密后:%@",ccc);
NSData *ddd = [da AES128DecryptWithKey:@"song.com"];// aes解密
NSString *eee = [[NSString alloc] initWithData:ddd encoding:NSUTF8StringEncoding];
NSLog(@"解密AES128后:%@",eee); NSString *url = [NSString stringWithFormat:@"https://%@:82/frame/webInteface.do?", NHBaseURL];
AFHTTPRequestOperation *operation = [NetWorkInst POST:url parameters:dict constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
} success:^(AFHTTPRequestOperation *operation, id responseObject) { }];

欢迎关注公众号

最新文章

  1. geotrellis使用(二十七)栅格数据色彩渲染
  2. 整理文件,翻出了以前作的ps稿 (^o^)c旦``
  3. A Simple OpenGL Shader Example
  4. AP(affinity propagation)研究
  5. fastjson解析json,model字段有顺序要求吗
  6. MySQL数据库my.cnf性能参数如何调优
  7. Java多线程学习(转载)
  8. Java开发中文件读取方式总结
  9. jquery动态添加元素无法触发绑定事件的解决方案。
  10. putty修改编码
  11. 使用logrotate分割tomcat日志
  12. Nginx前端设置反向代理,后端Apache如何获取访客的真实IP,结合PHP
  13. 【Oracle RAC】Linux系统Oracle11gR2 RAC安装配置详细过程V3.1(图文并茂)
  14. 精确率、准确率、召回率和F1值
  15. asp.net后台获取前台页面大小
  16. jenkins shell脚本自动化构建阿里云k8s上应用
  17. PAT 1035 插入与归并(25)(代码+思路+测试点分析)
  18. linux下日语语言包安装
  19. 项目Beta冲刺(团队)总结
  20. nginx文件访问403问题

热门文章

  1. POJ Treasure Exploration 【DAG交叉最小路径覆盖】
  2. Android学习笔记_38_图片的拖动、缩放功能和多点触摸
  3. 用HTML写伪类选择器,结构伪类选择器,伪元素选择器样式
  4. BFC、IFC、GFC、FFC
  5. ATK 设计框架辅助工具-代码生成器
  6. 优雅的QSignleton (三) 通过属性器实现Singleton
  7. Swift_Set详解
  8. Myeclipse项目移植到eclipse
  9. css选择器有哪些
  10. LeetCode 中级 - 翻转矩阵后的得分(861)