C# Des加密解密

        private static string key = "VSQAdmin";//密文 加密解密是都需要密文且保持一致

        private static string iv = "VSQAdmin";//加密解密时key 、 iv 中传入的key、iv值一致




        /// <summary>
        /// Des解密
        /// </summary>
        /// <param name="pToDecrypt"></param>
        /// <returns></returns>
        public  static string DecryptString(string pToDecrypt)
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
            for (int x = 0; x < pToDecrypt.Length / 2; x++)
            {
                int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
                inputByteArray[x] = (byte)i;
            }

            des.Key = UTF8Encoding.UTF8.GetBytes(key);//***************key与加密时的Key保持一致
            des.IV = UTF8Encoding.UTF8.GetBytes(iv);//*****************skey与加密时的IV保持一致

            MemoryStream ms = new MemoryStream();

            CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();

            StringBuilder ret = new StringBuilder();
            return Encoding.UTF8.GetString(ms.ToArray());
        }
        /// <summary>
        /// Des加密
        /// </summary>
        /// <param name="encryptString"></param>
        /// <returns></returns>
        public static string Des(string encryptString)
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();

            byte[] sor = Encoding.UTF8.GetBytes(encryptString);
            //传入key、iv
            des.Key = UTF8Encoding.UTF8.GetBytes(key);
            des.IV = UTF8Encoding.UTF8.GetBytes(iv);

            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);

            cs.Write(sor, 0, sor.Length);
            cs.FlushFinalBlock();

            StringBuilder sb = new StringBuilder();
            foreach (byte b in ms.ToArray())
            {
                sb.AppendFormat("{0:X2}", b);
            }
            return sb.ToString();
        }

  

private static string key = "VSQAdmin";//密文 加密解密是都需要密文且保持一致      private static string iv = "VSQAdmin";//加密解密时key 、 iv 中传入的key、iv值一致      /// <summary> /// Des解密 /// </summary> /// <param name="pToDecrypt"></param> /// <returns></returns> public static string DecryptString(string pToDecrypt) { DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte[] inputByteArray = new byte[pToDecrypt.Length / 2]; for (int x = 0; x < pToDecrypt.Length / 2; x++) { int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16)); inputByteArray[x] = (byte)i; } des.Key = UTF8Encoding.UTF8.GetBytes(key);//***************key与加密时的Key保持一致 des.IV = UTF8Encoding.UTF8.GetBytes(iv);//*****************skey与加密时的IV保持一致 MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); StringBuilder ret = new StringBuilder(); return Encoding.UTF8.GetString(ms.ToArray()); } /// <summary> /// Des加密 /// </summary> /// <param name="encryptString"></param> /// <returns></returns> public static string Des(string encryptString) { DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte[] sor = Encoding.UTF8.GetBytes(encryptString); //传入key、iv des.Key = UTF8Encoding.UTF8.GetBytes(key); des.IV = UTF8Encoding.UTF8.GetBytes(iv); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write); cs.Write(sor, 0, sor.Length); cs.FlushFinalBlock(); StringBuilder sb = new StringBuilder(); foreach (byte b in ms.ToArray()) { sb.AppendFormat("{0:X2}", b); } return sb.ToString(); }

  

C# Des加密解密

上一篇:delphi真正实现延时暂停功能


下一篇:ThinkCMF后台验证码不显示,无法登陆怎么办?