Sponsored By

Featured Blog | This community-written post highlights the best of what the game industry has to offer. Read more like it on the Game Developer Blogs.

Implementing Scoreoid In Unity with Advanced Encryption

Scoreoid is a Truly cross platform Gaming Backed Service. But there is no Guide for integration with Unity, So i decided to share how and why i integrated Scoreoid in my Web Game (Unity ) "Thumbi".

Vijay Kartick Prem Kumar, Blogger

December 8, 2013

3 Min Read

Blog From http://jaykalabs.wordpress.com/2013/12/08/implementing-scoreoid-in-unity/

Scoreoid is a Truly cross platform Gaming Backed Service. But there is no Guide for integration with Unity, So i decided to share how and why i integrated Scoreoid for LeaderBoards in my Web Game (Unity ) "Thumbi" . You can find the game at http://www.wooglie.com/games/Strategy/Thumbi or http://www.kongregate.com/games/jaykalabs417/thumbi

Thumbi mobile game is made with Unity and uses Google play Game Services for LeaderBoards. Though Google's Game Play services works well for Web Games also ,the Plugin i used required an additional JavaScript File to be embedded in the Game's HTML File, which Web Game Publishers like Kongregate or wooglie did not allow. After some searching for Leaderboard services i came across Scoreoid which is very flexible and supports all platform as it is just a bunch of HTTP requests.

Scoreoid Security:

If you send scores without encryption anybody with Paros Proxy can manipulate the http post. Scoreoid provides and advanced security layer (beta) using encryption algorithm “DES”, block mode “ECB” and interchangeable padding “PKCS5″ or “PKCS7″. You have to enable it in settings. Also Scoreoid has both HTTP and HTTPS Api URL, but HTTPS provides better security than HTTP. Use the below helper class to encrypt and decrypt messages.


[code language="csharp"]
using UnityEngine;
using System.Collections;
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

public class Crypto : MonoBehaviour {

public static string Decrypt(string encryptedString)
{
DESCryptoServiceProvider desProvider = new DESCryptoServiceProvider();
desProvider.Mode = CipherMode.ECB;
desProvider.Padding = PaddingMode.PKCS7;
desProvider.Key = Encoding.ASCII.GetBytes("97ed399c");
using (MemoryStream stream = new MemoryStream(Convert.FromBase64String(encryptedString)))
{
using (CryptoStream cs = new CryptoStream(stream, desProvider.CreateDecryptor(), CryptoStreamMode.Read))
{
using (StreamReader sr = new StreamReader(cs, Encoding.ASCII))
{
return sr.ReadToEnd();
}
}
}
}

public static string Encrypt(string decryptedString)
{
DESCryptoServiceProvider desProvider = new DESCryptoServiceProvider();
desProvider.Mode = CipherMode.ECB;
desProvider.Padding = PaddingMode.PKCS7;
desProvider.Key = Encoding.ASCII.GetBytes("97ed399c");
using (MemoryStream stream = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(stream, desProvider.CreateEncryptor(), CryptoStreamMode.Write))
{
byte[] data = Encoding.Default.GetBytes(decryptedString);
cs.Write(data, 0, data.Length);
cs.FlushFinalBlock(); // <-- Add this
return Convert.ToBase64String(stream.ToArray());
}
}
}

}

[/code]

SubmittingScores:

Submitting Scores or Creating User is very Simple.The following code shows you how. Note your GameID and Api key as these are required for every transaction.


[code language="csharp"]
public void SubmitScore(int Score)
{

/* API  method */
string url = "https://api.scoreoid.com/v1/createScore";

/* Unity WWW Class used for HTTP Request */
WWWForm form = new WWWForm();

form.AddField( "game_id", "YourGameID");
string encry= "api_key=YourAPIKEY&"+"response=json&"
+"username="+ProfileName +"&score="+Score;
encry=Crypto.Encrypt(encry);
form.AddField( "s", encry);
WWW www = new WWW(url, form);
StartCoroutine(SubmitscoreCoRoutine(www));
}

IEnumerator SubmitscoreCoRoutine(WWW www)
{
yield return www;

var N=JSON.Parse (Crypto.Decrypt (www.text));

if(N!=null)
{
if(N["success"].Value!=null && N["success"].Value!=string.Empty)
//Score Submitted

}
else
{
             //Score Not Submitted
}

}[/code]

You can receive response in JSON or XML. I choose JSON because it does not take much bandwidth for transferring data. Parsing XML requires System.XML which adds around 1.5 MB to your build. Web Games should be small to decrease loading time. Hence JSON is best choice as JSON parsers are just around 10KB.

There are several libraries for parsing JSON in unity like miniJSON, LiteJson,etc but most of them are full of bugs or did not support our purpose. The best Library is SimpleJSON.

Parsing

You can see from my above code that i'm checking whether the JSON .parse(decrypted string) is null before using it.This is because in case of error the message such as 'gameID doesnot exist', is sent as plain text and not encrytped by Scoreoid.  All other success messages and game related error messages such as 'error playername exists' are all encrypted. Finally Just parse the information if it is not null and run in game actions.

Thus Scoreoid Is Implemented. Hope it helps.

Read more about:

Featured Blogs
Daily news, dev blogs, and stories from Game Developer straight to your inbox

You May Also Like