zoukankan      html  css  js  c++  java
  • TrippleDESCSPEncrypt 加密解密试试看

        public class TrippleDESCSPEncrypt
        {
            //12个字符  
            private static string customIV = "4vHKRj3yfzU=";
            //32个字符  
            private static string customKey = "xhVs6DRXLfUGxw+AhtfQdpQGoa+8SA9d";
    
            /// <summary>  
            /// 加密字符串  
            /// </summary>  
            /// <param name="password"></param>  
            /// <returns></returns>  
            public string EncryptPassword(string password)
            {
                string encryptPassword = string.Empty;
    
                SymmetricAlgorithm algorithm = new TripleDESCryptoServiceProvider();
                algorithm.Key = Convert.FromBase64String(customKey);
    
                algorithm.IV = Convert.FromBase64String(customIV);//在ECB模式下,IV不起作用
                algorithm.Mode = CipherMode.ECB;
                algorithm.Padding = PaddingMode.PKCS7;
    
                ICryptoTransform transform = algorithm.CreateEncryptor();
    
                byte[] data = (new System.Text.ASCIIEncoding()).GetBytes(password);
                MemoryStream memoryStream = new MemoryStream();
                CryptoStream cryptoStream = new CryptoStream(memoryStream, transform, CryptoStreamMode.Write);
    
                cryptoStream.Write(data, 0, data.Length);
                cryptoStream.FlushFinalBlock();
                encryptPassword = Convert.ToBase64String(memoryStream.ToArray());
    
                memoryStream.Close();
                cryptoStream.Close();
    
                return encryptPassword;
            }
    
            /// <summary>  
            /// 解密字符串  
            /// </summary>  
            /// <param name="password"></param>  
            /// <returns></returns>  
            public string DecryptPassword(string password)
            {
                string decryptPassword = string.Empty;
    
                SymmetricAlgorithm algorithm = new TripleDESCryptoServiceProvider();
                algorithm.Key = Convert.FromBase64String(customKey);
                algorithm.IV = Convert.FromBase64String(customIV);//在ECB模式下不起作用
                algorithm.Mode = CipherMode.ECB;
                algorithm.Padding = PaddingMode.PKCS7;
    
                ICryptoTransform transform = algorithm.CreateDecryptor(algorithm.Key, algorithm.IV);
           //ICryptoTransform transform = algorithm.CreateDecryptor();//在ECB模式下也可以使用此句。
            
    byte[] buffer = Convert.FromBase64String(password); MemoryStream memoryStream = new MemoryStream(buffer); CryptoStream cryptoStream = new CryptoStream(memoryStream, transform, CryptoStreamMode.Read); StreamReader reader = new StreamReader(cryptoStream, System.Text.Encoding.ASCII); decryptPassword = reader.ReadToEnd(); reader.Close(); cryptoStream.Close(); memoryStream.Close(); return decryptPassword; }
  • 相关阅读:
    使用批处理脚本在win10系统启动Redis 5.0.10
    异常分析 JedisConnectionException: java.net.SocketTimeoutException: Read timed out
    Spring Boot基于redis分布式锁模拟直播秒杀场景
    管理的经验二
    第三方api接口
    接口测试总结
    测试框架的基本能力
    接口测试的价值
    面试的经验
    管理的经验
  • 原文地址:https://www.cnblogs.com/wolfocme110/p/4594647.html
Copyright © 2011-2022 走看看