PHP

djekl’s Gamertag API v2 [JSON ONLY]

OK so I have recently recoded the API with a few extra features. And because many of you are already using this I didn’t want to mess to much with the v1.
Well all I am going to say is here is the link, have fun.
https://www.djekldev.co.uk/card/v2

This version is now using a cache system that will update if the cache is older than an hour. This means for you faster results.

AES 256 Encryption between PHP and C#

OK so today I will be sharing with you AES 256 Encryption for use between PHP and C#.
This encryption can be Encrypted in either PHP or C# and Decrypted in the other.
 
 
 
PHP Code:

function addpadding($string, $blocksize = 32)
{
    $len = strlen($string);
    $pad = $blocksize - ($len % $blocksize);
    $string .= str_repeat(chr($pad), $pad);
    return $string;
}

function strippadding($string)
{
    $slast = ord(substr($string, -1));
    $slastc = chr($slast);
    $pcheck = substr($string, -$slast);
    if(preg_match("/$slastc{".$slast."}/", $string)){
        $string = substr($string, 0, strlen($string)-$slast);
        return $string;
    } else {
        return false;
    }
}

function encrypt($string = "")
{
	$keyfile = "./AES.keyz";
	$keyfile = file($keyfile);
    $key = base64_decode($keyfile[0]);
    $iv = base64_decode($keyfile[1]);

	return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, addpadding($string), MCRYPT_MODE_CBC, $iv));
}

function decrypt($string = "")
{
	$keyfile = "./AES.keyz";
	$keyfile = file($keyfile);
    $key = base64_decode($keyfile[0]);
    $iv = base64_decode($keyfile[1]);

	$string = base64_decode($string);

	return strippadding(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $string, MCRYPT_MODE_CBC, $iv));
}

 
 
 
C# Code:

        private String AES_encrypt(String Input)
        {
            var aes = new RijndaelManaged();
            aes.KeySize = 256;
            aes.BlockSize = 256;
            aes.Padding = PaddingMode.PKCS7;
            aes.Key = Convert.FromBase64String(AES_Key);
            aes.IV = Convert.FromBase64String(AES_IV);

            var encrypt = aes.CreateEncryptor(aes.Key, aes.IV);
            byte[] xBuff = null;
            using (var ms = new MemoryStream())
            {
                using (var cs = new CryptoStream(ms, encrypt, CryptoStreamMode.Write))
                {
                    byte[] xXml = Encoding.UTF8.GetBytes(Input);
                    cs.Write(xXml, 0, xXml.Length);
                }

                xBuff = ms.ToArray();
            }

            String Output = Convert.ToBase64String(xBuff);
            return Output;
        }

        private String AES_decrypt(String Input)
        {
                RijndaelManaged aes = new RijndaelManaged();
                aes.KeySize = 256;
                aes.BlockSize = 256;
                aes.Mode = CipherMode.CBC;
                aes.Padding = PaddingMode.PKCS7;
                aes.Key = Convert.FromBase64String(AES_Key);
                aes.IV = Convert.FromBase64String(AES_IV);

                var decrypt = aes.CreateDecryptor();
                byte[] xBuff = null;
                using (var ms = new MemoryStream())
                {
                    using (var cs = new CryptoStream(ms, decrypt, CryptoStreamMode.Write))
                    {
                        byte[] xXml = Convert.FromBase64String(Input);
                        cs.Write(xXml, 0, xXml.Length);
                    }

                    xBuff = ms.ToArray();
                }

                String Output = Encoding.UTF8.GetString(xBuff);
                return Output;
        }

 
 
 
For the AES.Keyz file this is it.

    PSVJQRk9QTEpNVU1DWUZCRVFGV1VVT0ZOV1RRU1NaWQ=
    YWlFLVEZZUFNaWlhPQ01ZT0lLWU5HTFJQVFNCRUJZVA=

    32 chr string (32 * 8 = 256 bit)

For this the IV and Key MUST both be a 32 char (256bit) key. I am using a byte[]

Nice and simple to use and has helped me out more than once.
It also supports PKCS7 padding within the PHP.

Example Application
http://dl.dropbox.com/u/11393837/C%20sharp%20AES%20EnDecryption.rar

EDIT: PHP code updated, thanks to PepLamb for noticing.

djekl’s XBOX Gamertag API

OK so a long time ago Duncan Mackenzie created his RESTFUL API for Xbox Gamertag Data.
Unfortunately, as time has come to pass, Microsoft have updated their site and servers, thus meaning that this service no longer works.

A while ago I was looking into creating my own by reading the RAW HTML from the OFFICIAL XBOX Gamercard. For this method I was using the php function “str_replace()”, this worked but was not efficient or sufficient to get all the information from it cleanly.

So the other day (when I had all but given up on this personal project) I happened to find this PHP Simple HTML DOM Parser over at SourceForge.
Once I found this everything fell into place. With this you can read sections of the data to just extract what you want.

Eventually after a few days of work I have created this djekl’ XBOX Gamertag API. This simply reads all the information from the OFFICIAL XBOX Gamercard and outputs it in a useful XML format for usage.

I may release the full source here on my blog but not sure yet. Still trying to get into the Xbox Developer program since they opened it up to the UK.

So what do you think?

Update:

As a few of you have asked how to use XML in PHP here is an example I created a while ago for a different project. You will need to use cUrl to read the XML from my site.