A reversible DES and TripleDES Encryption Class (original downmoon)

Source: Internet
Author: User

A reversible DES and TripleDES Encryption Class (original downmoon)

Using System;
Using System. Security;
Using System. Security. Cryptography;
Using System. IO;
Using System. Text;
Namespace EncryptDownmoon
...{
/** // <Summary>
/// Summary of EncryptSqlConn.
/// </Summary>
Public class EncryptSqlConn
...{
Public EncryptSqlConn ()
...{
If (mCSP = null)
...{
MCSP = SetEnc ();
}
}
/** // <Summary>
/// Set the encryption mode. The value 0 indicates DES, and the value 1 indicates TripleDES.
/// </Summary>
/// <Returns> </returns>
Public EncryptSqlConn (int EnMethod)
...{
If (EnMethod = 0)
...{
LngEnMethod = 0;
}
Else
...{
LngEnMethod = 1;
}
If (mCSP = null)
...{
MCSP = SetEnc ();
}

}
Method # region Method

Private ‑ricalgorithm mCSP;
Private int m_lngEnMethod = 0;
/** // <Summary>
/// Encryption method. 0 indicates DES, and 1 indicates TripleDES.
/// </Summary>
Public int lngEnMethod
...{
Get
...{
Return m_lngEnMethod;
}
Set
...{
M_lngEnMethod = value;
}
}
Private parameter ricalgorithm SetEnc ()
...{
If (lngEnMethod = 0)
...{
Return new DESCryptoServiceProvider ();
}
Else
Return new TripleDESCryptoServiceProvider ();
}
// Test the key value
Private string genKeyValue
...{
Get
...{
MCSP. GenerateKey ();
Return Convert. ToBase64String (mCSP. Key );
}
}
// Test the IV Value
Private string genIVValue
...{
Get
...{
MCSP. GenerateIV ();
Return Convert. ToBase64String (mCSP. IV );
}
}
/** // <Summary>
/// Encrypted string
/// </Summary>
/// <Param name = "Value"> </param>
/// <Returns> </returns>
Public string EncryptString (string Value)
...{
ICryptoTransform ct;
MemoryStream MS;
CryptoStream cs;
Byte [] byt;
Ct = mCSP. CreateEncryptor (mCSP. Key, mCSP. IV );
// Ct = mCSP. CreateEncryptor (genKeyValue, genIVValue );
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 ());
}

/** // <Summary>
/// Decrypt the string
/// </Summary>
/// <Param name = "Value"> </param>
/// <Returns> </returns>
Public string DecryptString (string Value)
...{
ICryptoTransform ct;
MemoryStream MS;
CryptoStream cs;
Byte [] byt;
Ct = mCSP. CreateDecryptor (mCSP. Key, mCSP. IV );
// Ct = mCSP. CreateDecryptor (genKeyValue, genIVValue );
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
 
}
}

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.