8

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?

1

3 Answers 3

9

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.

2
  • 1
    Yeah, that's what I was looking for. Just something to prevent the easy dragging of pictures from the resources. I'm still new to this, so how would I create a mapped-data-file in the first place? Feb 21, 2011 at 19:57
  • Oops, dataForKey does of course not work on NSData, got confused with NSUserDefaults. Forget about that. But I added another solution that should work :)
    – Anne
    Feb 21, 2011 at 20:45
3

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.

-2

Why do you want to protect the images? It goes without saying that anything you display can be recorded with a screenshot, so if you're trying to protect the images from the person viewing them, there isn't much point.

If you still want to protect them (say, some images should only be available to certain people), encrypting them on disk might be an option. I'm not an Objective-C guy, but this1 seems like a good place to look.

4
  • No, the images will be freely viewable within the app, and I do realize the fact that people could just take screenshots. However, I'm just trying to stop the average person from getting easy access to the source images. Feb 21, 2011 at 19:13
  • This might be a stupid question, but are the images still separate files in the output, or do they get mushed together in a resource file? Using a resource hacker might be above your average user. Feb 21, 2011 at 19:19
  • OSX applications are essentially folders. The content can be viewed by clicking 'Show Package Contents'. Images are by default stored in the resources folder, untouched.
    – Anne
    Feb 21, 2011 at 19:26
  • 3
    i hate useless answers. like when ppl asked how to do something, some ppl always wanna know why u wanna do it.
    – OMGPOP
    Dec 17, 2012 at 3:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.