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
}
}