[C#] TrippleDES加密 →→→→→进入此内容的聊天室

来自 , 2020-01-07, 写在 C#, 查看 111 次.
URL http://www.code666.cn/view/d790c9e6
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Security.Cryptography;
  6. using System.Text;
  7.  
  8. namespace COMMON.DEncrypt
  9. {
  10.     public class TDESCSPEncrypt
  11.     {
  12.         //12个字符  
  13.         private static string customIV = "jfYi3ggEEV4=";//"4vHKRj3yfzU=";
  14.         //32个字符  
  15.         private static string customKey = "9DQrNW0KBi1LLGok6ayQtvuZucoXtLb0";//"xhVs6DRXLfUGxw+AhtfQdpQGoa+8SA9d";
  16.  
  17.         /// <summary>  
  18.         /// 加密字符串  
  19.         /// </summary>  
  20.         /// <param name="password"></param>  
  21.         /// <returns></returns>  
  22.         public static string EncryptPassword(string password, string key_Base64, string iv_Base64)
  23.         {
  24.             string encryptPassword = string.Empty;
  25.  
  26.             SymmetricAlgorithm algorithm = new TripleDESCryptoServiceProvider();
  27.             algorithm.Key = Convert.FromBase64String(key_Base64);
  28.             algorithm.IV = Convert.FromBase64String(iv_Base64);
  29.             algorithm.Mode = CipherMode.ECB;
  30.             algorithm.Padding = PaddingMode.PKCS7;
  31.  
  32.             ICryptoTransform transform = algorithm.CreateEncryptor();
  33.  
  34.             byte[] data = Encoding.Unicode.GetBytes(password);
  35.             MemoryStream memoryStream = new MemoryStream();
  36.             CryptoStream cryptoStream = new CryptoStream(memoryStream, transform, CryptoStreamMode.Write);
  37.  
  38.             cryptoStream.Write(data, 0, data.Length);
  39.             cryptoStream.FlushFinalBlock();
  40.             encryptPassword = Convert.ToBase64String(memoryStream.ToArray());
  41.  
  42.             memoryStream.Close();
  43.             cryptoStream.Close();
  44.  
  45.             return encryptPassword;
  46.         }
  47.  
  48.         /// <summary>  
  49.         /// 解密字符串  
  50.         /// </summary>  
  51.         /// <param name="encryptPassword"></param>  
  52.         /// <returns></returns>  
  53.         public static string DecryptPassword(string encryptPassword, string key_Base64, string iv_Base64)
  54.         {
  55.             string decryptPassword = string.Empty;
  56.  
  57.             SymmetricAlgorithm algorithm = new TripleDESCryptoServiceProvider();
  58.             algorithm.Key = Convert.FromBase64String(key_Base64);
  59.             algorithm.IV = Convert.FromBase64String(iv_Base64);
  60.             algorithm.Mode = CipherMode.ECB;
  61.             algorithm.Padding = PaddingMode.PKCS7;
  62.  
  63.             ICryptoTransform transform = algorithm.CreateDecryptor(algorithm.Key, algorithm.IV);
  64.  
  65.             byte[] buffer = Convert.FromBase64String(encryptPassword);
  66.             MemoryStream memoryStream = new MemoryStream(buffer);
  67.             CryptoStream cryptoStream = new CryptoStream(memoryStream, transform, CryptoStreamMode.Read);
  68.             StreamReader reader = new StreamReader(cryptoStream, Encoding.Unicode);
  69.             decryptPassword = reader.ReadToEnd();
  70.  
  71.             reader.Close();
  72.             cryptoStream.Close();
  73.             memoryStream.Close();
  74.  
  75.             return decryptPassword;
  76.         }
  77.  
  78.         public static string EncryptPassword(string password, string key_Base64)
  79.         {
  80.             return EncryptPassword(password, key_Base64, customIV);
  81.         }
  82.  
  83.         public static string DecryptPassword(string encryptPassword, string key_Base64)
  84.         {
  85.             return DecryptPassword(encryptPassword, key_Base64, customIV);
  86.         }
  87.  
  88.         public static string EncryptPassword(string password)
  89.         {
  90.             return EncryptPassword(password, customKey, customIV);
  91.         }
  92.  
  93.         public static string DecryptPassword(string encryptPassword)
  94.         {
  95.             return DecryptPassword(encryptPassword, customKey, customIV);
  96.         }
  97.  
  98.         static TripleDESCryptoServiceProvider _DCSP;
  99.         private static TripleDESCryptoServiceProvider DCSP
  100.         {
  101.             get
  102.             {
  103.                 if (_DCSP == null)
  104.                     _DCSP = new TripleDESCryptoServiceProvider();
  105.                 //
  106.                 return _DCSP;
  107.             }
  108.         }
  109.  
  110.         public static string CreateKEY()
  111.         {
  112.             DCSP.GenerateKey();
  113.             return Convert.ToBase64String(DCSP.Key);
  114.         }
  115.     }
  116. }
  117.  

回复 "TrippleDES加密"

这儿你可以回复上面这条便签

captcha