using System.Security.Cryptography;
namespace OutSystems.NssCryptoAES
public class CssCryptoAES
public static void Main()
Console.WriteLine("Hello!");
String ssCipherText = "";
CssCryptoAES.MssEncrypt("password", "61a2f93d34d5bef3905a8a045936bfb8", out ssCipherText);
Console.WriteLine("First Encryption Method : " + ssCipherText);
CssCryptoAES.MssDecrypt(ssCipherText, "61a2f93d34d5bef3905a8a045936bfb8", out ssDecrypt);
Console.WriteLine("First Decryption Method : " + ssDecrypt);
CssCryptoAES.MssEncrypt2("password", "61a2f93d34d5bef3905a8a045936bfb8", "This Salt", out ssCipherText);
Console.WriteLine("Second Encryption Method : " + ssCipherText);
CssCryptoAES.MssDecrypt2(ssCipherText, "61a2f93d34d5bef3905a8a045936bfb8", "This Salt", out ssDecrypt);
Console.WriteLine("Second Decryption Method : " + ssDecrypt);
public void MssComputeHash(string ssMessage, out string ssHashedMessage)
MD5 md5 = System.Security.Cryptography.MD5.Create();
byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(ssMessage);
byte[] hash = md5.ComputeHash(inputBytes);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hash.Length; i++)
sb.Append(hash[i].ToString("X2"));
ssHashedMessage = sb.ToString();
public static void MssDecrypt(string ssCipherText, string ssPassword, out string ssClearText)
byte[] cipherBytes = Convert.FromBase64String(ssCipherText);
PasswordDeriveBytes pdb = new PasswordDeriveBytes(ssPassword, new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00});
byte[] decryptedData = Decrypt(cipherBytes, pdb.GetBytes(32), pdb.GetBytes(16));
ssClearText = System.Text.Encoding.Unicode.GetString(decryptedData);
public static void MssEncrypt(string ssClearText, string ssPassword, out string ssCipherText)
byte[] clearBytes = System.Text.Encoding.Unicode.GetBytes(ssClearText);
PasswordDeriveBytes pdb = new PasswordDeriveBytes(ssPassword, new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00});
byte[] encryptedData = Encrypt(clearBytes, pdb.GetBytes(32), pdb.GetBytes(16));
ssCipherText = Convert.ToBase64String(encryptedData);
public static void MssEncrypt2(string plainText, string ssPassword, string ssSalt, out string ssCipherText)
using (RijndaelManaged cipher = new RijndaelManaged())
using (Rfc2898DeriveBytes rfc2898 = new Rfc2898DeriveBytes(ssPassword, Encoding.UTF8.GetBytes(ssSalt), 65536))
secretKey = rfc2898.GetBytes(32);
cipher.IV = new byte[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
cipher.Mode = CipherMode.CBC;
cipher.Padding = PaddingMode.PKCS7;
using (ICryptoTransform encryptor = cipher.CreateEncryptor())
using (System.IO.MemoryStream memoryStream = new MemoryStream())
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
using (StreamWriter streamWriter = new StreamWriter(cryptoStream))
streamWriter.Write(plainText);
encryptedData2 = memoryStream.ToArray();
ssCipherText = Convert.ToBase64String(encryptedData2);
public static void MssDecrypt2(string cipherText, string ssPassword, string ssSalt, out string ssDecrypt)
using (RijndaelManaged cipher = new RijndaelManaged())
using (Rfc2898DeriveBytes rfc2898 = new Rfc2898DeriveBytes(ssPassword, Encoding.UTF8.GetBytes(ssSalt), 65536))
secretKey = rfc2898.GetBytes(32);
cipher.IV = new byte[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
cipher.Mode = CipherMode.CBC;
cipher.Padding = PaddingMode.PKCS7;
using (ICryptoTransform decryptor = cipher.CreateDecryptor())
using (System.IO.MemoryStream memoryStream = new MemoryStream(Convert.FromBase64String(cipherText)))
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
using (StreamReader streamReader = new StreamReader(cryptoStream))
ssDecrypt = streamReader.ReadToEnd();
public static byte[] Encrypt(byte[] clearData, byte[] Key, byte[] IV)
MemoryStream ms = new MemoryStream();
Rijndael alg = Rijndael.Create();
alg.Mode = CipherMode.CBC;
alg.Padding = PaddingMode.PKCS7;
CryptoStream cs = new CryptoStream(ms, alg.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(clearData, 0, clearData.Length);
byte[] encryptedData = ms.ToArray();
public static byte[] Decrypt(byte[] cipherData, byte[] Key, byte[] IV)
MemoryStream ms = new MemoryStream();
Rijndael alg = Rijndael.Create();
alg.Mode = CipherMode.CBC;
alg.Padding = PaddingMode.PKCS7;
CryptoStream cs = new CryptoStream(ms, alg.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(cipherData, 0, cipherData.Length);
byte[] decryptedData = ms.ToArray();