C#加密类
1 2 3 4 5 | var es= EncryptSugar.GetInstance(); string word = "abc" ; var wordEncrypt = es.Encrypto(word); //加密 var wordDecrypt = es.Decrypto(wordEncrypt); //解密 var wordMd5 = es.MD5(word); //md5加密 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 | 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 } } |
分类:
C#语法糖
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?