using System; using System.Collections.Generic; using System.Text; using System.Security.Cryptography; using System.IO; namespace Zhaizj.Framework.Utils { /// /// 证书加密解密 /// public class LicenseEncry { private SymmetricAlgorithm _algthm; private string _key; private string _IV; /// /// 对称加密算法构造函数 /// public LicenseEncry() { Inital(); } /// /// 对称加密算法构造函数,需要传入密钥字符串 /// /// 密钥字符串 public LicenseEncry(string Key) { Inital(); _key = EncryKey(Key); } /// /// 对称加密算法构造函数,需要传入密钥字符串和初始化向量字符串 /// /// 密钥字符串 /// 初始向量字符串 public LicenseEncry(string Key,string IV) { Inital(); _key = EncryKey(Key); _IV = IV; } /// /// 基本配置初始化 /// private void Inital() { _algthm = new RijndaelManaged(); //128位的key _key = "*)L7uaDdsl#@(*&873JKLF(*#klj*)L7uaDdsl#@(*&873JKLF(*#kljsdsfhjskldf789#()RU*(h9oa9#*w7R(Q#U(*)L7*)L7uaDdsl#@(*&873JKLF(*#kljsdsfhjskldf789#()RU*(h9oa9#*w7R(Q#U(uaDdsl#@(*&873JKLF(*#kljsdsfhjskldf789#()RU*(h9oa9#*w7R(Q#U(sdsfhjskldf789#()RU*(h9oa9#*w7R(Q#U("; _IV = "FG&^lsdlkjsdojslkad3240897&^9o34ly89f(*&#(!@_@_dfskldfhjks5d4f"; //_algthm.KeySize = 256; } private string EncryKey(string key) { //32位MD5加密 string longKey = MD5Encrp.MD5(key); //将key变成64位的长密钥 while(longKey.Length<256) { longKey += longKey; } return longKey; } /// /// 对原文进行加密后返回加密文 /// /// /// public string Encrypt(string Source) { //原文字节 byte[] byteIn = UTF8Encoding.UTF8.GetBytes(Source); //密文流 MemoryStream ms = new MemoryStream(); //加密算法转换器生成 _algthm.Key = GetLegalKey(); _algthm.IV = GetLegalIV(); ICryptoTransform crypto = _algthm.CreateEncryptor(); //执行加密转换 CryptoStream cs = new CryptoStream(ms, crypto, CryptoStreamMode.Write); cs.Write(byteIn, 0, byteIn.Length); cs.FlushFinalBlock(); ms.Close(); cs.Close(); //返回编码后的字符串结果 byte[] byteOut = ms.ToArray(); return Convert.ToBase64String(byteOut); } /// /// 对密文进行解密后返回加原文 /// /// /// public string Decrypt(string Source) { //密文字节 byte[] byteIn = Convert.FromBase64String(Source); //密文流 MemoryStream ms = new MemoryStream(byteIn, 0, byteIn.Length); //解密算法转换器生成 _algthm.Key = GetLegalKey(); _algthm.IV = GetLegalIV(); ICryptoTransform crypto = _algthm.CreateDecryptor(); //执行解密转换 CryptoStream cs = new CryptoStream(ms, crypto, CryptoStreamMode.Read); //返回编码后的字符串结果 StreamReader sr = new StreamReader(cs); return sr.ReadToEnd(); } /// /// 比较现有密钥与算法密钥的长度,使得现有密钥此成为合法长度 /// /// private byte[] GetLegalKey() { string tmpKey = _key; _algthm.GenerateKey(); int okLen = _algthm.Key.Length; if (tmpKey.Length > okLen) { //_key过长则截取 tmpKey = tmpKey.Substring(0, okLen); } else if (tmpKey.Length < okLen) { //_key过短则补充 tmpKey = tmpKey.PadRight(okLen, ' '); } return ASCIIEncoding.ASCII.GetBytes(tmpKey); } /// /// 比较现有IV与算法IV的长度,使得现有密钥此成为合法长度 /// /// private byte[] GetLegalIV() { string tmpIV = _IV; _algthm.GenerateIV(); int okLen = _algthm.IV.Length; if (tmpIV.Length > okLen) { //_IV过长则截取 tmpIV = tmpIV.Substring(0, okLen); } else if (tmpIV.Length < okLen) { //_IV过短则补充 tmpIV = tmpIV.PadRight(okLen, ' '); } return ASCIIEncoding.ASCII.GetBytes(tmpIV); } } /// /// MD5加密算法 /// public static class MD5Encrp { /// /// MD5函数 /// /// 原始字符串 /// MD5结果 public static string MD5(string str) { byte[] b = Encoding.Default.GetBytes(str); b = new MD5CryptoServiceProvider().ComputeHash(b); string ret = ""; for (int i = 0; i < b.Length; i++) ret += b[i].ToString("x").PadLeft(2, '0'); return ret; } } }