- (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(@"======="); }