How do you store data on android devices?

Hi!

How do you store your game data on android devices. I know there is UserDefaults but I do not know if that is the smartest thing I do not want the user to be able to change it. So what do you guys do?

Thanks!

Encrypt you data before you store it.
If you don’t know how to store data besides UserDefault, have a look at FileUtils.
getWritablePath() and writeDataToFile() are good starting points if you wanna implement your own save game format.

for storing data there are :
1- plist files with the FileUtils class for reading and writing to theme
2-xml files with tinyxml2 library which comes integrated with cocos2d by default

as for encrypting the data you can do that but keep in mine the performance loss that you might get each time from reading or writing the data as it will require encoding and decoding but depending on your situation it might be worth it!

and if you’re still persistent about how you wanna approach this(encoding and decoding) then i suggest you start googling for “c++ encrypt xml files”.

Thanks to you both! So @Joseph39 do you think it would be worth it for saving scores. I mean one of the points is that the users can compete with scores. Thanks!

there is no right answer to this question; it really depends on your goals.

i think you should try both options(store data in a readable and unreadable human format) and see if there is gonna be any performance loss and it might not be that much of difference if the data is that small !!

as for how you might try and encrypt the data here is a good solution called “XOR encryption (or Exclusive-OR encryption)”.

Kk thanks! So the simple XOR saves string but I am only going to save int, how can I encrypt that too, convert…if so how?

He uses this code:

 //  main.cpp 
 // 
 //  Created by Kyle Banks on 2013-10-05. 
 // 
 
 
 #include <iostream> 
 
 
 using namespace std; 
 
 
 string encryptDecrypt(string toEncrypt) { 
     char key[3] = {'K', 'C', 'Q'}; //Any chars will work, in an array of any size 
     string output = toEncrypt; 
      
     for (int i = 0; i < toEncrypt.size(); i++) 
         output[i] = toEncrypt[i] ^ key[i % (sizeof(key) / sizeof(char))]; 
      
     return output; 
 } 
 
 
 int main(int argc, const char * argv[]) 
 { 
     string encrypted = encryptDecrypt("kylewbanks.com"); 
     cout << "Encrypted:" << encrypted << "\n"; 
      
     string decrypted = encryptDecrypt(encrypted); 
     cout << "Decrypted:" << decrypted << "\n"; 
      
     return 0; 
 } 

Could I store the int as encrypted strings and then decrypt the saved string when I need the int by converting? Do you or anybody know how?

Thanks!

if you wanna use @Kyle Banks way of encrypting strings then can use these c++11 functions

std::to_string() // to convert ints to strings
std::stoi() // to convert strings to ints

or even better/faster then the last one here is another way which operates on numbers directly without having to convert to anything

   uint64_t theNumber;

   uint64_t cryptbase1= 12345678909876, cryptbase2= 234567890987654;

   // encrypt 
   uint64_t encrypted= (theNumber + cryptbase1) ^ cryptbase2;

   // decrypt 
   uint64_t decrypted= (encrypted ^ cryptbase2) - cryptbase1;

and obviously you don’t have to use uint64_t instead you can use any other type

P.S.1: i think the first method (Kyle Banks) is more slower and at the same time more secure and the second method is faster and at the same time less secure so choose wisely.

P.S.2: if you look more on “fast number encryption” no doubt you will find better and faster solutions then you already have!!

2 Likes