您的位置:首页 > 理论基础 > 计算机网络

【iOS】MD5加密与网络数据安全

2016-06-26 22:09 369 查看
在做网络应用程序的时候, 时时刻刻要保证用户数据的安全, 因此要加密。

MD5算法在国内用的很多. 

MD5算法的特点

*同样的数据加密结果是一样的.(32个字符)

*不可逆的.(不能逆向解密)

*可用于文件校验/指纹识别.

MD5算法是公开的,iOS中已经包装好了MD5算法。

可以将其写成字符串的分类:

[objc] view
plaincopy





- (NSString *)md5String  

{  

    const charchar *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];  

}  

在iOS程序中对用户的登录数据进行加密存储非常重要。做到,即使数据被劫持,也无法还原出原始数据的地步。

一、普通MD5加密

太简单的MD5加密很容易被破解。一般在进行MD5加密时会使用“加佐料”的方法。

简单的MD5可到这个网站进行破解:www.cmd5.com

下面是进行MD5加密的方法: 其中token即为加的字符串,可以为任意长度的奇形怪状字符串。

[objc] view
plaincopy





- (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加密.  (服务器简单存储加盐与加密保存过的就行了).  现实中的情况有公钥/私钥, 服务器并不是简单存储密码.  

    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);  

        }];  

    }];  

}  

二、更加高级的方法

用公钥和私钥的概念。

一个公钥(都知道),一个私钥(只有服务器自己知道).密码要动态变化才行.

*用户:用token+时间进行加密,传送给服务器

*服务器:  取出用户密码(存储时用私钥加过密),用时间+公钥等与客户端发送的密码进行比较.(服务器还要检查发送密码的时间差,1分钟以内)

详细见注释:摘自老刘。

[objc] view
plaincopy





- (IBAction)login:(id)sender  

{  

    NSString *pwd = self.pwdText.text;  

    // 进行MD5加密  

    pwd = [pwd stringByAppendingString:token];  

    // 每次都是一样的!例如:黑客拦截了路由器中的数据  

    // 就能够获得到加密后的密码!  

    pwd = [pwd md5String];  

      

    // 在服务器后台,保存的是用私有密钥加盐处理的MD5密码串  

    pwd = [NSString stringWithFormat:@"%@%@%@", pwd, publicKey, @"2014062914:14:30"];  

    // 利用日期,可以保证加密生成的字符串不一样  

    pwd = [pwd md5String];  

      

    // 提交给服务器的内容:新的密码,生成密码的事件、  

    /** 

     服务器的处理: 

      

     1. 从服务器取出用户的密码(是用私有密钥加密的) 

     2. 服务器知道共有密钥,根据给定的时间(动态生成新的密码),与客户端提交的密码进行比较 

     3. 服务器同时需要检查提交密码的事件差值,跟客户端提交的日期偏差在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/article/details/36755117
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: