A reversible encryption Class (using 3DES encryption) _c# Tutorial

Source: Internet
Author: User
Tags pkcs7
I. Summary
Namespaces: System.Security.Cryptography.TripleDES Class
Simple description: Represents the base class for triple data Encryption standard algorithms, all implementations of TripleDES must derive from this base class. is inherited from the SymmetricAlgorithm class. TripleDES uses the DES algorithm for three consecutive iterations. It can use two or three 56-bit keys.
Purpose of Use: Secure encryption a way, the difference between the key and the vector, will produce different cryptographic strings. Because it is the three consecutive iterations of the DES algorithm, and the algorithm is reversible, it is good for data confidentiality and recoverability.
Second, the code example
This code refers to some of the code examples on MSDN and, based on its own circumstances, supplements a portion of what is not mentioned on MSDN
Copy Code code as follows:

Using System;
Using System.Security;
Using System.Security.Cryptography;
Using System.IO;
Using System.Text;
Using System.Threading;
Namespace Trip3des
{
<summary>
Summary description of the CLASS1.
</summary>
public class Dllencrypt
{
Secret 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 () {}
#region public string encryptstring (string Value)
<summary>
Encrypt 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);
Specify the mode of operation 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>
Decrypting a string
</summary>
<param name= "Value" > Added string </param>
<returns> after decryption of the 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
}
}

Third, summary
The class library is convenient for keeping the key and vector, the input and output are all string variables, so it is more convenient and the key can be generated with MSCP. GenerateKey () to generate, the generation of vectors can also be generated using MCSP.GENERATEIV (). You can also be flexible to write their own 3DES algorithm.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.