iOS加密

2018-06-30  本文已影响6人  GreenB

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;

@implementation ViewController

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];
    }
 }];

}

/**

/**

/**

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

2
.m

}

}

}

}

3.BASE64加密

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

// BASE64加密

NSString *baseEncodeString = [GTMBase64 encodeBase64String:password];

NSString *baseDecodeString = [GTMBase64 decodeBase64String:baseEncodeString];

NSLog(@"baseEncodeString = %@",baseEncodeString);

NSLog(@"baseDecodeString = %@",baseDecodeString);
上一篇下一篇

猜你喜欢

热点阅读