zoukankan      html  css  js  c++  java
  • iOS中的加密方式 与 文件解压缩

    1、Base64加密方式

    Base64是一种加密方法,可逆的加密。

    Base64中的可打印字符包括字母A-Z、a-z、数字0-9,这样共有62个字符。/ + 填充 =

    echo -n BC|base64

    base64 ABC -o 123.txt

    base64 -D 123.txt -o uio.txt

    Base64 iOS7之后加入系统库里的

    类方法加锁,对象方法解锁

    1) 字符串转化为base64字符串

    NSString * string = @"ABC";
    
    NSData * data = [string dataUsingEncoding:NSUTF8StringEncoding];
        
    NSString * base64String = [data
    base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
    NSLog(@"%@",base64String);
    

    2)加密后的data转化成加密前的字符串

    //将普通的data转化为加密后的data
    
    NSData * base64Data = [data base64EncodedDataWithOptions:0];
    
    //加密后的Data转换为普通data
    NSData * data2 = [[NSData alloc] initWithBase64EncodedData:base64Data options:0];
        
    NSString * string2 = [[NSString alloc] initWithData:data2 encoding:NSUTF8StringEncoding];
        
    NSLog(@"%@",string2);
    

    3)加密图片

    UIImage * image = [UIImage imageNamed:@"111"];
        
    NSData * data = UIImagePNGRepresentation(image);
        
    NSString * base64String = [data base64EncodedStringWithOptions:0];
    
    //解密图片
        
    NSData * decodeData = [[NSData alloc] initWithBase64EncodedString:base64String options:0];
    
    UIImage * decodeImage = [[UIImage alloc] initWithData:decodeData];
    

    2、MD5加密

    Message Digest Algorithm MD5(中文名为消息摘要算法第五版)为计算机安全领域广泛使用的一种散列函数,用以提供消息的完整性保护
    是计算机广泛使用的杂凑算法之一(又译摘要算法、哈希算法),主流编程语言普遍已有MD5实现。

    MD5算法具有以下特点:

    • 1、压缩性:任意长度的数据,算出的MD5值长度都是固定的。
    • 2、容易计算:从原数据计算出MD5值很容易。
    • 3、抗修改性:对原数据进行任何改动,哪怕只修改1个字节,所得到的MD5值都有很大区别。
    • 4、强抗碰撞:已知原数据和其MD5值,想找到一个具有相同MD5值的数据(即伪造数据)是非常困难的。

    MD5的作用是让大容量信息在用数字签名软件签署私人密钥前被"压缩"成一种保密的格式(就是把一个任意长度的字节串变换成一定长的十六进制数字串)。除了MD5以外,其中比较有名的还有sha-1、RIPEMD以及Haval等。

    1)加密

        
    NSString * password = @"123";
        
    NSLog(@"%@",password.md5String);
    
    //md5加盐 够复杂
    NSString * repassword = [password stringByAppendingString:@"vbd,AS.JFLD.ADj"];
        
    NSLog(@"%@",repassword.md5String);
    

    3、钥匙串加密方式

    SSKeychain 第三方

    iCloud 钥匙串,苹果给我们提供的密码保存的解决方案,iOS7之后有的。

    存沙盒:

    • 1、如果手机越狱,密码容易被窃取。

    • 2、当软件更新时,沙盒里的内容是不被删除的。但是,如果将软件卸载后重装,沙盒里的数据就没有了。

    • 3、每个APP的沙盒是相对独立的,密码无法共用。

    存钥匙串里:

    • 1、苹果提供的安全方案,rsa加密,相对安全。

    • 2、无论软件更新或删除,密码都存在,都可以自动登录。

    • 3、同一公司的APP密码是可以共用的。

    钥匙串c语言的

    1)代码

    setPassword 密码

    Service 服务标识

    account 用户名

    1、添加钥匙串
    
    #define kLoginService @"loginService"
    
    [SSKeychain setPassword:password forService:kLoginService account:username error:NULL];
    
    2、删除钥匙串
        
    [SSKeychain deletePasswordForService:kLoginService account:@"Daquan" error:NULL];
    
    3、通过服务,账号找到密码
    self.passwordTF.text = [SSKeychain passwordForService:kLoginService account:@"Daquan" error:NULL];
    

    4、文件解压缩

    SSZipArchive 第三方

    1、压缩

    NSArray * array = @[
    
    [[NSBundle mainBundle] pathForResource:@"传输中加密" ofType:@"png"],
    [[NSBundle mainBundle] pathForResource:@"加密请求头" ofType:@"png"]
    
    ];
    //根据所有文件的地址压缩文件
    
    [SSZipArchive createZipFileAtPath:@"/Users/dahuan/Desktop/image.zip" withFilesAtPaths:array];
    

    2、解压

    1)第一种方式
    
    [[[NSURLSession sharedSession] downloadTaskWithURL:[NSURL URLWithString:@"http://192.168.1.200/image.zip"] completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
            
            ``//unzipFileAtPath 压缩包的路径``
    ``        //toDestination 解压后的路径``
            
    NSString * cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
            
    [SSZipArchive unzipFileAtPath:location.path toDestination:cachesPath];
            
            
    }] resume];
    
    2)第二种方式
    NSString * path = [[[NSBundle mainBundle] bundlePath]
                           stringByAppendingPathComponent:@"images"];
        
    NSLog(@"%@",path);
        
    //根据目录压缩文件
    
    [SSZipArchive createZipFileAtPath:@"/Users/dahuan/Desktop/image.zip" withContentsOfDirectory:path];
    
    
  • 相关阅读:
    [React] Styled System with extendable Box element
    [CSS] Use grid-template to make your CSS Grid declarations more readable
    [Machine Learning Ex2] Linear Regression with Multiple Variables
    [Machine Learning] Normal Equation for linear regression
    [React] Use react styled system with styled components
    [Machine Learning] Polynomial Regression
    [Machine Learning] Gradient Descent in Practice I
    IT 运行在云端,而云运行在 Linux 上
    html+php超大视频上传代码
    html+php超大视频上传源代码
  • 原文地址:https://www.cnblogs.com/PSSSCode/p/5308427.html
Copyright © 2011-2022 走看看