ios - 如何使用 NSFileManager 保护或加密视频文件?

标签 ios objective-c iphone ios7 ios8

我需要在 iOS 上保护文档目录或(库目录)中的文件,尤其是视频。我不希望用户可以通过 Xcode 或 iExplorer 应用程序下载文档目录中的视频。我需要保护库目录中的私有(private)内容(或)如何在从服务器下载后加密视频文件。请帮我解决这个问题。

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@.mp4",videoUrlName]]];
NSString* path = [NSString stringWithFormat:@"%@.mp4",videoName];
AFDownloadRequestOperation *videoDownloadRequest = [[AFDownloadRequestOperation alloc] initWithRequest:request targetPath:path shouldResume:YES];

[videoDownloadRequest setProgressiveDownloadProgressBlock:^(AFDownloadRequestOperation *operation, NSInteger bytesRead, long long totalBytesRead, long long totalBytesExpected, long long totalBytesReadForFile, long long totalBytesExpectedToReadForFile)
 {
     float progress = totalBytesReadForFile / (float)totalBytesExpectedToReadForFile;
     NSString *progressMessage = [NSString stringWithFormat:@"%@%@ / %@", @"Downloading...", [self fileSizeStringWithSize:totalBytesReadForFile], [self fileSizeStringWithSize:totalBytesExpectedToReadForFile]];
 }];

[videoDownloadRequest setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
 {
     UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Message" message:[NSString stringWithFormat:@"%@ has been added to your Playlist",detailCellView.titleLabel.text] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
     [alertView show];
 }
                                            failure:^(AFHTTPRequestOperation *operation, NSError *error)
 {


 }];
[[NSOperationQueue mainQueue] addOperation:videoDownloadRequest];

最佳答案

您可以使用任何加密算法来加密您的数据。 AES256 是最常用的算法之一。

您可以使用这个使用 AES 的优秀包装器 https://github.com/RNCryptor/RNCryptor

用法也非常简单。

[videoDownloadRequest setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
 {
    NSData *data = [[NSFileManager defaultManager] contentsAtPath:path];
    NSString *password = @"Secret password";
    NSData *ciphertext = [RNCryptor encryptData:data password:password];

   [data writeToFile:path atomically:YES];
 }

但请记住,真正的安全取决于您保存 key 的安全程度(即上述情况下的 Secret 密码)

关于ios - 如何使用 NSFileManager 保护或加密视频文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33705725/

相关文章:

ios - 防止重复输入消息

ios - performSelectorInBackground 上的 EXC_BAD_ACCESS

ios - 如何在使用 NSUserDefaults 重新加载 View 后保存 tableView 单元格的复选标记?

ios - 在 performBatchUpdates 中重新加载 collectionview 时未调用 UICollectionview cellForItemAtIndexPath

ios - 添加几个 NSStrings 到 NSMutableArray

iphone - 在cocos2d中绘制连续的渐变线

ios - Ionic v1 安全区域无法在 Xcode 11.5 模拟器上运行

ios - swift performSegueWithIdentifier 不工作

iphone - Xcode 4 : Missing File's Owner with Storyboard?

ios - 向 iOS 项目添加和访问二进制 blob 原始数据文件