我是加密新手,正在尝试使用 RNCryptor 加密录音。文件已正确加密,但在解密后,创建的 NSData 会导致 AVAudioPlayer 初始化失败。我用于加密和解密的方法是......
- (void)renameFileInDocumentsFolder:(NSString *)oldFilename withNewName:(NSString *)newFilename
{
NSFileManager *filemgr;
NSString *oldPath = [self getFilePathFromDocumentsFolder:oldFilename];
filemgr = [NSFileManager defaultManager];
NSData *data = [filemgr contentsAtPath:oldPath];
NSString *destPath = [[oldPath stringByDeletingLastPathComponent] stringByAppendingPathComponent:[newFilename stringByAppendingString:@".m4a"]];
NSLog(@"DEST:%@", destPath);
NSError *error;
NSData *encryptedData = [RNEncryptor encryptData:data
withSettings:kRNCryptorAES256Settings
password:@"ABC123"
error:&error];
[encryptedData writeToFile:destPath atomically:YES];
[filemgr removeItemAtPath:oldPath error:&error];
}
-(NSData *)decryptFilePathFromDocumentsFolder:(NSString *)filename
{
AudioRecorderAppDelegate *appDelegate=[AudioRecorderAppDelegate sharedDelegate];
_cacheDirectory = [[[appDelegate applicationCacheDirectory]path]stringByAppendingPathComponent:@"Recordings"];
// NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSString *filePath = [_cacheDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.m4a", filename]];
NSData *data = [NSData dataWithContentsOfFile:filePath];
NSData *decData = [RNDecryptor decryptData:data withPassword:@"ABC123" error:&error];
return decData;
}
AVAudioPlayer 初始化是... - (void)initializeAudioPlayer {
NSData *recording = [self decryptFilePathFromDocumentsFolder:_fileNameTextField.text];
if(!audioPlayer)
{
NSError *error=nil;
audioPlayer = [[AVAudioPlayer alloc]
initWithData:recording fileTypeHint:@".m4a" error:&error];
if (error)
NSLog(@"Error: %@", [error localizedDescription]);
else
我不知道我做错了什么,或者 RNCryptor 是否甚至打算加密音频文件,但任何帮助将不胜感激。