简体   繁体   English

带录音的iOS-RNCryptor

[英]iOS-RNCryptor with audio recordings

I'm new to encryption and am trying to encrypt recordings with RNCryptor. 我是加密技术的新手,正在尝试使用RNCryptor加密录音。 The files are encrypted properly but, after decrypting, the created NSData causes the AVAudioPlayer to fail initialization. 文件已正确加密,但是解密后,创建的NSData会导致AVAudioPlayer初始化失败。 The method I'm using for the encryption and decryption are... 我用于加密和解密的方法是...

- (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;
}

The AVAudioPlayer initialization is... - (void)initializeAudioPlayer { 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

I don't know what I'm doing wrong or if RNCryptor is even meant to encrypt audio files but any help would be greatly appreciated. 我不知道我在做什么错,或者我是否知道RNCryptor甚至打算加密音频文件,但是任何帮助将不胜感激。

There are lots of places where you're not checking errors, both in the cryptor calls, and in the reading and writing of the file. 在密码调用以及文件的读写中,有很多地方都没有检查错误。 Make sure at each point that you actually have what you expect. 确保在每一点上您实际都有期望。 Make sure the written and returned data are of reasonable sizes (about the same as the original file). 确保写入和返回的数据大小合理(与原始文件大致相同)。 Make sure the original file actually is playable. 确保原始文件实际上是可播放的。 Makes sure after decryption you have precisely the same bytes as you started with. 确保解密后您拥有与开始时完全相同的字节。

RNCryptor doesn't care what it encrypts. RNCryptor不在乎它加密的内容。 But if you're doing this on the UI thread (which it looks like you're doing, reading a large file from disk can cause you're program to hang, in some cases long enough for the OS to kill you. Generally large file operations (like large audio files) need to be done asynchronously. 但是,如果您是在UI线程上执行此操作(看上去就像在执行此操作,则从磁盘读取大文件可能会导致程序挂起,在某些情况下,等待时间足以使OS杀死您。)文件操作(如大型音频文件)需要异步完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM