C#加密类

实现代码:

var es= EncryptSugar.GetInstance();
string word = "abc";
var wordEncrypt = es.Encrypto(word);//加密
var wordDecrypt = es.Decrypto(wordEncrypt);//解密
var wordMd5 = es.MD5(word);//md5加密

封装类:

using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.Xml;
using System.IO;
using System.Linq;
namespace SyntacticSugar
{
    /// <summary>
    /// ** 描述:加密类
    /// ** 创始时间:2015-6-30
    /// ** 修改时间:-
    /// ** 作者:sunkaixuan
    /// ** 使用说明:http://www.cnblogs.com/sunkaixuan/p/4610729.html
    /// </summary>
    public class EncryptSugar
    {
        private static readonly object _instanceLock = new object();
        private static EncryptSugar _instance = null;
        private static SymmetricAlgorithm mobjCryptoService;
        private static string _key;
        private static readonly object _cacheLock = new object();
        private static Dictionary<string, string> _cache = new Dictionary<string, string>();
        /// <summary>
        /// 最大缓存条数
        /// </summary>
        private static int _maxCacheNum = 10000;
 
        /// <summary>
        /// 对称加密类的构造函数
        /// </summary>
        public static EncryptSugar GetInstance()
        {
            if (_instance == null)
            {
                lock (_instanceLock)
                {
                    if (_instance == null)
                    {
                        mobjCryptoService = new RijndaelManaged();
                        _key = "Guz(%&as1213^^d(fa%(HilJ$lhj!y6&(*jkP87jH7";
                        _instance = new EncryptSugar();
                    }
                }
            }
            return _instance;
        }
 
 
 
        /// <summary>
        /// 加密方法
        /// </summary>
        /// <param name="Source">待加密的串</param>
        /// <returns>经过加密的串</returns>
        public string Encrypto(string source)
        {
            if (_cache.ContainsKey(source))
            {
                return _cache[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();
            string reval = Convert.ToBase64String(bytOut);
            lock (_cacheLock)
            {
                if (_cache.Count > _maxCacheNum)
                {
                    foreach (var it in _cache.Take(_maxCacheNum/5))
                    {
 
                        _cache.Remove(it.Key);
 
                    }
                }
                _cache.Add(source, reval);
            }
            return reval; ;
 
        }
 
        /// <summary>
        /// 解密方法
        /// </summary>
        /// <param name="Source">待解密的串</param>
        /// <returns>经过解密的串</returns>
        public string Decrypto(string source)
        {
            lock (_cacheLock)
            {
                if (_cache.Any(it => it.Value == source))
                {
                    return _cache.Single(it => it.Value == source).Key;
                }
            }
 
            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();
 
        }
 
        /// <summary>
        /// MD5加密,不可逆
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public string MD5(string source)
        {
            return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(source, "MD5");
        }
 
        #region 私有函数
        /// <summary>
        /// 获得密钥
        /// </summary>
        /// <returns>密钥</returns>
        private byte[] GetLegalKey()
        {
 
            string sTemp = _key;
 
            mobjCryptoService.GenerateKey();
 
            byte[] bytTemp = mobjCryptoService.Key;
 
            int KeyLength = bytTemp.Length;
 
            if (sTemp.Length > KeyLength)
 
                sTemp = sTemp.Substring(0, KeyLength);
 
            else if (sTemp.Length < KeyLength)
 
                sTemp = sTemp.PadRight(KeyLength, ' ');
 
            return ASCIIEncoding.ASCII.GetBytes(sTemp);
 
        }
 
        /// <summary>
        /// 获得初始向量IV
        /// </summary>
        /// <returns>初试向量IV</returns>
        private byte[] GetLegalIV()
        {
 
            string sTemp = "asdfas&&dfg*$#+)*Y41sdgsdgs&*%$$$^&&GGslsadKdfK1";
 
            mobjCryptoService.GenerateIV();
 
            byte[] bytTemp = mobjCryptoService.IV;
 
            int IVLength = bytTemp.Length;
 
            if (sTemp.Length > IVLength)
 
                sTemp = sTemp.Substring(0, IVLength);
 
            else if (sTemp.Length < IVLength)
 
                sTemp = sTemp.PadRight(IVLength, ' ');
 
            return ASCIIEncoding.ASCII.GetBytes(sTemp);
 
        }
        #endregion
    }
 
}

 

private SymmetricAlgorithm EncryptionService;
        private string Key, IV;
        /// 
        /// 对称加密类的构造函数
        /// 
        public DesEncry()
        {
            EncryptionService = new RijndaelManaged();
            Key = "e229bc02c9572d3b786c29edf93987701ca63d08d86fff5bc594cac5d0cd9782";
            IV = "8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92";
        }
        /// <summary>
        /// 对称加密类的构造函数,实例化初始化Key,Iv值
        /// </summary>
        /// <param name="key">密匙</param>
        public DesEncry(string key)
        {
            EncryptionService = new RijndaelManaged();
            Key = key; // 任意字符串
            IV = "8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92";
        }
        /// <summary>
        /// 对称加密类的构造函数,实例化初始化Key,Iv值
        /// </summary>
        /// <param name="key">密匙</param>
        /// <param name="iv">向量IV</param>
        public DesEncry(string key, string iv)
        {
            EncryptionService = new RijndaelManaged();
            Key = key;
            IV = iv;
        }

