123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- using System;
- using System.IO;
- using System.Security.Cryptography;
- using System.Text;
- namespace WWPipeLine.Commons
- {
- /// <summary>
- /// 扩展静态类
- /// </summary>
- public static class ExtendedUtils
- {
- private static string encryptKey = Guid.NewGuid().ToString("").Substring(0, 4);
- private static string encryptValue = "sxzy";
- public static string Encrypt(string str, string keyStr = "")
- {
- try
- {
- DESCryptoServiceProvider descsp = new DESCryptoServiceProvider();
- if (string.IsNullOrEmpty(keyStr))
- {
- keyStr = encryptKey;
- }
- byte[] key = Encoding.Unicode.GetBytes(keyStr);
- byte[] data = Encoding.Unicode.GetBytes(str);
- MemoryStream MStream = new MemoryStream();
- CryptoStream CStream = new CryptoStream(MStream, descsp.CreateEncryptor(key, key), CryptoStreamMode.Write);
- CStream.Write(data, 0, data.Length);
- CStream.FlushFinalBlock();
- return Convert.ToBase64String(MStream.ToArray()) + keyStr;
- }
- catch (Exception ex)
- {
- Commons.LogHelper.Error(ex);
- }
- return string.Empty;
- }
- //字符串解密
- public static string Decrypt(string str, string keyStr = "")
- {
- DESCryptoServiceProvider descsp = new DESCryptoServiceProvider();
- try
- {
- if (string.IsNullOrEmpty(keyStr))
- {
- keyStr = encryptKey;
- }
- byte[] key = Encoding.Unicode.GetBytes(keyStr);
- byte[] data = Convert.FromBase64String(str);
- MemoryStream MStream = new MemoryStream();
- CryptoStream CStram = new CryptoStream(MStream, descsp.CreateDecryptor(key, key), CryptoStreamMode.Write);
- CStram.Write(data, 0, data.Length);
- CStram.FlushFinalBlock();
- return Encoding.Unicode.GetString(MStream.ToArray());
- }
- catch (Exception ex)
- {
- Commons.LogHelper.Error(ex);
- }
- return string.Empty;
- }
- public static string Encrypts(string str)
- {
- try
- {
- DESCryptoServiceProvider descsp = new DESCryptoServiceProvider();
- byte[] key = Encoding.Unicode.GetBytes(encryptValue);
- byte[] data = Encoding.Unicode.GetBytes(str);
- MemoryStream MStream = new MemoryStream();
- CryptoStream CStream = new CryptoStream(MStream, descsp.CreateEncryptor(key, key), CryptoStreamMode.Write);
- CStream.Write(data, 0, data.Length);
- CStream.FlushFinalBlock();
- return Convert.ToBase64String(MStream.ToArray());
- }
- catch (Exception ex)
- {
- Commons.LogHelper.Error(ex);
- }
- return string.Empty;
- }
- //字符串解密
- public static string Decrypts(string str)
- {
- DESCryptoServiceProvider descsp = new DESCryptoServiceProvider();
- try
- {
- byte[] key = Encoding.Unicode.GetBytes(encryptValue);
- byte[] data = Convert.FromBase64String(str);
- MemoryStream MStream = new MemoryStream();
- CryptoStream CStram = new CryptoStream(MStream, descsp.CreateDecryptor(key, key), CryptoStreamMode.Write);
- CStram.Write(data, 0, data.Length);
- CStram.FlushFinalBlock();
- return Encoding.Unicode.GetString(MStream.ToArray());
- }
- catch (Exception ex)
- {
- Commons.LogHelper.Error(ex);
- }
- return string.Empty;
- }
- }
- }
|