///
///
///
///
///
///
public string EncryptTDESMessage(string plainMessage, string password)
{
// TripleDESCryptoServiceProvider defines a wrapper object to access the
// cryptographic service provider (CSP) version of the TripleDES algorithm.
TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
// The IV property gets or sets the initialization vector (IV) for the symmetric
// algorithm. If this property is a null reference (Nothing in Visual Basic) when
// it is used, the GenerateIV method is called to create a new random value.
des.IV = new byte[8];
// PasswordDeriveBytes derives a key from a password.
PasswordDeriveBytes pdb = new PasswordDeriveBytes(password, new byte[0]);
// The Key property gets or sets the secret key for the TripleDES algorithm.
// The CryptDeriveKey method derives a cryptographic key from the PasswordDeriveBytes
// object and returns the derived Key.
// CryptDeriveKey(algname, alghashname, keysize, rgbIV)
// algname: The algorithm name for which to derive the key.
// alghashname: The hash algorithm name to use to derive the key.
// keysize: The size of the key to derive.
// rgbIV: The initialization vector (IV) to use to derive the key.
des.Key = pdb.CryptDeriveKey("RC2", "MD5", 128, new byte[8]);
// The MemoryStream class creates streams that have memory as a backing store instead
// of a disk or a network connection. MemoryStream encapsulates data stored as an unsigned
// byte array that is initialized upon creation of a MemoryStream object, or the array can
// be created as empty. The encapsulated data is directly accessible in memory. Memory
// streams can reduce the need for temporary buffers and files in an application.
MemoryStream ms = new MemoryStream(plainMessage.Length * 2);
// CryptoStream defines a stream that links data streams to cryptographic transformations.
CryptoStream encStream = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
// Encoding.UTF8.GetBytes encodes a specified range of characters from a Unicode
// character array or a String and stores the results in a specified byte array.
byte[] plainBytes = Encoding.UTF8.GetBytes(plainMessage);
// Writes the value of the plainBytes array, starting at index 0 and until it
// reaches index equal to the length of the string (array).
encStream.Write(plainBytes, 0, plainBytes.Length);
// FlushFinalBlock updates the underlying data source or repository with the
// current state of the buffer, then clears the buffer.
encStream.FlushFinalBlock();
// Create a new byte array using the length of the MemoryStream.
byte[] encryptedBytes = new byte[ms.Length];
// Position gets or sets the current position within the stream.
ms.Position = 0;
// Read a block of bytes from the current stream and writes the data to the buffer.
ms.Read(encryptedBytes, 0, (int)ms.Length);
// Close the CryptoStream.
encStream.Close();
// Return the encrypted message.
// Convert.ToBase64String converts the value of an array of 8-bit unsigned integers
// to its equivalent String representation consisting of base 64 digits.
return Convert.ToBase64String(encryptedBytes);
}
///
///
///
///
///
///
public string DecryptTDESMessage(string encryptedBase64, string password)
{
// TripleDESCryptoServiceProvider defines a wrapper object to access the
// cryptographic service provider (CSP) version of the TripleDES algorithm.
TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
// The IV property gets or sets the initialization vector (IV) for the symmetric
// algorithm. If this property is a null reference (Nothing in Visual Basic) when
// it is used, the GenerateIV method is called to create a new random value.
des.IV = new byte[8];
// PasswordDeriveBytes derives a key from a password.
PasswordDeriveBytes pdb = new PasswordDeriveBytes(password, new byte[0]);
// The Key property gets or sets the secret key for the TripleDES algorithm.
// The CryptDeriveKey method derives a cryptographic key from the PasswordDeriveBytes
// object and returns the derived Key.
// CryptDeriveKey(algname, alghashname, keysize, rgbIV)
// algname: The algorithm name for which to derive the key.
// alghashname: The hash algorithm name to use to derive the key.
// keysize: The size of the key to derive.
// rgbIV: The initialization vector (IV) to use to derive the key.
des.Key = pdb.CryptDeriveKey("RC2", "MD5", 128, new byte[8]);
// Create a new byte array and initialize it with the value passed into the function.
// Convert.FromBase64String converts the specified String representation of a value
// consisting of base 64 digits to an equivalent array of 8-bit unsigned integers.
byte[] encryptedBytes = Convert.FromBase64String(encryptedBase64);
// The MemoryStream class creates streams that have memory as a backing store instead
// of a disk or a network connection. MemoryStream encapsulates data stored as an unsigned
// byte array that is initialized upon creation of a MemoryStream object, or the array can
// be created as empty. The encapsulated data is directly accessible in memory. Memory
// streams can reduce the need for temporary buffers and files in an application.
MemoryStream ms = new MemoryStream(encryptedBase64.Length);
// CryptoStream defines a stream that links data streams to cryptographic transformations.
CryptoStream decStream = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
// Writes the value of the plainBytes array, starting at index 0 and until it
// reaches index equal to the length of the string (array).
decStream.Write(encryptedBytes, 0, encryptedBytes.Length);
// FlushFinalBlock updates the underlying data source or repository with the
// current state of the buffer, then clears the buffer.
decStream.FlushFinalBlock();
// Create a new byte array using the length of the MemoryStream.
byte[] plainBytes = new byte[ms.Length];
// Position gets or sets the current position within the stream.
ms.Position = 0;
// Read a block of bytes from the current stream and writes the data to the buffer.
ms.Read(plainBytes, 0, (int)ms.Length);
// Close the CryptoStream.
decStream.Close();
// Return the decrypted message.
return Encoding.UTF8.GetString(plainBytes);
}