string t = DateTime.Now.Ticks.ToString(); t = DESKey.DESEncrypt(t, DESKey.DesKeyStr); string[] strid = new string[t.Length];// for (int i = 0; i < t.Length; i++)//把字符赋给数组 { strid[i] = t.Substring(i, 1); } string s = ""; Random rdid = new Random(); for (int i = 0; i < 9; i++)//从数组随机抽取字符组成新的字符生成机器三 { s += strid[rdid.Next(0, strid.Length)]; }
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 | class DESKey { public const string DesKeyStr = "BLUE2013" ; #region DES加密 /// <summary> /// DES加密 /// </summary> /// <param name="pToEncrypt">需要加密的字符串</param> /// <returns>加密后的字符串</returns> public static string DESEncrypt( string pToEncrypt, string DesKeyStr) { try { DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte [] inputByteArray = Encoding.Default.GetBytes(pToEncrypt); des.Key = ASCIIEncoding.ASCII.GetBytes(DesKeyStr); des.IV = ASCIIEncoding.ASCII.GetBytes(DesKeyStr); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); StringBuilder ret = new StringBuilder(); foreach ( byte b in ms.ToArray()) { ret.AppendFormat( "{0:X2}" , b); } ret.ToString(); return ret.ToString(); } catch { return "" ; } } #endregion #region DES解密 /// <summary> /// DES解密 /// </summary> /// <param name="pToDecrypt">加密后的字符串</param> /// <returns>解密后的字符串</returns> public static string DESDecrypt( string pToDecrypt, string DesKeyStr) { try { DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte [] inputByteArray = new byte [pToDecrypt.Length / 2]; for ( int x = 0; x < pToDecrypt.Length / 2; x++) { int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16)); inputByteArray[x] = ( byte )i; } des.Key = ASCIIEncoding.ASCII.GetBytes(DesKeyStr); des.IV = ASCIIEncoding.ASCII.GetBytes(DesKeyStr); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); StringBuilder ret = new StringBuilder(); return System.Text.Encoding.Default.GetString(ms.ToArray()); } catch { return "" ; } } #endregion } |
转自:http://Www.CnBlogs.Com/WebEnh/
如果想下次快速找到我,记得点下面的关注哦!
本博客Android APP 下载 |
![]() |
支持我们就给我们点打赏 |
![]() |
支付宝打赏 支付宝扫一扫二维码 |
![]() |
微信打赏 微信扫一扫二维码 |
![]() |
如果想下次快速找到我,记得点下面的关注哦!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!