123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- using System;
- using System.Data;
- using System.Text;
- using System.Security.Cryptography;
- using Microsoft.Extensions.Caching.Memory;
- using System.IO;
- namespace Common
- {
- public class Dbconn
- {
- public static void InsertCache(string key, object val)
- {
- new MemoryCache(new MemoryCacheOptions()).Set(key, val, new MemoryCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromMinutes(10)));
- }
- public static void InsertCache(string key, object val, int minutes)
- {
- new MemoryCache(new MemoryCacheOptions()).Set(key, val, new MemoryCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromMinutes(minutes)));
- }
- public static void InsertCache(string key, object val, DateTime endtime)
- {
- new MemoryCache(new MemoryCacheOptions()).Set(key, val);
- }
- /// <summary>
- /// des解密
- /// </summary>
- /// <param name="content"></param>
- /// <param name="key"></param>
- /// <returns></returns>
- public static string DesDecrypt(string content, string key = "yun1mu23")
- {
- try
- {
- content = content.Replace(" ", "+");
- DESCryptoServiceProvider des = new DESCryptoServiceProvider();
- byte[] inputByteArray = Convert.FromBase64String(content);
- des.Key = ASCIIEncoding.ASCII.GetBytes(key);
- des.IV = ASCIIEncoding.ASCII.GetBytes(key);
- MemoryStream ms = new MemoryStream();
- CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
- cs.Write(inputByteArray, 0, inputByteArray.Length);
- cs.FlushFinalBlock();
- return System.Text.Encoding.Default.GetString(ms.ToArray());
- }
- catch (Exception ex)
- {
- Function.WriteLog(DateTime.Now.ToString() + "\r\n" + content + "\r\n" + ex.ToString(), "des解密异常");
- return "{}";
- }
- }
- public static string DesEncrypt(string content, string key = "yun1mu23")
- {
- try
- {
- DESCryptoServiceProvider des = new DESCryptoServiceProvider();
- byte[] inputByteArray = System.Text.Encoding.Default.GetBytes(content);
- des.Key = ASCIIEncoding.ASCII.GetBytes(key);
- des.IV = ASCIIEncoding.ASCII.GetBytes(key);
- MemoryStream ms = new MemoryStream();
- CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
- cs.Write(inputByteArray, 0, inputByteArray.Length);
- cs.FlushFinalBlock();
- return Convert.ToBase64String(ms.ToArray());
- }
- catch (Exception ex)
- {
- Function.WriteLog(DateTime.Now.ToString() + "\r\n" + content + "\r\n" + ex.ToString(), "des加密异常");
- return "{}";
- }
- }
- public static string Encrypt3DES(string a_strString, string Key3des = "yun1mu23")
- {
- DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
- DES.Key = Encoding.UTF8.GetBytes(Key3des);
- DES.Mode = CipherMode.ECB;
- DES.Padding = PaddingMode.PKCS7;
- ICryptoTransform DESEncrypt = DES.CreateEncryptor();
- byte[] Buffer = Encoding.UTF8.GetBytes(a_strString);
- return Convert.ToBase64String(DESEncrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));
- }
- public static string Decrypt3DES(string a_strString, string Key3des = "yun1mu23")
- {
- DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
- DES.Key = Encoding.UTF8.GetBytes(Key3des);
- DES.Mode = CipherMode.ECB;
- DES.Padding = PaddingMode.PKCS7;
- ICryptoTransform DESDecrypt = DES.CreateDecryptor();
- byte[] Buffer = Convert.FromBase64String(a_strString);
- return UTF8Encoding.UTF8.GetString(DESDecrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));
- }
- public static string AesEncrypt(string str, string key, string iv)
- {
- if (string.IsNullOrEmpty(str)) return null;
- Byte[] toEncryptArray = Encoding.UTF8.GetBytes(str);
- System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged
- {
- Key = Encoding.UTF8.GetBytes(key),
- IV = Encoding.UTF8.GetBytes(iv),
- Mode = System.Security.Cryptography.CipherMode.CBC,
- Padding = System.Security.Cryptography.PaddingMode.PKCS7
- };
- System.Security.Cryptography.ICryptoTransform cTransform = rm.CreateEncryptor();
- Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
- return Convert.ToBase64String(resultArray, 0, resultArray.Length);
- }
- public static string AesDecrypt(string str, string key, string iv)
- {
- if (string.IsNullOrEmpty(str)) return null;
- Byte[] toEncryptArray = Convert.FromBase64String(str);
- System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged
- {
- Key = Encoding.UTF8.GetBytes(key),
- IV = Encoding.UTF8.GetBytes(iv),
- Mode = System.Security.Cryptography.CipherMode.CBC,
- Padding = System.Security.Cryptography.PaddingMode.PKCS7
- };
- System.Security.Cryptography.ICryptoTransform cTransform = rm.CreateDecryptor();
- Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
- return Encoding.UTF8.GetString(resultArray);
- }
- }
- }
|