/**************************************************
* All rights reserved : Mr_Sheng
* wen a The name : EncryptHelper.cs
* The file :
* Type specification : EncryptHelper Encryption helper class
* Authorization statement :
* This program is free software;
* According to the free software foundation GPL v3 License terms, this program is reissued and / Or modification;
* This procedure is issued for the purpose of use without any warranty;
* There is also no implied warranty of merchantability or fitness for a particular purpose.
* Please refer to GNU General public authorization v3 (see license.txt File).
* Version history :
* v2.0.0 Mr_Sheng 2009-09-09 Modify the
*
***************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
namespace Sheng.Common
{
/// <summary>
/// Encryption helper 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"> The 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); // The key
DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey); // Initialization vector
ICryptoTransform desencrypt = DES.CreateEncryptor(); // Encryptor object
result = desencrypt.TransformFinalBlock(data, 0, data.Length); // Converts the specified region of the specified byte array
return BitConverter.ToString(result);
}
catch (Exception ex)
{
//ex.Message = "DES Encryption abnormal ";
throw ex;
}
}
/// <summary>
/// DES Decrypt string
/// </summary>
/// <param name="sInputString"> The input character </param>
/// <param name="sKey">Key</param>
/// <returns> Decryption result </returns>
public string DESDecryptString(string sInputString, string sKey)
{
try
{
// Converts 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 abnormal ";
throw ex;
}
}
}
}