Hello World!


Hello World!

Hello everyone, I'm creating this blog with the purpose of sharing my and your ideas with all hungry for game development.

Follow Tweeter

empty space


Tuesday, January 24, 2012

XML - Encrypting / Decrypting ( RijndaelManaged )

I really have liked to use XML to manage databut when you use this type of file in a standalone project where the data is exposed to users, it can be a big problem. So, I'll show you how to make the texts unreadable for the user using encryption. In this case ( RijndaelManaged algorithm ).


I,m using the same system of the older post : XML - Loading data from a xml file. with some modifications.



Here is my Xml file pre created:


  
    Level 1 (xml)
    Try comes close to all four people. (xml)
     Hi, I'm a Blue man! (xml) 
     Hi, I'm a Green man! (xml) 
     Hi, I'm a Pink man! (xml) 
     Hi, I'm a Yellow man! (xml) 
    Very Good! (xml)
  
  
    Level 2 (xml)
    Try comes close to all four people, again! (xml)
     Hi, I'm a Blue man in the level 2 (xml) 
     Hi, I'm a Green man in the level 2 (xml) 
     Hi, I'm a Pink man in the level 2 (xml) 
     Hi, I'm a Yellow man in the level 2 (xml) 
    Very Good! (xml)
  
I put the functions below in the script to encrypt / decrypt the xml file.
public static string Encrypt (string toEncrypt)
{
 byte[] keyArray = UTF8Encoding.UTF8.GetBytes ("12345678901234567890123456789012");
 // 256-AES key
 byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes (toEncrypt);
 RijndaelManaged rDel = new RijndaelManaged ();
 rDel.Key = keyArray;
 rDel.Mode = CipherMode.ECB;
 // http://msdn.microsoft.com/en-us/library/system.security.cryptography.ciphermode.aspx
 rDel.Padding = PaddingMode.PKCS7;
 // better lang support
 ICryptoTransform cTransform = rDel.CreateEncryptor ();
 byte[] resultArray = cTransform.TransformFinalBlock (toEncryptArray, 0, toEncryptArray.Length);
 return Convert.ToBase64String (resultArray, 0, resultArray.Length);
}

public static string Decrypt (string toDecrypt)
{
 byte[] keyArray = UTF8Encoding.UTF8.GetBytes ("12345678901234567890123456789012");
 // AES-256 key
 byte[] toEncryptArray = Convert.FromBase64String (toDecrypt);
 RijndaelManaged rDel = new RijndaelManaged ();
 rDel.Key = keyArray;
 rDel.Mode = CipherMode.ECB;
 // http://msdn.microsoft.com/en-us/library/system.security.cryptography.ciphermode.aspx
 rDel.Padding = PaddingMode.PKCS7;
 // better lang support
 ICryptoTransform cTransform = rDel.CreateDecryptor ();
 byte[] resultArray = cTransform.TransformFinalBlock (toEncryptArray, 0, toEncryptArray.Length);
 return UTF8Encoding.UTF8.GetString (resultArray);
}
And now the encrypted xml file looks like this:

