using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
namespace GenerateSslKey
{
class Program
{
static byte[] MasterKey_00 = string2array("C2CA...");
static byte[] ssl_kek_source = string2array("7F5B...");
static byte[] ssl_kek_generation_source = string2array("EF2C...");
static byte[] ssl_key_generation_source = string2array("9A38...");
static void Main(string[] args)
{
byte[] kek_Key = new byte[16];
kek_Key = GenerateAesKek(ssl_kek_source, MasterKey_00, ssl_kek_generation_source, ssl_key_generation_source);
Console.WriteLine("Generated key: " + array2string(kek_Key));
Console.WriteLine("Should be: 'B011...'");
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
static byte[] GenerateAesKek(byte[] seed, byte[] MasterKey, byte[] key_x, byte[] key_y = null)
{
byte[] key_x_copy = new byte[16], seed_copy = new byte[16], key_y_copy = new byte[16];
//decrypt key_x
key_x.CopyTo(key_x_copy, 0);
key_x_copy = aes_128_ecb_dec(MasterKey, key_x_copy);
//decrypt seed
seed.CopyTo(seed_copy, 0);
seed_copy = aes_128_ecb_dec(key_x_copy, seed_copy);
//decrypt key_y or use the decrypted seed
if (key_y != null)
{
key_y.CopyTo(key_y_copy, 0);
key_y_copy = aes_128_ecb_dec(seed_copy, key_y_copy);
}
else
{
seed_copy.CopyTo(key_y_copy, 0);
}
return key_y_copy;
}
static byte[] aes_128_ecb_dec(byte[] key, byte[] data)
{
byte[] result = new byte[data.Length];
try
{
using (var rm = new RijndaelManaged())
{
rm.Mode = CipherMode.ECB;
rm.Padding = PaddingMode.None;
rm.KeySize = 128;
rm.BlockSize = 128;
rm.Key = key;
rm.IV = new byte[0x10];
using (var itc = rm.CreateDecryptor())
{
result = itc.TransformFinalBlock(data, 0, data.Length);
}
}
return result;
}
catch (CryptographicException e)
{
Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
return null;
}
}
static byte[] string2array(string data)
{
string trimmed = data.Trim();
byte[] result = new byte[trimmed.Length / 2];
for (int i = 0; i < (trimmed.Length / 2); i++)
{
result[i] = byte.Parse(trimmed.Substring(i * 2, 2), System.Globalization.NumberStyles.HexNumber);
}
return result;
}
static string array2string(byte[] data)
{
string result = "";
for (int i = 0; i < data.Length; i++)
{
result += string.Format("{0:X2}", data[i]);
}
return result;
}
}
}