        /// <summary>
        /// 写入或得到自定义的密匙KEY
        /// </summary>
        public string key
        {
            set
            {
                Key = value;
            }
            get
            {
                return Key;
            }
        }

        /// <summary>
        /// 写入或得到自定义的向量IV
        /// </summary>
        public string iv
        {
            set
            {
                IV = value;
            }
            get
            {
                return IV;
            }
        }

        /// <summary>
        /// 获得密钥
        /// </summary>
        /// <returns>密钥</returns>
        private byte[] GetKey()
        {
            string sTemp = Key;
            EncryptionService.GenerateKey();
            byte[] bytTemp = EncryptionService.Key;
            int KeyLength = bytTemp.Length;
            if (sTemp.Length > KeyLength)
                sTemp = sTemp.Substring(0, KeyLength);
            else if (sTemp.Length < KeyLength)
                sTemp = sTemp.PadRight(KeyLength, ' ');
            return ASCIIEncoding.ASCII.GetBytes(sTemp);
        }
        /// <summary>
        /// 获得初始向量IV
        /// </summary>
        /// <returns>初始向量IV</returns>
        private byte[] GetIV()
        {
            string sTemp = IV;
            EncryptionService.GenerateIV();
            byte[] bytTemp = EncryptionService.IV;
            int IVLength = bytTemp.Length;
            if (sTemp.Length > IVLength)
                sTemp = sTemp.Substring(0, IVLength);
            else if (sTemp.Length < IVLength)
                sTemp = sTemp.PadRight(IVLength, ' ');
            return ASCIIEncoding.ASCII.GetBytes(sTemp);
        }
        /// <summary>
        /// 加密方法
        /// </summary>
        /// <param name="Source">待加密的串</param>
        /// <returns>经过加密的串</returns>
        public string Encrypto(string Source)
        {
            byte[] bytIn = UTF8Encoding.UTF8.GetBytes(Source);
            MemoryStream ms = new MemoryStream();
            EncryptionService.Key = GetKey();
            EncryptionService.IV = GetIV();
            ICryptoTransform encrypto = EncryptionService.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);
            EncryptionService.Key = GetKey();
            EncryptionService.IV = GetIV();
            ICryptoTransform encrypto = EncryptionService.CreateDecryptor();
            CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read);
            StreamReader sr = new StreamReader(cs);
            return sr.ReadToEnd();
        }

 

posted on 2017-05-09 16:26  记性特差  阅读(188)  评论(0编辑  收藏  举报