First, summary
Namespace: system.security.cryptography.tripledes class
Brief Description: Represents the base class of the triple data encryption standard algorithm, all implementations of TripleDes must be derived from this class, but tripledes inherited from the Symmetricalgorithm class. Tripledes uses the three consecutive iterations of the DES algorithm. It can use two or three 56-bit keys.
Use the purpose: Compare a safe encryption, the key, and the vectors are different, and different encryption strings are produced. Because it is the three consecutive iterations of the DES algorithm, and the algorithm is reversible, so it is good for data confidentiality and recovery.
How to use: Direct string input and output.
Second, the code example
This code refers to the code example on the partial MSDN, and supplements some of the contents not mentioned on the MSDN according to its own actual situation.
Using system;
Using system.security;
Using system.security.cryptography;
Using system.io;
Using system.text;
Using system.threading;
Namespace Trip3des
{
///
/// Class1 summary description.
///
Public Class Dllencrypt
{
// key
Private const string skey = "qjzgeh6heszdvjecnfpguxzaib7nlqm3";
// vector, vector can be empty
Private const string siv = "QCDY6X APLW =";
// Construct a symmetric algorithm
Private symmetricalgorithm mcsp = new tripledescryptoServiceProvider ();
Public DLLENCRYPT () {}
#REGON PUBLIC STRING EncryptString (String Value)
///
/// Encrypted string
///
/// input string
///
Encrypted string
Public String EncryptString (String Value)
{
Icryptotransform CT;
MemoryStream MS;
CryptostReam CS;
BYTE [] BYT;
Mcsp.key = convert.FromBase64String (SKEY);
Mcsp.iv = convert.frombase64string (siv);
/ / Specify an encrypted arithmetic mode
Mcsp.Mode = system.security.cryptography.ciphermode.ecb;
// Get or set the padding 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 ());
}
#ndregion
#region public string DecryptString (String Value)
///
/// Decrypt string
///
/// Add a dense string
///
Decryptive string
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 ());
}
#ndregion
}
}
Third, summary
Making a class library is more convenient for the keys and vector, the input and output is all String type variables, which is more convenient, and the generation of the key can be generated by MSCP. GenerateKey (), the generation of vector can also use Mcsp.Generateiv () To generate. You can also write your own 3DES algorithm yourself.