Osheep

时光不回头,当下最重要。

iOS视频、图片下载加密/解密以及保存到本地

所用资源

加密库 RNCryptor
一款自带缓存的AFN二次封装 PPNetworkHelper
参考文章 iOS文件加密-项目中使用

ps:可以先看上面的文章再来看我这篇O(∩_∩)O~

内容下载

使用 PPNetworkHelper的文件下载功能

+ (NSURLSessionDownloadTask *)Download:(NSString *)urlString parameters:(id)parameters progress:(void (^)(NSProgress *downloadProgress)) progressBlock success:(void (^)(id responseObject))success failure:(void (^)(NSError *error))failure;
//由于需要自定义一些配置,所用LYPPNetworkHepler 继承 PPNetworkHelper此类。
+ (NSURLSessionDownloadTask *)Download:(NSString *)urlString parameters:(id)parameters progress:(void (^)(NSProgress *))progressBlock success:(void (^)(id))success failure:(void (^)(NSError *))failure{
    return [LYPPNetworkHepler downloadWithURL:urlString fileDir:@"leejtom" progress:^(NSProgress *progress) {
        LYLog(@"download: %@", progress.localizedDescription);
    } success:^(NSString *filePath) {
        [LYAVPlayerUtils encryptWithFilePath:filePath];
    } failure:^(NSError *error) {
        LYLog(@"%@",error);
    }];
}

加密

//加密
+ (void)encryptWithFilePath:(NSString *)filePath{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        NSData *avdata = [NSData dataWithContentsOfURL:[NSURL URLWithString:filePath]];
        if (!avdata) {
            LYLog(@"文件不存在");
            return;
        }
        NSError *error;
        NSData *encryptAV = [RNEncryptor encryptData:avdata withSettings:kRNCryptorAES256Settings password:@"123456" error:&error];
        if (error) {
            LYLog(@"encrypt error:%@",error);
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            NSError *error;
            LYLog(@"加密:%d",[encryptAV writeToURL:[NSURL URLWithString:filePath] options:NSDataWritingAtomic error:&error]);
            if (error) {
                LYLog(@"writeToFile error:%@",error);
            }
//          解开注释测试 解密
//          else{
//              [self decryptWithFilePath:filePath];
//          }
        });
        
    });
}

解密

//解密
+ (void)decryptWithFilePath:(NSString *)filePath{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        NSData *avdata = [NSData dataWithContentsOfURL:[NSURL URLWithString:filePath]];
        if (!avdata) {
            LYLog(@"文件不存在");
            return;
        }
        NSError *error;
        NSData *encryptAV = [RNDecryptor decryptData:avdata withPassword:@"123456" error:&error];
        if (error) {
            LYLog(@"decrypt error:%@",error);
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            NSError *error;
            LYLog(@"解密: %d",[encryptAV writeToURL:[NSURL URLWithString:filePath] options:NSDataWritingAtomic error:&error]);
            if (error) {
                LYLog(@"writeToFile error:%@",error);
            }
        });
        
    });
}

遇到的问题

一开始加密一直失败,断点发现原来是使用dataWithContentsOfFile获取data失败了。改成使用dataWithContentsOfURL 解决这个问题。

  • (nullable instancetype)initWithContentsOfFile:(NSString *)path;
  • (nullable instancetype)initWithContentsOfURL:(NSURL *)url;
点赞