I. Summary
Namespace: System. Security. Cryptography. TripleDES class
Simple Description: it indicates the base class of the Triple Data Encryption Standard algorithm. All implementations of TripleDES must be derived from this base class. Is inherited from the SymmetricAlgorithm class. TripleDES uses three consecutive iterations of the DES algorithm. It can use two or three 56-bit keys.
Purpose: secure encryption: different keys and vectors produce different encrypted strings. Because it is three consecutive iterations of the DES algorithm and the algorithm is reversible, it is good for data confidentiality and recoverability.
Ii. Sample Code
This code references some sample codes on MSDN and adds some content not mentioned on MSDN according to your actual situation.
Copy codeThe Code is as follows: using System;
Using System. Security;
Using System. Security. Cryptography;
Using System. IO;
Using System. Text;
Using System. Threading;
Namespace TRIP3DES
{
/// <Summary>
/// Summary of Class1.
/// </Summary>
Public class dllEncrypt
{
// Key
Private const string sKey = "qJzGEh6hESZDVJeCnFPGuxzaiB7NLQM3 ";
// Vector. The vector can be empty.
Private const string sIV = "qcDY6X + aPLw = ";
// Construct a symmetric algorithm
Private incluricalgorithm mCSP = new TripleDESCryptoServiceProvider ();
Public dllEncrypt (){}
# Region public string EncryptString (string Value)
/// <Summary>
/// Encrypted string
/// </Summary>
/// <Param name = "Value"> input string </param>
/// <Returns> encrypted string </returns>
Public string EncryptString (string Value)
{
ICryptoTransform ct;
MemoryStream MS;
CryptoStream cs;
Byte [] byt;
MCSP. Key = Convert. FromBase64String (sKey );
MCSP. IV = Convert. FromBase64String (sIV );
// Specify the encrypted computing mode
MCSP. Mode = System. Security. Cryptography. CipherMode. ECB;
// Obtain or set the filling mode of the encryption algorithm
MCSP. Padding = System. Security. Cryptography. PaddingMode. PKCS7;
Ct = mCSP. CreateEncryptor (mCSP. Key, mCSP. IV );
Byt = Encoding. UTF8.GetBytes (Value );
MS = new MemoryStream ();
Cs = new CryptoStream (MS, ct, CryptoStreamMode. Write );
Cs. Write (byt, 0, byt. Length );
Cs. FlushFinalBlock ();
Cs. Close ();
Return Convert. ToBase64String (ms. ToArray ());
}
# Endregion
# Region public string DecryptString (string Value)
/// <Summary>
/// Decrypt the string
/// </Summary>
/// <Param name = "Value"> encrypted string </param>
/// <Returns> decrypted string </returns>
Public string DecryptString (string Value)
{
ICryptoTransform ct;
MemoryStream MS;
CryptoStream cs;
Byte [] byt;
MCSP. Key = Convert. FromBase64String (sKey );
MCSP. IV = Convert. FromBase64String (sIV );
MCSP. Mode = System. Security. Cryptography. CipherMode. ECB;
MCSP. Padding = System. Security. Cryptography. PaddingMode. PKCS7;
Ct = mCSP. CreateDecryptor (mCSP. Key, mCSP. IV );
Byt = Convert. FromBase64String (Value );
MS = new MemoryStream ();
Cs = new CryptoStream (MS, ct, CryptoStreamMode. Write );
Cs. Write (byt, 0, byt. Length );
Cs. FlushFinalBlock ();
Cs. Close ();
Return Encoding. UTF8.GetString (ms. ToArray ());
}
# Endregion
}
}
Iii. Summary
It is convenient to store keys and vectors. All input and output variables are string variables. mSCP can be used to generate keys. generateKey () to generate the vector. mCSP can also be used to generate the vector. generateIV. You can also flexibly compile your own 3DES algorithm.