Mwpbrm94CDw0Q5UXhTkzdF09fl6ZGAjWpmh3bL6eJXx7QFvSxXYZMrrUPnuHreGhrT5lzLV5gYsBQykOYBepn2PWpu0
fmlBRdl+65iAS3+B9dM96M1EkbJUsJ+JZKZehaTq5mjgsxxZIw0ytnvh+PSYBRgIliVreSdvGWptk2xQTZTaqUyW8sy0b1V6m
jWJ3TlGOvpmbYRgdBVnIbtbeB+NM6quFb+ecgyLqpYL3QsSdiAJOZz6geozoBTRWc6nsbivK9buj5F4RfBTLM/d0NArTT8ybB
x9TvcRy0uBtpKAX4rxvjVVLH/GCCe8pD0wGVUn94RSr4G1C6eAEcedZqNjQwqL6PQeDS5J5WAylNIqQM5g+Rntp3LhHDjWXUA
6IZEDFzlfliSrNlQhu0pGZLyWJ+Eqd1alseNzn2kxwbow3+AaJjFxkhpQhGEDPvth040hpQP+D23D2/QGdS24COb3AgSoGrXH
oMTQsfVxhMFt8ZeZgF8dXKPFzFRz/d4L75cFDpOCcxp8E0pmXXvyuw3XBF8o9Ug7EJYi0SJoDxNbLiKjZat+1vdkZL5qwHhtrX
l8sWwIrBxE4OsoONI5bw1+2/7WURghAIvdf3FvsvYVTQMeFhobZGnZe8vpq+dFCUHArxc34kOxJvDJRrVL3iI66k84baTrT0U
1bVukWEtoeHWx7f92ndrjSppPjrv3objIq9hsntOZiw3bMQsM6UG2qyi2N4c0H+33R9okBwQL/r8soJ52N6xaDFTfHuvAI25e
Ok4CEwnpabA6SAqMkiLeoUoGCNhL/YuPuGqLhQbY5BvHSfRSDB8/hVAKLobraEhhv4WFQaXNJU7ICfKG5cFyUeC7P4voQTiuZ
Gc2lzKpriWOjK/i+skE/XBxNpqiloyqXNETseUCIynRg4fzx6SR5n/iI1D0CwV2UKmci+ykPB4NFOgla2qayGyqTGACiexw9A
qh7xqJtuBuMvE+I5JC2j9lTO/66qtvAtKIcGbN43PsPGG6B19TEsEoTJZ8XEhhv4WFQaXNJU7ICfKG5cKf3/Z63J+J8uapK1c
vrTKuPxV6LufAmBC/60kGJd3QaoVYfBQ0sP+RECiGDfy5pbupce5JY6f7+ye37YunYUAs=


Here you can download the fully functioning project. There are many possibilities. I suggest this method only for small or moderate xml files, it can be demanding for processing. I hope you enjoyed it, give your comments!


18 comments:

  1. Thank you very much! That was really helpful

    ReplyDelete
  2. Thank you for sharing, Encryption / Decryption works great! Only I converted it to Unityscript!

    ReplyDelete
    Replies
    1. Would you mind sharing it? :D

      Delete
    2. A bit too late but maybe someone may still find this useful. :)
      XML - Encrypting / Decrypting Unityscript version.
      https://docs.google.com/file/d/0BwjWQOrXClhbYzRoTTRzUGZRcms/edit?usp=sharing

      Delete
  3. This is very usefull, can pilotsidiots share the Unityscript version?

    ReplyDelete
  4. Interesting, but how do you do your Encryption as part of the build process so that you can continue to use the editable XML for your development, and Encrypt the files prior to building your player without having to manually do the encryption step? THat information would be highly useful with this scheme.

    ReplyDelete
    Replies
    1. hi, did you find any solution for your concern? i was thinking about this too. thanks ..

      Delete
  5. Really liked your website really got your point acrosss. Found it while going through google so wanted to tell you good job and you have a bookmark from me.

    ReplyDelete
  6. this is fantastic!!!!!! Thanks a lot !!!!

    ReplyDelete
  7. hear good, can i use it in php post/get method (not xml),
    anyone have this as unity c#?
    pls hlp

    ReplyDelete
    Replies
    1. found an solver in : stackoverflow.com/questions/224453/decrypt-php-encrypted-string-in-c-sharp
      may anyone need it too
      :)

      Delete
  8. serializer.Deserialize doesn't seem to deserialize decrypted text properly. I tried applying this learning some XML serialization code that I'm working on and it barfs when it decrypts it (as a string.. not the file that it saves out).

    The problem I have with this solution that I was trying to fix was the fact that it writes out the XML to a file rather than storing the decryption in memory.

    ReplyDelete
  9. this can help me for my project. Thank a lot :)

    ReplyDelete
  10. Amazing!!! I will start using it for my JSON.

    I have been using for a long time a custom antiCheats Solution.

    I was bascially caluclating the entire MD5 Checksum of all values and if that didnt match anymore when you load the SafeGame the Game was not playable anymore. PRO You could still see the JSON and it helps finding bugs CONTRA very unflexible when you wanna add more values into the AntiCheat because the MD5 will never match so you have to partially deactivate it.

    Simply encrypting the SafeGame is probaby the best solution if you have done all the basics.

    Nice code works perfectly for my ~10KB big JSON :) Time to fight against cheater!

    ReplyDelete
  11. Hi, thanks for the post, but Im using the WWW class to get the XML file, instead your "Application.dataPath" and I'm getting some errors handling the decript... can you help me??

    ReplyDelete
    Replies
    1. Sorry, i'm not updating this blog any more, but i'm pretty sure you'll gonna found a nice solution in the web.

      Delete