Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hiding (or encrypting) app resources?

I've developing a Cocoa app that has certain resources (images) which I wish to protect, but still display. Normally one would just place these in the resources folder, but storing there makes it quite easy to grab and use. Is there any way to keep these images hidden, but still access them within the app?

like image 724
sudo rm -rf Avatar asked Feb 21 '11 17:02

sudo rm -rf


2 Answers

Simple solution:

Merge all files into one big data-file, optionally using 'salts'.
Then retrieve specific files with something like this:

NSData *dataFile = [NSData dataWithContentsOfFile:filePath];  
NSData *theFile = [dataFile subdataWithRange: NSMakeRange(startPos,endPos)];

This does not really protect the files,
but prevents people simply dragging out the resources.
At least, the data-file is unusable, certainly with salts.

Another solution:

Create NSData object for every resource.
Add all objects to a NSMutableArray.
Convert the array to one big NSData object.
Write the NSData object to a file.
And add it to the resources folder.

Your app can then read the data-file.
And retrieve the array with the resources.

// Convert array to data
NSData* data=[NSKeyedArchiver archivedDataWithRootObject:theArray];

Use NSKeyedUnarchiver to retrieve the array again.

like image 120
Anne Avatar answered Oct 28 '22 15:10

Anne


In order for you to protect the images in one big file, you can just dump the image data to a NSData object sequentially.

If you want, you can use either salts, as previously mentioned, or you can use AES encryption method, as shown here.

Then, you will have to either save the image files structurally (using an NSArray or similar) or record the image offsets so you can retrieve the image data blocks correctly.

This has some drawbacks, specially if your images change over time. That way you will have to monitor those changes and re-structure the file accordingly.

On other option is for you to simply mask the image files by changing name/extension to one of your choice. This will leave some users away from touch.

Finally, you can search for some archiving frameworks using zip like functions and keep the images there (as Blizzard uses in their MPQ format). This will be the best option (since it provides you with encryption methods and it abstracts you of the mechanisms of encryption and archiving) but it may not be easy to find such a framework.

like image 31
Tiago Avatar answered Oct 28 '22 15:10

Tiago