// Main Method
const string original = "Here is some data to encrypt!";
Console.WriteLine("Data to encypt/decrypt: " + original + "\n");
Console.WriteLine("New Technique");
// Create a new instance of the Aes class. This generates a new key and initialization vector (IV)
using (var aes = Aes.Create())
{
if (aes == null) return;
// Encrypt the string to an array of bytes
var encrypted = AesMode.NewEncryptMethod(original, aes.Key, aes.IV);
// Decrypt the bytes to a string
var decrypted = AesMode.NewDecryptMethod(encrypted, aes.Key, aes.IV);
//Display the encrypted data and the decrypted data
Console.WriteLine("Encrypted: {0}", Convert.ToBase64String(encrypted));
Console.WriteLine("Decrypted: {0}", decrypted);
}
// Classes reference
public static byte[] NewEncryptMethod(string plainText, byte[] key, byte[] iv)
{
// Check arguments.
if (plainText == null || plainText.Length <= 0)
throw new ArgumentNullException("plainText");
if (key == null || key.Length <= 0)
throw new ArgumentNullException("key");
if (iv == null || iv.Length <= 0)
throw new ArgumentNullException("key");
byte[] encrypted = { };
// Create an Aes object with the specified key and IV
using (var aes = Aes.Create())
{
if (aes == null) return encrypted;
aes.Key = key;
aes.IV = iv;
// Create a decrytor to perform the stream transform.
var encryptor = aes.CreateEncryptor(aes.Key, aes.IV);
// Create the streams used for encryption.
using (var msEncrypt = new MemoryStream())
{
using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (var swEncrypt = new StreamWriter(csEncrypt))
{
//Write all data to the stream.
swEncrypt.Write(plainText);
}
encrypted = msEncrypt.ToArray();
}
}
}
// Return the encrypted bytes from the memory stream.
return encrypted;
}
public static string NewDecryptMethod(byte[] cipherText, byte[] key, byte[] iv)
{
// Check arguments.
if (cipherText == null || cipherText.Length <= 0)
throw new ArgumentNullException("cipherText");
if (key == null || key.Length <= 0)
throw new ArgumentNullException("key");
if (iv == null || iv.Length <= 0)
throw new ArgumentNullException("key");
// Declare the string used to hold
// the decrypted text.
string plainText;
// Create an Aes object
// with the specified key and IV.
using (var aes = Aes.Create())
{
if (aes == null) return null;
aes.Key = key;
aes.IV = iv;
// Create a decrytor to perform the stream transform.
var decryptor = aes.CreateDecryptor(aes.Key, aes.IV);
// Create the streams used for decryption.
using (var msDecrypt = new MemoryStream(cipherText))
{
using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (var srDecrypt = new StreamReader(csDecrypt))
{
// Read the decrypted bytes from the decrypting stream and place them in a string
plainText = srDecrypt.ReadToEnd();
}
}
}
}
return plainText;
}
// Main Method
const string original = "Here is some data to encrypt!";
Console.WriteLine("Data to encypt/decrypt: " + original + "\n");
Console.WriteLine("\nOld Technique");
var encryptData = AesMode.OldEncryptMethod(original);
var decryptData = AesMode.OldDecryptMethod(encryptData);
//Display the encrypted data and the decrypted data
Console.WriteLine("Encrypted: {0}", encryptData);
Console.WriteLine("Decrypted: {0}", decryptData);
// Classes Reference
// A key used to encode and later decode the data
private const string PrivateKey = "@pRiVaTeKeY123";
/// <summary>
/// OldEncryptMethod
/// </summary>
/// <param name="plainData">Contains plain data to encrypt</param>
/// <returns>Encrypt Data</returns>
public static string OldEncryptMethod(string plainData)
{
byte[] result;
var utf8Encoding = new UTF8Encoding();
var hashProvider = new MD5CryptoServiceProvider();
var tDesKey = hashProvider.ComputeHash(utf8Encoding.GetBytes(PrivateKey));
var tDesAlgorithm = new TripleDESCryptoServiceProvider
{
Key = tDesKey,
Mode = CipherMode.ECB,
Padding = PaddingMode.PKCS7
};
var dataToEncrypt = utf8Encoding.GetBytes(plainData);
try
{
var encryptor = tDesAlgorithm.CreateEncryptor();
result = encryptor.TransformFinalBlock(dataToEncrypt, 0, dataToEncrypt.Length);
}
finally
{
tDesAlgorithm.Clear();
hashProvider.Clear();
}
return Convert.ToBase64String(result);
}
/// <summary>
/// OldDecryptMethod
/// </summary>
/// <param name="encryptData">Contains encrypt data to decrypt</param>
/// <returns>Decrypt Data</returns>
public static string OldDecryptMethod(string encryptData)
{
byte[] results;
var utf8Encoding = new UTF8Encoding();
var hashProvider = new MD5CryptoServiceProvider();
var tDesKey = hashProvider.ComputeHash(utf8Encoding.GetBytes(PrivateKey));
var tDesAlgorithm = new TripleDESCryptoServiceProvider
{
Key = tDesKey,
Mode = CipherMode.ECB,
Padding = PaddingMode.PKCS7
};
var dataToDecrypt = Convert.FromBase64String(encryptData);
try
{
var decryptor = tDesAlgorithm.CreateDecryptor();
results = decryptor.TransformFinalBlock(dataToDecrypt, 0, dataToDecrypt.Length);
}
finally
{
tDesAlgorithm.Clear();
hashProvider.Clear();
}
return utf8Encoding.GetString(results);
}
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (0)