1 // 2 // ViewController.m 3 // IOS_0129_HTTP請求 4 // 5 // Created by ma c on 16/1/29. 6 // Copyright © 2016年 博文科技. All rights reserved. 7 // 8 9 #import "ViewController.h" 10 #import "MBProgressHUD+MJ.h" 11 #import "NSString+Hash.h" 12 13 @interface ViewController () 14 @property (weak, nonatomic) IBOutlet UITextField *textName; 15 @property (weak, nonatomic) IBOutlet UITextField *textPassword; 16 17 - (IBAction)btnlogin; 18 19 @end 20 21 @implementation ViewController 22 23 /* 24 25 網絡安全 26 1.常見加密算法 27 MD5\SHA\DES\3DES\RC2和RC4\RSA\IDEA\DSA\AES 28 2.加密算法的選擇 29 通常公司都有本身的加密方案,按照公司的接口文檔的規定去加密 30 3.MD5 31 1>全稱:「Message Digest Algorithem5」,譯爲「消息摘要算法第五版」 32 2>效果:對輸入信息生成惟一的128位散列值(32個字符) 33 3>特色: 34 a.輸入兩個不一樣的明文不會獲得兩個相同的輸出值 35 b.根據輸出值,不會獲得原始的明文,即其過程不可逆 36 4>應用 37 a.MD5加密算法有較好的安全性,並且免費,所以該加密算法被普遍應用 38 b.主要運用在數字簽名、文件完整性驗證以及口令加密等方面 39 5>MD5解密網站:http://www.cmd5.com 40 41 2.網絡數據加密 42 1>加密對象:隱私數據,好比密碼,銀行信息 43 2>加密方案 44 a.提交隱私數據必須用POST請求 45 b.使用加密算法對隱私數據進行加密,好比MD5 46 3>加密加強:加大破解難度 47 a.對明文進行兩個MD5加密:MD5(MD5($pass)) 48 b.先對明文進行撒鹽,在進行MD5加密:MD5($pass.$salt) 49 50 3.本地存儲加密 51 1>加密對象:重要數據,好比單擊遊戲數據 52 53 4.代碼安全問題 54 1>如今已經有工具和技術能反編譯出源代碼:逆向工程 55 2>問題:如今反編譯出來的都是C語言代碼,可讀性不高 56 3>優勢:最起碼能知道源代碼裏面用的是那些框架 ->指導咱們開發不瞭解的軟件 57 4>解決方案:混淆代碼 58 59 */ 60 61 - (void)viewDidLoad { 62 [super viewDidLoad]; 63 64 self.view.backgroundColor = [UIColor groupTableViewBackgroundColor]; 65 } 66 67 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event 68 { 69 [self.view endEditing:YES]; 70 } 71 72 - (IBAction)btnlogin { 73 74 NSString *usernameText = self.textName.text; 75 if (usernameText.length == 0) { 76 [MBProgressHUD showError:@"請輸入帳號"]; 77 return; 78 } 79 80 self.textPassword.secureTextEntry = YES; 81 NSString *password = self.textPassword.text; 82 if (password.length == 0) { 83 [MBProgressHUD showError:@"請輸入密碼"]; 84 return; 85 } 86 // 增長蒙板 87 [MBProgressHUD showMessage:@"正在拼命加載..."]; 88 89 //2.POST請求 90 NSString *strURL = @"http://localhost:8080/MJServer/login"; 91 NSURL *url = [NSURL URLWithString:strURL]; 92 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 93 94 //5s後請求超時(默認60s超時) 95 request.timeoutInterval = 5; 96 //設置請求方式 97 request.HTTPMethod = @"POST"; 98 //設置請求頭 99 [request setValue:@"iPhone6" forHTTPHeaderField:@"User-Agent"]; 100 //對密碼進行加密 101 password = [self doubleMD5:password]; 102 password = [self MD5Salt:password]; 103 password = [self MD5Reorder:password]; 104 //設置請求體 105 NSString *param = [NSString stringWithFormat:@"username=%@&pwd=%@",usernameText,password]; 106 //NSString -> NSData 107 request.HTTPBody = [param dataUsingEncoding:NSUTF8StringEncoding]; 108 109 //異步請求 110 [self sendAsyncWithRequest:request]; 111 112 } 113 //異步請求 114 - (void)sendAsyncWithRequest:(NSURLRequest *)request 115 { 116 NSOperationQueue *queue = [NSOperationQueue mainQueue]; 117 118 [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) { 119 120 //隱藏蒙版 121 [MBProgressHUD hideHUD]; 122 NSHTTPURLResponse *resp = (NSHTTPURLResponse *)response; 123 NSString *msg = [NSHTTPURLResponse localizedStringForStatusCode:resp.statusCode]; 124 NSLog(@"%ld %@ %@",resp.statusCode, msg, resp.allHeaderFields); 125 126 //這個block會在請求完畢的時候自動調用 127 if (connectionError || data == nil) { 128 [MBProgressHUD showError:@"請求失敗"]; 129 return; 130 } 131 //解析服務器返回的JSON數據 132 NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil]; 133 NSString *error = dict[@"error"]; 134 if (error) { 135 [MBProgressHUD showError:error]; 136 } 137 else{ 138 NSString *success = dict[@"success"]; 139 [MBProgressHUD showSuccess:success]; 140 } 141 }]; 142 } 143 144 ///兩次加密 145 - (NSString *)doubleMD5:(NSString *)text 146 { 147 return [[text md5String] md5String]; 148 } 149 150 ///先撒鹽,在進行MD5加密 151 - (NSString *)MD5Salt:(NSString *)text 152 { 153 NSString *salt = [text stringByAppendingString:@"abc"]; 154 return [salt md5String]; 155 } 156 ///先加密,後亂序 157 - (NSString *)MD5Reorder:(NSString *)text 158 { 159 //加密 160 NSString *pwd = [text md5String]; 161 162 NSString *prefix = [pwd substringFromIndex:2]; 163 NSString *subfix = [pwd substringToIndex:2]; 164 165 //亂序 166 NSString *result = [prefix stringByAppendingString:subfix]; 167 168 return result; 169 } 170 171 @end