0

I have an app that downloads MP3 files from our web server and momentarily stores them in an NSData object. This object is then written to a .mp3 file stored to the /Library/Cache folder in the app's sandbox.

When it is time to play the file, I load it into an AVPlayerItem like so:

    NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0]
                          stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.mp3", trackID]];

    NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];
    self.trackPlayerItem = [[AVPlayerItem alloc] initWithURL:fileURL];

I think proceed to load this item into an AVPlayer and play it.

So my main question is: How do I encrypt these mp3 files while they're stored on the disk to ensure they can't just be plucked from the file system by anyone with a Jailbroken device?

I've already looked on Stack Overflow but couldn't find anything that helped.

Hope someone can help me out. Thanks

Kiran Panesar
  • 3,094
  • 2
  • 22
  • 28
  • 1
    You don't even need to jailbreak for the files to be accessible. Check out http://www.macroplant.com/iexplorer for just one app that allows this. – Ben Clayton Aug 01 '12 at 17:51

1 Answers1

2

Check out this thread on adding RSA encryption/decryption to NSData.

Community
  • 1
  • 1
Iñigo Beitia
  • 6,303
  • 4
  • 40
  • 46
  • Thanks for the link. How would I go about encrypting the .MP3 file like that? Encrypt the downloaded NSData, save the NSData to the disk for the cache and only decrypt and re-save as MP3 when needed? – Kiran Panesar Aug 01 '12 at 17:25
  • 1
    @KiranPanesar your mp3 file is just an `NSFile` so when creating it with `- (BOOL)createFileAtPath:(NSString *)path contents:(NSData *)contents attributes:(NSDictionary *)attributes`, use the encrypted `NSData` for the contents. When playing loading the file, use `[NSData dataWithContentsOfFile:mp3FileName];` to retrieve the data and decrypt it before playing. – Iñigo Beitia Aug 01 '12 at 17:38
  • Got the encryption & decryption working – thanks. But because I'm loading the track into an AVPlayerItem, have you got an idea on how I would load an NSData object into AVPlayerItem? – Kiran Panesar Aug 02 '12 at 11:21
  • @KiranPanesar post a new question, it could be useful for other people. – Iñigo Beitia Aug 02 '12 at 14:21
  • Good idea. Here is the link: http://stackoverflow.com/questions/11780455/how-to-load-nsdata-into-an-avplayeritem – Kiran Panesar Aug 02 '12 at 15:07