【iOS】MD5数据加密和网络安全
- (NSString *)md5String
{
const char *string = self.UTF8String;
int length = (int)strlen(string);
unsigned char bytes[CC_MD5_DIGEST_LENGTH];
CC_MD5(string, length, bytes);
return [self stringFromBytes:bytes length:CC_MD5_DIGEST_LENGTH];
}
做到。即使数据被劫持。也无法还原出原始数据的地步。
- (IBAction)login:(UIButton *)sender {
[self postLogin];
}
/**提交用户数据的时候用post相对安全. 同一时候将用户数据转换成模型最好*/
- (void)postLogin {
//1.URL
NSString *urlStr = [NSString stringWithFormat:@"http://localhost/login.php"];
NSURL *url = [NSURL URLWithString:urlStr];
//2.建立 Mutablerequest
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
//3.设置
request.HTTPMethod = @"POST";
//请求体可在firebug中找
NSString *pwd = self.userPwd.text;
//先加盐, 用MD5加密. (server简单存储加盐与加密保存过的即可了). 现实中的情况有公钥/私钥, server并非简单存储密码.
pwd = [pwd stringByAppendingString:token];
pwd = [pwd md5String];
NSLog(@"%@", pwd);
NSString *body = [NSString stringWithFormat:@"username=%@&password=%@", self.userName.text, pwd];
request.HTTPBody = [body dataUsingEncoding:NSUTF8StringEncoding];
//4.建立连接. (data即为取到的数据, 和get一样)
[NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler: ^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@, %@", [NSThread currentThread], str);
//更新显示须要在主线程中
[[NSOperationQueue mainQueue] addOperationWithBlock: ^{
self.label.text = str;
NSLog(@"%@, %@", [NSThread currentThread], str);
}];
}];
}
二、更加高级的方法
- (IBAction)login:(id)sender
{
NSString *pwd = self.pwdText.text;
// 进行MD5加密
pwd = [pwd stringByAppendingString:token];
// 每次都是一样的! 比如:黑客拦截了路由器中的数据
// 就行获得到加密后的password! pwd = [pwd md5String]; // 在server后台,保存的是用私有密钥加盐处理的MD5password串
pwd = [NSString stringWithFormat:@"%@%@%@", pwd, publicKey, @"2014062914:14:30"];
// 利用日期,可以保证加密生成的字符串不一样
pwd = [pwd md5String]; // 提交给server的内容:新的password,生成password的事件、
/**
server的处理: 1. 从server取出用户的password(是用私有密钥加密的)
2. server知道共同拥有密钥。依据给定的时间(动态生成新的password)。与client提交的password进行比較
3. server同一时候须要检查提交password的事件差值。跟client提交的日期偏差在1分钟之内。 */
NSLog(@"%@", pwd); [self postLogonWithUserName:self.userNameText.text password:pwd];
} #pragma mark - POST登录
- (void)postLogonWithUserName:(NSString *)userName password:(NSString *)password
{
// 1. url
NSString *urlStr = @"http://192.168.25.2/login.php";
NSURL *url = [NSURL URLWithString:urlStr]; // 2. request,POST方法。须要建立一个可变的请求
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; // 1> POST 方法,全部涉及用户隐私的数据传递,都须要用POST方式提交!
request.HTTPMethod = @"POST"; // 2> 数据体
NSString *bodyStr = [NSString stringWithFormat:@"username=%@&password=%@", userName, password]; // 将字符串转换成二进制数据
request.HTTPBody = [bodyStr dataUsingEncoding:NSUTF8StringEncoding]; // 3. 发送“异步”请求。在其它线程工作。不堵塞当前线程程序运行
[NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { // 1> JSON,格式是和NSDictionary的高速包装格式很
// 将JSON转换成字典 Serialization
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:1 error:NULL]; CZUserInfo *userInfo = [CZUserInfo userInfoWithDict:dict]; NSLog(@"%@ %@", userInfo.userId, userInfo.userName);
}]; NSLog(@"=======");
}
转载请注明出处:http://blog.csdn.net/xn4545945
【iOS】MD5数据加密和网络安全的更多相关文章
- IOS关于数据加密(主要为登录加密)想总结的
首先上来就来说一下,IOS常见的几种加密算法 *哈希(散列)函数 : MD5.SHA *对称加密算法:DES.3DES.AES *非对称加密算法:RSA 一.哈希(散列)函数 1.MD5 MD ...
- iOS MD5加密
1.MD5加密 Message Digest Algorithm MD5(中文名为消息摘要算法第五版)为计算机安全领域广泛使用的一种散列函数,用以提供消息的完整性保护.该算法的文件号为RFC 1321 ...
- Java工作流引擎关于数据加密流程(MD5数据加密防篡改)
关键字: 驰骋工作流程快速开发平台 工作流程管理系统 工作流引擎 asp.net工作流引擎 java工作流引擎. 开发者表单 拖拽式表单 工作流系统 流程数据加密 md5 数据保密流程数据防篡改 ...
- iOS MD5加密实现方法
使用方法 先导入头文件 #import "MD5.h" //md5测试 NSString * md1= [MD5 md5:" ]; NSLog(@"32bit= ...
- iOS MD5加密字符串
参考:http://stackoverflow.com/questions/1524604/md5-algorithm-in-objective-c 在线测试MD5:http://www.cmd5.c ...
- iOS MD5加密算法
考虑到用户账户安全,对用户的登录密码进行MD5加密 什么是MD5加密呢...懒了就不在这搬砖了,大家可以自己搜索查查,在此记录下代码,以供以后学习查询! 下面,直接上代码... // 需要倒入这个头文 ...
- iOS MD5 (Swift3)
import Foundation extension Int { func hexedString() -> String { return NSString(format:"%02 ...
- ios MD5大小写加密
#import "NSString+change.h" #import <CommonCrypto/CommonDigest.h> @implementation NS ...
- iOS,一行代码进行RSA、DES 、AES、MD5加密、解密
本文为投稿文章,作者:Flying_Einstein(简书) 加密的Demo,欢迎下载 JAVA端的加密解密,读者可以看我同事的这篇文章:http://www.jianshu.com/p/98569e ...
随机推荐
- uip UDPclient模式通信移植,p本地ort可以是无规
现在移植UDPclient模式,使用广播地址检测. //udp_client.c /********************************************************** ...
- 分析java.lang.NullPointerException thrown in RelativeLayout measure()
典型的再现环境 模型: Sony Ericsson Android version: 2.3.4 StackTrace: E/AndroidRuntime( 3579): FATAL EXCEPTIO ...
- jquey :eq(1)
$("#div_Goods .datagrid-row .numberbox:eq(1)") $("#div_Goods .datagrid-row .numberbox ...
- Java 并发专题 : CyclicBarrier 打造一个安全的门禁系统
继续并发专题~ 这次介绍CyclicBarrier:看一眼API的注释: /** * A synchronization aid that allows a set of threads to all ...
- ZeroBrane Studio远程调试Lua程序(转)
环境: ZeroBrane Studio安装在Windows 7上,而要调试的程序运行在CentOS上: 设置: 在windows 7上,打开ZeroBrane Studio,打开需要调试的文件,例如 ...
- J2SE习题(2)
第四.五周练习题 1.a. Define a class called BlogEntry that could be used to store an entry for a Weblog. Th ...
- TimesTen更改CacheGroup管理用户ORACLE结束和TT结束password【TimesTen操作和维修基地】
password管理一直操作的一部分的安全管理和维护.CacheGroup管理用户password虽然并不复杂变化.然而,这是用于生产,改不好比较easy导致失败.简介点击这里CacheGroup管理 ...
- response.setHeader各种使用方法
一秒刷新页面一次 response.setHeader("refresh","1"); 二秒跳到其它页面 response.setHeader("re ...
- Xampp mysql无法启动的解决方案(转)
如果出现mysql 无法启动表明在安装xampp 前已经安装了mysql,造成mysql服务无法启动. [mysql] MySQL Service detected with wrong path23 ...
- 香蕉派路由功Openwrt、Android功耗对照測试
路由这个东西是要长期通电使用的,所以功耗也是须要关注的.如今香蕉派路由已经有了openwrt和android两个 系统,这两个系统的功耗是否一样呢? 測试工具:QUIGG的德国产功耗測试仪一个.手机充 ...