Merry Christmas

I would like to wish everyone a merry Christmas, hope you all got everything you asked for off Santa.

Merry Christmas

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.

SecurityTube Megaprimer course DVD’s

Recently @SecurityTube have started releasing Megaprimer course DVD’s. These are the video’s from the website on a DVD for you to watch at home to learn from.

I am showing my support by offering the hosting of a mirror. – http://securitytube.djekl.co.uk

Be sure to check them out at the following links:
@SecurityTube on Twitter
SecurityTube Website

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.

[C#] JSON to XML (snippet)

OK so I have noticed that more and more people are using JSON for there API’s.
Now this I know can be seen as tricky for some to use, so what I have done today is gather some code together to convert JSON to a more familuar Markup language XML.

1st things 1st
Without the dll from James Newton King, I would not have been able to do this.
Also you will need to download said dll for use with this example code. || DOWNLOAD LINK.

Now to the code, here it is.
Its just an example of how you can do it, and is provided as is!

using System;
using System.IO;
using System.Net;
using System.Windows.Forms;
using System.Xml;
using Newtonsoft.Json;

namespace JSON2XML
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e1)
        {
            /* because the origional JSON string has multiple root's this needs to be added */
            string json = "{BFBC2_GlobalStats:";
            json += DownlodUrl("http://api.bfbcs.com/api/xbox360?globalstats");
            json += "}";

            XmlDocument doc = (XmlDocument)JsonConvert.DeserializeXmlNode(json);
            textBox1.Text = GetXmlString(doc);
        }

        private string DownlodUrl(string url)
        {
            string result = null;
            try
            {
                WebClient client = new WebClient();
                result = client.DownloadString(url);
            }
            catch (Exception ex)
            {
                // handle error
                result = ex.Message;
            }
            return result;
        }

        private string GetXmlString(XmlDocument xmlDoc)
        {
            StringWriter sw = new StringWriter();
            XmlTextWriter xw = new XmlTextWriter(sw);
            xw.Formatting = System.Xml.Formatting.Indented;
            xmlDoc.WriteTo(xw);
            return sw.ToString();
        }
    }
}

Forrst

Well today I finally got into Forrst

A massive thanks goes out to Zack Kitzmiller @ZackKitzmiller who I now know is STAFF on Forrst.

Well if you want come check me out http://forrst.me/djekl