iOS开发网络篇—数据安全

在做网络应用程序的时候, 时时刻刻要保证用户数据的安全, 因此要加密。
MD5算法在国内用的很多. 
 
MD5算法的特点
*同样的数据加密结果是一样的.(32个字符)
*不可逆的.(不能逆向解密)
*可用于文件校验/指纹识别.
 
MD5算法是公开的,iOS中已经包装好了MD5算法。
可以将其写成字符串的分类:
 
[objc] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. - (NSString *)md5String  
  2. {  
  3.     const charchar *string = self.UTF8String;  
  4.     int length = (int)strlen(string);  
  5.     unsigned char bytes[CC_MD5_DIGEST_LENGTH];  
  6.     CC_MD5(string, length, bytes);  
  7.     return [self stringFromBytes:bytes length:CC_MD5_DIGEST_LENGTH];  
  8. }  


在iOS程序中对用户的登录数据进行加密存储非常重要。做到,即使数据被劫持,也无法还原出原始数据的地步。
 
一、普通MD5加密
 
太简单的MD5加密很容易被破解。一般在进行MD5加密时会使用“加佐料”的方法。
 
简单的MD5可到这个网站进行破解:www.cmd5.com
 
下面是进行MD5加密的方法: 其中token即为加的字符串,可以为任意长度的奇形怪状字符串。
[objc] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. - (IBAction)login:(UIButton *)sender {  
  2.     [self postLogin];  
  3. }  
  4.   
  5. /**提交用户数据的时候用post相对安全. 同时将用户数据转换成模型最好*/  
  6. - (void)postLogin {  
  7.     //1.URL  
  8.     NSString *urlStr = [NSString stringWithFormat:@"http://localhost/login.php"];  
  9.     NSURL *url = [NSURL URLWithString:urlStr];  
  10.     //2.建立 Mutablerequest  
  11.     NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];  
  12.   
  13.     //3.设置  
  14.     request.HTTPMethod = @"POST";  
  15.     //请求体可在firebug中找  
  16.   
  17.     NSString *pwd = self.userPwd.text;  
  18.     //先加盐, 用MD5加密.  (服务器简单存储加盐与加密保存过的就行了).  现实中的情况有公钥/私钥, 服务器并不是简单存储密码.  
  19.     pwd = [pwd stringByAppendingString:token];  
  20.     pwd = [pwd md5String];  
  21.     NSLog(@"%@", pwd);  
  22.   
  23.     NSString *body = [NSString stringWithFormat:@"username=%@&password=%@", self.userName.text, pwd];  
  24.     request.HTTPBody = [body dataUsingEncoding:NSUTF8StringEncoding];  
  25.   
  26.     //4.建立连接. (data即为取到的数据, 和get一样)  
  27.     [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler: ^(NSURLResponse *response, NSData *data, NSError *connectionError) {  
  28.         NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];  
  29.         NSLog(@"%@, %@", [NSThread currentThread], str);  
  30.   
  31.         //更新显示需要在主线程中  
  32.         [[NSOperationQueue mainQueue] addOperationWithBlock: ^{  
  33.             self.label.text = str;  
  34.             NSLog(@"%@, %@", [NSThread currentThread], str);  
  35.         }];  
  36.     }];  
  37. }  

二、更加高级的方法
 
用公钥和私钥的概念。
 
一个公钥(都知道),一个私钥(只有服务器自己知道).密码要动态变化才行.
*用户:用token+时间进行加密,传送给服务器
*服务器:  取出用户密码(存储时用私钥加过密),用时间+公钥等与客户端发送的密码进行比较.(服务器还要检查发送密码的时间差,1分钟以内)
 
详细见注释:摘自老刘。
 
[objc] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. - (IBAction)login:(id)sender  
  2. {  
  3.     NSString *pwd = self.pwdText.text;  
  4.     // 进行MD5加密  
  5.     pwd = [pwd stringByAppendingString:token];  
  6.     // 每次都是一样的!例如:黑客拦截了路由器中的数据  
  7.     // 就能够获得到加密后的密码!  
  8.     pwd = [pwd md5String];  
  9.       
  10.     // 在服务器后台,保存的是用私有密钥加盐处理的MD5密码串  
  11.     pwd = [NSString stringWithFormat:@"%@%@%@", pwd, publicKey, @"2014062914:14:30"];  
  12.     // 利用日期,可以保证加密生成的字符串不一样  
  13.     pwd = [pwd md5String];  
  14.       
  15.     // 提交给服务器的内容:新的密码,生成密码的事件、  
  16.     /** 
  17.      服务器的处理: 
  18.       
  19.      1. 从服务器取出用户的密码(是用私有密钥加密的) 
  20.      2. 服务器知道共有密钥,根据给定的时间(动态生成新的密码),与客户端提交的密码进行比较 
  21.      3. 服务器同时需要检查提交密码的事件差值,跟客户端提交的日期偏差在1分钟之内。 
  22.      */  
  23.     NSLog(@"%@", pwd);  
  24.       
  25.     [self postLogonWithUserName:self.userNameText.text password:pwd];  
  26. }  
  27.   
  28. #pragma mark - POST登录  
  29. - (void)postLogonWithUserName:(NSString *)userName password:(NSString *)password  
  30. {  
  31.     // 1. url  
  32.     NSString *urlStr = @"http://192.168.25.2/login.php";  
  33.     NSURL *url = [NSURL URLWithString:urlStr];  
  34.       
  35.     // 2. request,POST方法,需要建立一个可变的请求  
  36.     NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];  
  37.       
  38.     // 1> POST 方法,所有涉及用户隐私的数据传递,都需要用POST方式提交!  
  39.     request.HTTPMethod = @"POST";  
  40.       
  41.     // 2> 数据体  
  42.     NSString *bodyStr = [NSString stringWithFormat:@"username=%@&password=%@", userName, password];  
  43.       
  44.     // 将字符串转换成二进制数据  
  45.     request.HTTPBody = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];  
  46.       
  47.     // 3. 发送“异步”请求,在其他线程工作,不阻塞当前线程程序执行  
  48.     [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {  
  49.   
  50.         // 1> JSON,格式是和NSDictionary的快速包装格式非常  
  51.         // 将JSON转换成字典 Serialization  
  52.         NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:1 error:NULL];  
  53.           
  54.         CZUserInfo *userInfo = [CZUserInfo userInfoWithDict:dict];  
  55.           
  56.         NSLog(@"%@ %@", userInfo.userId, userInfo.userName);  
  57.     }];  
  58.       
  59.     NSLog(@"=======");  
  60. }  


 

转载请注明出处:http://blog.csdn.net/xn4545945  

原文地址:https://www.cnblogs.com/iosblogx/p/4474600.html