标签:cocoa touch 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加密.  (服务器简单存储加盐与加密保存过的就行了).  现实中的情况有公钥/私钥, 服务器并不是简单存储密码.
	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];
    // 每次都是一样的!例如:黑客拦截了路由器中的数据
    // 就能够获得到加密后的密码!
    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(@"=======");
}
【iOS】MD5加密与网络数据安全,布布扣,bubuko.com
标签:cocoa touch ios md5 数据安全 加密
原文地址:http://blog.csdn.net/xn4545945/article/details/36755117