iOS加密

作者: GreenB | 来源:发表于2018-06-30 11:52 被阅读6次

    1.MD5加密

    import "ViewController.h"

    import "MBProgressHUD.h"

    import "NSString+Hash.h"

    @interface ViewController ()
    @property (weak, nonatomic) IBOutlet UITextField *username;
    @property (weak, nonatomic) IBOutlet UITextField *pwd;

    • (IBAction)login;
      @end

    @implementation ViewController

    • (void)viewDidLoad
      {
      [super viewDidLoad];
      // Do any additional setup after loading the view, typically from a nib.
      }

    • (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
      {
      [self.view endEditing:YES];
      }

    • (IBAction)login {
      // 1.用户名
      NSString *usernameText = self.username.text;
      if (usernameText.length == 0) {
      [MBProgressHUD showError:@"请输入用户名"];
      return;
      }

      // 2.密码
      NSString *pwdText = self.pwd.text;
      if (pwdText.length == 0) {
      [MBProgressHUD showError:@"请输入密码"];
      return;
      }

      // 增加蒙板
      [MBProgressHUD showMessage:@"正在拼命登录中...."];

      // 3.发送用户名和密码给服务器(走HTTP协议)
      // 创建一个URL : 请求路径
      NSURL *url = [NSURL URLWithString:@"http://218.83.161.124:8080/job/login"];

      // 创建一个请求
      NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

      // 5秒后算请求超时(默认60s超时)
      request.timeoutInterval = 15;

      request.HTTPMethod = @"POST";

    warning 对pwdText进行加密

    pwdText = [self MD5Reorder:pwdText];
    
    // 设置请求体
    NSString *param = [NSString stringWithFormat:@"username=%@&pwd=%@", usernameText, pwdText];
    
    NSLog(@"%@", param);
    
    // NSString --> NSData
    request.HTTPBody = [param dataUsingEncoding:NSUTF8StringEncoding];
    
    // 设置请求头信息
    [request setValue:@"iPhone 6" forHTTPHeaderField:@"User-Agent"];
    
    // 发送一个同步请求(在主线程发送请求)
    // queue :存放completionHandler这个任务
    NSOperationQueue *queue = [NSOperationQueue mainQueue];
    [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:
     ^(NSURLResponse *response, NSData *data, NSError *connectionError) {
         // 隐藏蒙板
         [MBProgressHUD hideHUD];
    
        // 这个block会在请求完毕的时候自动调用
        if (connectionError || data == nil) { // 一般请求超时就会来到这
            [MBProgressHUD showError:@"请求失败"];
            return;
        }
    
        // 解析服务器返回的JSON数据
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
        NSString *error = dict[@"error"];
        if (error) {
            [MBProgressHUD showError:error];
        } else {
            NSString *success = dict[@"success"];
            [MBProgressHUD showSuccess:success];
        }
     }];
    

    }

    /**

    • MD5(pass.salt)
    • @param text 明文
    • @return 加密后的密文
      */
    • (NSString *)MD5Salt:(NSString *)text
      {
      // 撒盐:随机地往明文中插入任意字符串
      NSString *salt = [text stringByAppendingString:@"aaa"];
      return [salt md5String];
      }

    /**

    • MD5(MD5($pass))
    • @param text 明文
    • @return 加密后的密文
      */
    • (NSString *)doubleMD5:(NSString *)text
      {
      return [[text md5String] md5String];
      }

    /**

    • 先加密,后乱序
    • @param text 明文
    • @return 加密后的密文
      */
    • (NSString *)MD5Reorder:(NSString *)text
      {
      NSString *pwd = [text md5String];

      // 加密后pwd == 3f853778a951fd2cdf34dfd16504c5d8
      NSString *prefix = [pwd substringFromIndex:2];
      NSString *subfix = [pwd substringToIndex:2];

      // 乱序后 result == 853778a951fd2cdf34dfd16504c5d83f
      NSString *result = [prefix stringByAppendingString:subfix];

      NSLog(@"\ntext=%@\npwd=%@\nresult=%@", text, pwd, result);

      return result;
      }
      @end

    2.AES加密

    AES加密iOS代码加密使用方法

    // AES加密

    NSString *encryptedData = [AESCrypt encrypt:userName password:password];//加密
    
    NSString *message = [AESCrypt decrypt:encryptedData password:password]; //解密
    
    NSLog(@"加密结果 = %@",encryptedData);
    
    NSLog(@"解密结果 = %@",message);
    

    3.BASE64加密iOS代码加密添加如下方法

    .h

    • (NSString*)encodeBase64String:(NSString *)input;

    • (NSString*)decodeBase64String:(NSString *)input;

    • (NSString*)encodeBase64Data:(NSData *)data;

    • (NSString*)decodeBase64Data:(NSData *)data;

    2
    .m

    • (NSString*)encodeBase64String:(NSString * )input {

      NSData *data = [input dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];

      data = [GTMBase64 encodeData:data];

      NSString *base64String = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

      return base64String;

    }

    • (NSString*)decodeBase64String:(NSString * )input {

      NSData *data = [input dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];

      data = [GTMBase64 decodeData:data];

      NSString *base64String = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

      return base64String;

    }

    • (NSString*)encodeBase64Data:(NSData *)data {

      data = [GTMBase64 encodeData:data];

      NSString *base64String = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

      return base64String;

    }

    • (NSString*)decodeBase64Data:(NSData *)data {

      data = [GTMBase64 decodeData:data];

      NSString *base64String = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

      return base64String;

    }

    3.BASE64加密

    BASE64加密iOS代码加密使用方法

    // BASE64加密

    NSString *baseEncodeString = [GTMBase64 encodeBase64String:password];
    
    NSString *baseDecodeString = [GTMBase64 decodeBase64String:baseEncodeString];
    
    NSLog(@"baseEncodeString = %@",baseEncodeString);
    
    NSLog(@"baseDecodeString = %@",baseDecodeString);

    相关文章

      网友评论

          本文标题:iOS加密

          本文链接:https://www.haomeiwen.com/subject/yndtuftx.html