using System.Security.Cryptography;
protected static string APIKey = "fromnet7";
protected static string SecretKey = "nei5XieQuoGhah1u";
protected static string SecretFixed = "@portale_unicoop_tirreno";
protected static string BaseUrlLocal = "https://local.portale-unicoop.netseven.it";
protected static string BaseUrlStaging = "https://portale-unicoop.netseven.it";
protected static string BaseUrlProd = "https://unicooptirreno.it";
public static void Main()
string token = "ODk1YjYzNjY0YjJjNWRlZg==";
string password = "password-criptata";
string[] resultLocal = Program.Encrypt(token, password, Program.BaseUrlLocal);
string[] resultStaging = Program.Encrypt(token, password, Program.BaseUrlStaging);
string[] resultProd = Program.Encrypt(token, password, Program.BaseUrlProd);
protected static string[] Encrypt(string token, string clearText, string baseUrl) {
Console.WriteLine("Encoding for: " + baseUrl);
byte[] b = Convert.FromBase64String(token);
string Part0Key = System.Text.Encoding.UTF8.GetString(b);
string Part1Key = CalculateMD5Hash(Program.SecretKey + Program.SecretFixed);
Part1Key = Part1Key.Substring(0, 16);
Console.WriteLine("Key: " + Part0Key + " " + Part1Key);
string KeyStr = Part0Key + Part1Key;
byte[] key = System.Text.Encoding.UTF8.GetBytes(KeyStr);
byte[] encryptedBytes = null;
byte[] bytesToBeEncrypted = System.Text.Encoding.UTF8.GetBytes(clearText);
using (MemoryStream ms = new MemoryStream())
using (RijndaelManaged AES = new RijndaelManaged())
AES.Mode = CipherMode.CBC;
AES.Padding = PaddingMode.PKCS7;
using (var cs = new CryptoStream(ms, AES.CreateEncryptor(), CryptoStreamMode.Write))
cs.Write(bytesToBeEncrypted, 0, bytesToBeEncrypted.Length);
encryptedBytes = ms.ToArray();
retStr = Convert.ToBase64String(encryptedBytes);
ivStr = Convert.ToBase64String(AES.IV);
string data = HttpUtility.UrlEncode(Part0Key+","+retStr+","+ivStr);
Console.WriteLine(retStr);
Console.WriteLine(baseUrl + "/n7-sso-api/test-decrypt2/"+Program.APIKey+"?d="+data);
Console.WriteLine("***********************");
return new string[] {retStr, ivStr};
protected static string CalculateMD5Hash(string input)
MD5 md5 = System.Security.Cryptography.MD5.Create();
byte[] inputBytes = System.Text.Encoding.UTF8.GetBytes(input);
byte[] hash = md5.ComputeHash(inputBytes);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hash.Length; i++)
sb.Append(hash[i].ToString("x2"));