using System.Collections.Generic;
using System.Security.Cryptography;
using System.Threading.Tasks;
public static void Main()
string key = "44 52 d7 16 87 b6 bc 2c 93 89 c3 34 9f dc 17 fb 3d fb ba 62 24 af fb 76 76 e1 33 79 26 cd d6 02";
string decryptedText = Encryption.decryptData(@"1211ad43f08caead497049804667ad202aa7822a6574e2be1caaf965b754417f4fc76a932de8add2725b52bc60757aee102cae4e674b283ef462251636d872e8a98e6b45bbe07d5f7a48ddd37e777ec3d6f6a60414fb93e7351a2eef42457f33f6402600e06ac78a012bb5060f712b48", key);
Console.WriteLine(decryptedText);
public static string generateRandomHex(int byteLength)
int stringLength = byteLength * 2;
string alphabet = "abcdef0123456789";
Random rnd = new Random();
for (int i = 0; i < stringLength; i++)
int r = rnd.Next(0, alphabet.Length);
s = s.Replace("00", "11");
private static byte[] dataFromHexString(string hexString)
hexString = hexString.Trim();
hexString = hexString.Replace(" ", "");
byte[] data = Enumerable.Range(0, hexString.Length / 2)
.Select(x => Convert.ToByte(hexString.Substring(x * 2, 2), 16))
private static string dataToHexString(byte[] data)
string hexString = String.Concat(Array.ConvertAll(data, x => x.ToString("X2")));
public static string encryptData(string text, string hexKey)
string hexIV = generateRandomHex(16);
byte[] bytesData = System.Text.Encoding.UTF8.GetBytes(text);
string hexStr = dataToHexString(bytesData);
string cipherHexStr = __encryptData(hexStr, hexKey, hexIV);
string hmacHexKey = generateRandomHex(16);
string hmacHexStr = Encryption.__computeHMAC(hexIV, cipherHexStr, hexKey, hmacHexKey);
string encryptedHexStr = hexIV + hmacHexKey + hmacHexStr + cipherHexStr;
public static string decryptData(string hexStr, string hexKey)
string hexIV = hexStr.Substring(0, 32);
string hmacHexKey = hexStr.Substring(32, 32);
string hmacHexStr = hexStr.Substring(64, 64);
string cipherHexStr = hexStr.Substring(128);
string computedHmacHexStr = Encryption.__computeHMAC(hexIV, cipherHexStr, hexKey, hmacHexKey);
if (computedHmacHexStr.ToLower() == hmacHexStr.ToLower())
string decryptedStr = __decryptData(cipherHexStr, hexKey, hexIV);
byte[] data = dataFromHexString(decryptedStr);
plainText = System.Text.Encoding.UTF8.GetString(data);
private static void checkKey(string hexKey)
hexKey = hexKey.Replace(" ", "");
hexKey = hexKey.ToLower();
throw new Exception("key length is not 256 bit (64 hex characters)");
for(i=0;i<hexKey.Length;i+=2)
if(hexKey[i] == '0' && hexKey[i+1] == '0')
throw new Exception("key cannot contain zero byte block");
private static string __computeHMAC(string hexIV, string cipherHexStr, string hexKey, string hmacHexKey)
hexKey = hexKey.Replace(" ", "");
hexKey = hexKey.ToLower();
hmacHexKey = hmacHexKey.ToLower();
string hexString = hexIV + cipherHexStr + hexKey;
hexString = hexString.ToLower();
byte[] data = System.Text.Encoding.UTF8.GetBytes(hexString);
byte[] hmacKey = System.Text.Encoding.UTF8.GetBytes(hmacHexKey);
HMACSHA256 hmac = new HMACSHA256(hmacKey);
byte[] hashbytes = hmac.ComputeHash(data);
string hashHexStr = Encryption.dataToHexString(hashbytes);
public static string __encryptData(string hexString, string hexKey, string hexIV)
byte[] data = dataFromHexString(hexString);
byte[] key = dataFromHexString(hexKey);
byte[] iv = dataFromHexString(hexIV);
aes.Mode = CipherMode.CBC;
aes.Padding = PaddingMode.PKCS7;
var encryptor = aes.CreateEncryptor(key, iv);
MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write);
cryptoStream.Write(data, 0, data.Length);
cryptoStream.FlushFinalBlock();
byte[] encryptData = memoryStream.ToArray();
string encryptHexData = dataToHexString(encryptData);
private static string __decryptData(string hexString, string hexKey, string hexIV)
byte[] data = dataFromHexString(hexString);
byte[] key = dataFromHexString(hexKey);
byte[] iv = dataFromHexString(hexIV);
aes.Mode = CipherMode.CBC;
aes.Padding = PaddingMode.PKCS7;
var decryptor = aes.CreateDecryptor(key, iv);
MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Write);
cryptoStream.Write(data, 0, data.Length);
cryptoStream.FlushFinalBlock();
byte[] decryptData = memoryStream.ToArray();
string decryptHexData = dataToHexString(decryptData);