How to secure or encrypt video files with NSFileMa

2019-09-03 09:00发布

问题:

I need to secure files in document directory or (Library directory) on iOS especially videos. I don't want user can download videos in document directory by Xcode or iExplorer app. I need to secure the private content inside of the Library directory (or) how to encrypt video file after downloading from server. Please help me on this.

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

回答1:

You can use any of the encryption algorithms for encrypting your data. AES256 is one of the most common algorithms used.

You can make use of this excellent wrapper which uses AES https://github.com/RNCryptor/RNCryptor

The usage is also pretty much straight forward.

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

But keep in mind that the real security lies on how secure you are keeping the key(i.e the Secret password in the above case)



回答2:

Apple has blocked access to most of the Apps directory for apps downloaded from the App Store Refer this

We need to do one thing before the app submission

https://developer.apple.com/library/ios/qa/qa1719/_index.html