Copy codeThe Code is as follows:
/*************************************** ***********
* Copyright: Mr_Sheng
* File name: EncryptHelper. cs
* File description:
* Type description: EncryptHelper encryption help class
* Authorization statement:
* This program is free software;
* You may re-publish and/or modify this program in accordance with the GPL v3 authorization Terms issued by the Free Software Foundation;
* This procedure is released for the purpose of use, but is not subject to any Warranty liability;
* There is also no implied guarantee for the suitability of sales or specific purposes.
* For details, see GNU General Public Authorization v3(see the license.txt file ).
* Version history:
* V2.0.0 Mr_Sheng Modification
*
**************************************** ***********/
Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. Security. Cryptography;
Namespace Sheng. Common
{
/// <Summary>
/// Encryption help class
/// </Summary>
Public class EncryptHelper
{
/// <Summary>
/// MD5 Encryption
/// </Summary>
/// <Param name = "str"> </param>
/// <Returns> </returns>
Public static string MD5DecryptString (string str)
{
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider ();
Byte [] md5Source = System. Text. Encoding. UTF8.GetBytes (str );
Byte [] md5Out = md5.ComputeHash (md5Source );
Return Convert. ToBase64String (md5Out );
}
/// <Summary>
/// DES encrypted string
/// </Summary>
/// <Param name = "sInputString"> input character </param>
/// <Param name = "sKey"> Key </param>
/// <Returns> encryption result </returns>
Public string DESEncryptString (string sInputString, string sKey)
{
Try
{
Byte [] data = Encoding. Default. GetBytes (sInputString );
Byte [] result;
DESCryptoServiceProvider DES = new DESCryptoServiceProvider ();
DES. Key = ASCIIEncoding. ASCII. GetBytes (sKey); // Key
DES. IV = ASCIIEncoding. ASCII. GetBytes (sKey); // initialization Vector
ICryptoTransform desencrypt = DES. CreateEncryptor (); // encryptor object
Result = desencrypt. TransformFinalBlock (data, 0, data. Length); // convert the specified region of the specified byte array
Return BitConverter. ToString (result );
}
Catch (Exception ex)
{
// Ex. Message = "DES encryption exception ";
Throw ex;
}
}
/// <Summary>
/// DES decryption string
/// </Summary>
/// <Param name = "sInputString"> input character </param>
/// <Param name = "sKey"> Key </param>
/// <Returns> decrypt the result </returns>
Public string DESDecryptString (string sInputString, string sKey)
{
Try
{
// Convert a string to a byte array
String [] sInput = sInputString. Split ("-". ToCharArray ());
Byte [] data = new byte [sInput. Length];
Byte [] result;
For (int I = 0; I <sInput. Length; I ++)
{
Data [I] = byte. Parse (sInput [I], System. Globalization. NumberStyles. HexNumber );
}
DESCryptoServiceProvider DES = new DESCryptoServiceProvider ();
DES. Key = ASCIIEncoding. ASCII. GetBytes (sKey );
DES. IV = ASCIIEncoding. ASCII. GetBytes (sKey );
ICryptoTransform desencrypt = DES. CreateDecryptor ();
Result = desencrypt. TransformFinalBlock (data, 0, data. Length );
Return Encoding. Default. GetString (result );
}
Catch (Exception ex)
{
// Ex. Message = "DES decryption exception ";
Throw ex;
}
}
}
}