Audio Encryption/Decryption iOS

119 views
Skip to first unread message

Tristan Freeman

unread,
Sep 9, 2016, 4:46:34 PM9/9/16
to rncryptor
I'm new to encryption and I'm currently using RNCryptor to encrypt and decrypt audio recordings. The encryption is correct since the first few bytes are 0x301(which is what I want) but the decrypted NSData is causing the AVAudio Player to fail init. I'm not sure if the way that I'm handling the encryption is causing a conversion from an audio format but any help would be appreciated. The code I'm using to encrypt and decrypt is below.

- (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 *encData = [fileManager contentsAtPath:filePath];

    //NSData *encData = [NSData dataWithContentsOfFile:filePath];


    NSData *decData = [RNDecryptor decryptData:data withPassword:@"ABC123" error:&error];

    

    

    

    return decData;

}

Rob Napier

unread,
Sep 9, 2016, 4:50:32 PM9/9/16
to rncr...@googlegroups.com
You're not checking any of the errors. What error does the encryptor and decryptor return, if any?

Rob
--
You received this message because you are subscribed to the Google Groups "rncryptor" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rncryptor+...@googlegroups.com.
Visit this group at https://groups.google.com/group/rncryptor.
For more options, visit https://groups.google.com/d/optout.

Tristan Freeman

unread,
Sep 9, 2016, 7:35:18 PM9/9/16
to rncryptor
The encryptor and decrypt don't return any errors but I do get the OSStatus error for kAudioFileInvalidFileError.

Error: The operation couldn’t be completed. (OSStatus error 1685348671.)

Rob Napier

unread,
Sep 9, 2016, 11:51:51 PM9/9/16
to rncr...@googlegroups.com
If there isn't any error decrypting than you probably aren't encrypting what you think you are. Check the output bytes and make sure it works with no encryption.

Rob

Tristan Freeman

unread,
Sep 20, 2016, 10:31:00 AM9/20/16
to rncryptor
Thank you for the help! You were right, I wasn't encrypting what I thought I was but I got it figured out and working.
Reply all
Reply to author
Forward
0 new messages