• 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(); }

      

  • 相关阅读:
    Android:Unable to find explicit activity class报错
    家庭记账本2
    家庭记账本开发
    Android Studio中TableLayout(表格布局)
    Android Studio中View与ViewGroup的概念
    Android Studio反编译APK获取代码&资源
    Android程序签名打包
    Android Studio工程相关解析(各种文件,资源访问)
    Android Studio 的SDK更新不了问题解决
    模块
  • 原文地址:https://www.cnblogs.com/wmm0105/p/12467933.html
Copyright © 2020-2023  润新知