A reversible encryption class of USES 3DES encryption

  • 2020-05-07 20:17:15
  • OfStack

1. The feed
Namespace: System.Security.Cryptography.TripleDES class
Simple note: represents the base class of the 3 heavy data encryption standard algorithm from which all implementations of TripleDES must derive. Inherited from the SymmetricAlgorithm class. TripleDES USES three successive iterations of the DES algorithm. It can use two or three 56-bit keys.
Use purpose: more secure encryption 1 way, the key and vector of the different, will produce different encryption string. Since it is three successive iterations of DES and the algorithm is reversible, this is good for data confidentiality and recoverability.
2. Code samples
This code refers to the code sample on part of MSDN, and then adds the content not mentioned on part 1 of 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 
{ 
/// <summary> 
/// Class1  Abstract description.  
/// </summary> 
public class dllEncrypt 
{ 
// The key  
private const string sKey = "qJzGEh6hESZDVJeCnFPGuxzaiB7NLQM3"; 
// The vector, the vector can be empty  
private const string sIV = "qcDY6X+aPLw="; 
// structure 1 Symmetric algorithm  
private SymmetricAlgorithm mCSP = new TripleDESCryptoServiceProvider(); 
public dllEncrypt(){} 
#region public string EncryptString(string Value) 
/// <summary> 
///  Encrypted string  
/// </summary> 
/// <param name="Value"> Input string </param> 
/// <returns> The 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); 
// Specifies the operation mode for encryption  
mCSP.Mode = System.Security.Cryptography.CipherMode.ECB; 
// Gets or sets the fill mode for 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 string  
/// </summary> 
/// <param name="Value"> A densified string </param> 
/// <returns> The 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 
} 
} 

3. Summary
It is more convenient to make a class library for the storage of keys and vectors. The input and output are all string variables, which is also more convenient. The generation of keys can be generated by mSCP.GenerateKey (), and the generation of vectors can be generated by mCSP.GenerateIV (). You can also write your own 3DES algorithm.

Related articles: