/**/ /**************************************************************************模块编号:包含:作者: 审核:编写日期:2008-03-23描述:提供对称加密、摘要算法 ..........................................如果模块有修改,则每次修改添加以下注释:Log编号:修改作者:修改日期:修改描述:**************************************************************************/ 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(); } } }