using System;
using System.Security.Cryptography;
public class Test
{
public static void Main()
{
Console.WriteLine(EncryptString("test", "123456"));
}
public static string EncryptString(string Message, string Passphrase)
{
byte[] Results;
System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();
// Step 1. We hash the passphrase using MD5
// We use the MD5 hash generator as the result is a 128 bit byte array
// which is a valid length for the TripleDES encoder we use below
SHA256CryptoServiceProvider HashProvider = new SHA256CryptoServiceProvider();
byte[] temp = HashProvider.ComputeHash(UTF8.GetBytes(Passphrase));
byte[] key = new byte[24];
Array.Copy(temp, key, 24);
// Step 2. Create a new RijndaelManaged object
TripleDESCryptoServiceProvider crypt = new TripleDESCryptoServiceProvider();
// Step 3. Setup the encoder
crypt.Key = key;
crypt.Mode = CipherMode.ECB;
crypt.Padding = PaddingMode.PKCS7;
// Step 4. Convert the input string to a byte[]
byte[] DataToEncrypt = UTF8.GetBytes(Message);
//Console.WriteLine( Convert.ToBase64String(DataToEncrypt));
ICryptoTransform cTransform = crypt.CreateEncryptor();
// Step 5. Attempt to encrypt the string
try
{
ICryptoTransform Encryptor = crypt.CreateEncryptor();
Results = Encryptor.TransformFinalBlock(DataToEncrypt, 0, DataToEncrypt.Length);
}
finally
{
// Clear the crypt and Hashprovider services of any sensitive information
crypt.Clear();
HashProvider.Clear();
}
// Step 6. Return the encrypted string as a base64 encoded string
return Convert.ToBase64String(Results);
}
}