zoukankan      html  css  js  c++  java
  • 数据操作DES加密和解密

     #region 数据操作DES加密和解密
        /// <summary>
        /// DES加密字符串
        /// </summary>
        /// <param name="encryptString"> 待加??密??的??字??符??串?? </param>
        /// <param name="encryptKey"> 加??密??密??钥?,要?a求??为a8位? </param>
        /// <returns> 加??密??成??功|返???回?加??密??后??的??字??符??串??,??失???败???返???回?源??串?? </returns>
        public static string EncryptDES( string encryptString, string encryptKey)
        {
            byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
            try
            {
                byte[] rgbKey = Encoding .UTF8.GetBytes(encryptKey.Substring(0, 8));
                byte[] rgbIV = Keys;
                byte[] inputByteArray = Encoding .UTF8.GetBytes(encryptString);
                DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                return Convert .ToBase64String(mStream.ToArray());
            }
            catch
            {
                return encryptString;
            }
        }
     
     
     
        /// <summary>
        /// DES解a密??字??符??串??
        /// </summary>
        /// <param name="decryptString"> 待?y解a密??的??字??符??串?? </param>
        /// <param name="decryptKey"> 解a密??密??钥?,要?a求??为a8位?,和??加??密??密??钥?相??同?? </param>
        /// <returns> 解a密??成??功|返???回?解a密??后??的??字??符??串??,??失???败???返???源??串?? </returns>
        public static string DecryptDES( string decryptString, string decryptKey)
        {
            byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
     
            try
            {
                byte[] rgbKey = Encoding .UTF8.GetBytes(decryptKey);
                byte[] rgbIV = Keys;
                byte[] inputByteArray = Convert .FromBase64String(decryptString);
                DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                return Encoding .UTF8.GetString(mStream.ToArray());
            }
            catch
            {
                return decryptString;
            }
        }
     
        #endregion
     
     
     
      /// <summary>
        /// DES解a密??字??符??串??
        /// </summary>
        /// <param name="decryptString"> 待解密的字符串 </param>
        /// <param name="decryptKey"> 解密密钥要求为8位和加密密钥相同</param>
        /// <returns> 解a密成功|返回?解a密??后??的??字??符??串??,??失???败???返???源??串?? </returns>
        public static string DecryptDES( string decryptString, string decryptKey)
        {
            byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
     
            try
            {
                byte[] rgbKey = Encoding .UTF8.GetBytes(decryptKey);
                byte[] rgbIV = Keys;
                byte[] inputByteArray = Convert .FromBase64String(decryptString);
                DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                return Encoding .UTF8.GetString(mStream.ToArray());
            }
            catch
            {
                return decryptString;
            }
        }
     
        #endregion
  • 相关阅读:
    FindWindowEx使用方法
    什么是VSync
    getParameter
    高等数学积分公式大全
    为什么没有好用的Android游戏引擎?
    Best Time to Buy and Sell Stock III
    5.3 适配器模式(4.1)
    在动态网络下实现分布式共享存储
    人类智商一般在多少左右?爱因斯坦的智商是多少?
    海量数据处理面试题集锦
  • 原文地址:https://www.cnblogs.com/BungeeJumping/p/3397300.html
Copyright © 2011-2022 走看看