隐私数据的处理

用户访问网络免不了将一些数据上传到服务器,

可能是一些图片,也有可能是账号密码,

如何将这些数据进行处理,才能保证安全?

仅仅用POST请求提交用户的隐私数据,还是不能完全解决安全问题
可以利用软件(比如Charles)设置代理服务器,拦截查看手机的请求数据
因此:提交用户的隐私数据时,一定不要明文提交,要加密处理后再提交
 
常见的加密算法
MD5 SHA DES 3DES RC2和RC4 RSA IDEA DSA AES
加密算法的选择
一般公司都会有一套自己的加密方案,按照公司接口文档的规定去加密
 

一.利用算法加密(以MD5为例)

什么是MD5
全称是Message Digest Algorithm 5,译为“消息摘要算法第5版”
效果:对输入信息生成唯一的128位散列值(32个字符)
MD5的特点
输入两个不同的明文不会得到相同的输出值
根据输出值,不能得到原始的明文,即其过程不可逆

包含了苹果的原生库

#import <CommonCrypto/CommonDigest.h>

#import <CommonCrypto/CommonHMAC.h>

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

- (NSString *)stringFromBytes:(unsigned char *)bytes length:(int)length
{
	NSMutableString *mutableString = @"".mutableCopy;
	for (int i = 0; i < length; i++)
		[mutableString appendFormat:@"%02x", bytes[i]];
	return [NSString stringWithString:mutableString];
}

二.使用https协议

在URL前加https://前缀表明使用SSL加密的,你的电脑与服务器之间收发的信息传输将更加安全

Web服务器启用SSL需要获得一个服务器证书并将该证书与要使用SSL的服务器绑定

http和https使用的而是完全不同的连接方式,用的端口也不一样,前者是80,后者是443.

http的连接很简单,是无状态的

HTTPS协议是由SSL+HTTP协议构建的可进行加密传输,身份认证的网络协议要比http协议安全

SSL(安全套接层)

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //实现代理
    NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[[NSOperationQueue alloc] init]];
    
    NSURLSessionDataTask *task = [session dataTaskWithURL:[NSURL URLWithString:@"https://www.apple.com/cn/"] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        
        NSLog(@"%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
    }];
    
    [task resume];
}
#pragma mark - <NSURLSessionTaskDelegate>

/**
 * challenge :挑战,质询
 * completionHandler:通过调用这个block,来告诉URLSession要不要接收这个证书
 * NSURLSessionAuthChallengeDisposition 如何处理这个安全证书
 * NSURLCredential 安全证书
 */
- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler {
    
    //如果不是服务器信任类型得证书,直接返回
    if (![challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
        return;
    }
    
    //根据服务器的信任信息创建证书对象
    NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
    
    //保证不为空
    if (completionHandler) {
        //利用这个block使用这个证书
        completionHandler(NSURLSessionAuthChallengeUseCredential,credential);
    }
    
    //这也是一种写法
//    !completionHandler ? : completionHandler(NSURLSessionAuthChallengeUseCredential,challenge.proposedCredential);
    
    NSLog(@"-----didReceiveChallenge");
}

AFNetworking框架等内部已经对https安全证书进行了验证,不需要我们做多余的判断。

原文地址:https://www.cnblogs.com/langji/p/5387441.html