1
\$\begingroup\$

I'm creating a game using Cocos2D (objective-c) and it has come to the point where I have to save data to local storage, like high scores and similar stuff.

I know that NSUserDefaults is not a good option because it isn't encrypted. I would like to know what is or what are the best/simple methods of saving data with security to the iPhone to avoid a casual hacker from changing the score.

\$\endgroup\$
4
  • \$\begingroup\$ Fundamentally this is the same question as gamedev.stackexchange.com/questions/23296/… There shouldn't be anything specific to the iPhone that would make the answer any different, unless there's some kind of built-in mechanism in the SDK for doing so. \$\endgroup\$
    – Tetrad
    Apr 4, 2012 at 23:23
  • \$\begingroup\$ There is such a mechanism built into iOS (the Keychain), but it's really intended for passwords and probably isn't a good fit for saved games. \$\endgroup\$ Apr 5, 2012 at 4:56
  • \$\begingroup\$ @JohnCalsbeek, So better not to use keychain? \$\endgroup\$
    – marcg11
    Apr 5, 2012 at 10:55
  • \$\begingroup\$ @marcg11 Yeah, it's not intended for chunks of secure content, just for passwords. \$\endgroup\$ Apr 5, 2012 at 16:26

3 Answers 3

1
\$\begingroup\$

Personally, I wouldn't try to encrypt the file. It makes writing and parsing more complicated (eg. you'll have to work around the built-in mechanisms) and it's also much harder to debug, because you can't have a look what your app just wrote there.

Instead just write to the application directory or use NSUserDefaults.

Now this doesn't address your issue about hacking of these files. To add some protection I'd suggest that you create a hash (checksum) of that data and store it in the Keychain. Then every time you read the data back in, you compare the stored hash to the one that you'll get from the read data. If it's the same, everything is fine. If the data has been altered, your checksums won't match and you can do any counter-measure you'd like...

\$\endgroup\$
8
  • \$\begingroup\$ How exactly this cheksum works? I know with NSUserDefaults I can set the score by doing the following setInteger:score forKey:@"myScore"]; When you say store the cheksum in the keychain , whatt do you mean? \$\endgroup\$
    – marcg11
    Apr 5, 2012 at 10:48
  • \$\begingroup\$ By checksum/hash, I mean something like MD5. Then you generate a hash of your data (eg. everything that gets written to the file). If the data changes, it won't generate the same hash... \$\endgroup\$
    – bummzack
    Apr 5, 2012 at 12:58
  • 1
    \$\begingroup\$ Maybe this article will help you. \$\endgroup\$
    – bummzack
    Apr 5, 2012 at 13:00
  • \$\begingroup\$ Nice article. I won't save a lot of things, so it's better to send a string instead of NSData, ,right? That string is just all the things i want to save and more (like the udid, etc) concatenated? \$\endgroup\$
    – marcg11
    Apr 5, 2012 at 13:39
  • \$\begingroup\$ I'd use an NSDictionary as container for your settings. You can easily write that to a .plist file or serialize it to NSData. That's going to be flexible, even when you plan to write more contents later on. \$\endgroup\$
    – bummzack
    Apr 5, 2012 at 14:00
0
\$\begingroup\$

Well if you're worried about secure data, I would encrypt it yourself regardless of where it's stored. Since I don't do iOS programming I don't really know where you can store it but I would suggest also storing it online if possible. That way, you always have control over the data. But, if that's not an option, (if it lets you) store it in the app folder encrypted. Hope this helps.

\$\endgroup\$
0
\$\begingroup\$

If you want to store the players score than NSUserDefaults is the best option to store it as no one can change it except your application itself.

There are other options available in iOS devices, like in a file in the documents folder of your application in the form of plist, sqlite3 or normal text file, which can be modified by iExplorer application for mac and windows. But you don't have to worry about that as most of the iPhone users won't go to that much level to just change their score. They are gonna play ur game instead, if they like it.

So you don't need to worry about the where to store your application data. But even if you are worried than there are some encryption classes available in iOS framework that you can use to store your data.

\$\endgroup\$
1
  • \$\begingroup\$ The thing is that I'm new to iOs programming, and I can't find a good source of code of encryption, or the ones I find aren't very clear for me. \$\endgroup\$
    – marcg11
    Apr 5, 2012 at 10:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .