——————————————————————————MD5加密方式————————————————————————————-—
//tostring中的参数标示用何种格式来格式化字符串,其中X标示16进制,2表示两位
// string str1=byte.ToString("x");//str1="0";
// string str2=byte.ToString("x2");//str2="00";
public static string GetMD5Encording(string str)
{
MD5 md5 = new MD5CryptoServiceProvider();
byte[] _byte = Encoding.Default.GetBytes(str);
byte[] _byteRst = md5.ComputeHash(_byte);
StringBuilder SBuilder = new StringBuilder();
for (int i = 0; i < _byteRst.Length; i++)
{
SBuilder.Append(_byteRst[i].ToString("x2"));
}
return SBuilder.ToString();
}
}
————————————————————————————对称加密—————————————————————————————
using System.Text;
using System.IO;
using System;
using System.Text.RegularExpressions;
using System.Security.Cryptography;
namespace LHCHIP.Common
{
/**//// <summary>
/// 对称加密算法类
/// </summary>
public class DecryptEncrypt
{
/**//// <summary>
/// 返回自身的一个类
/// </summary>
public static DecryptEncrypt MyDecryptEncrypt
{
get
{
return new DecryptEncrypt();
}
}
private SymmetricAlgorithm mobjCryptoService;
private string Key;
/**//// <summary>
/// 对称加密类的构造函数
/// </summary>
internal DecryptEncrypt()
{
mobjCryptoService = new RijndaelManaged();
Key = "rrp(%&h70x89H$jgsfgfsI0456Ftma81&fvHrr&&76*h%(12lJ$lhj!y6&(*jkPer44a";
}
/**//// <summary>
/// 获得密钥
/// </summary>
/// <returns>密钥</returns>
private byte[] GetLegalKey()
{
string _TempKey = Key;
mobjCryptoService.GenerateKey();
byte[] bytTemp = mobjCryptoService.Key;
int KeyLength = bytTemp.Length;
if (_TempKey.Length > KeyLength)
_TempKey = _TempKey.Substring(0, KeyLength);
else if (_TempKey.Length < KeyLength)
_TempKey = _TempKey.PadRight(KeyLength, ' ');
return ASCIIEncoding.ASCII.GetBytes(_TempKey);
}
/**//// <summary>
/// 获得初始向量IV
/// </summary>
/// <returns>初试向量IV</returns>
private byte[] GetLegalIV()
{
string _TempIV = "@afetj*Ghg7!rNIfsgr95GUqd9gsrb#GG7HBh(urjj6HJ($jhWk7&!hjjri%$hjk";
mobjCryptoService.GenerateIV();
byte[] bytTemp = mobjCryptoService.IV;
int IVLength = bytTemp.Length;
if (_TempIV.Length > IVLength)
_TempIV = _TempIV.Substring(0, IVLength);
else if (_TempIV.Length < IVLength)
_TempIV = _TempIV.PadRight(IVLength, ' ');
return ASCIIEncoding.ASCII.GetBytes(_TempIV);
}
/**//// <summary>
/// 加密方法
/// </summary>
/// <param name="Source">待加密的串</param>
/// <returns>经过加密的串</returns>
public string Encrypto(string Source)
{
byte[] bytIn = UTF8Encoding.UTF8.GetBytes(Source);
MemoryStream ms = new MemoryStream();
mobjCryptoService.Key = GetLegalKey();
mobjCryptoService.IV = GetLegalIV();
//创建对称加密器对象
ICryptoTransform encrypto = mobjCryptoService.CreateEncryptor();
//定义将数据流链接到加密转换的流
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);
cs.Write(bytIn, 0, bytIn.Length);
cs.FlushFinalBlock();
ms.Close();
byte[] bytOut = ms.ToArray();
return Convert.ToBase64String(bytOut);
}
/**//// <summary>
/// 解密方法
/// </summary>
/// <param name="Source">待解密的串</param>
/// <returns>经过解密的串</returns>
public string Decrypto(string Source)
{
byte[] bytIn = Convert.FromBase64String(Source);
MemoryStream ms = new MemoryStream(bytIn, 0, bytIn.Length);
mobjCryptoService.Key = GetLegalKey();
mobjCryptoService.IV = GetLegalIV();
//创建对称解密器对象
ICryptoTransform encrypto = mobjCryptoService.CreateDecryptor();
//定义将数据流链接到加密转换的流
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read);
StreamReader sr = new StreamReader(cs);
return sr.ReadToEnd();
}
}
}