zoukankan      html  css  js  c++  java
  • DES加密解决算法

         /// <summary>
            /// DES加密算法
            /// sKey为8位或16位
            /// </summary>
            /// <param name="pToEncrypt">需要加密的字符串</param>
            /// <param name="sKey">密钥</param>
            /// <returns></returns>
            public static string DesEncryptStr(string pToEncrypt, string sKey)
            {
                StringBuilder ret = new StringBuilder();
    
                 
                    DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                    byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
                    des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
                    des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
                    MemoryStream ms = new MemoryStream();
                    CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
                    cs.Write(inputByteArray, 0, inputByteArray.Length);
                    cs.FlushFinalBlock();
    
                    foreach (byte b in ms.ToArray())
                    {
                        ret.AppendFormat("{0:X2}", b);
                    }
                    ret.ToString();
                 
                return ret.ToString();
                //return a;
            }
    /// <summary> /// DES解密算法 /// sKey为8位或16位 /// </summary> /// <param name="pToDecrypt">需要解密的字符串</param> /// <param name="sKey">密钥</param> /// <returns></returns> public static string DesDecryptStr(string pToDecrypt, string sKey) { MemoryStream ms = new MemoryStream(); 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 = ASCIIEncoding.ASCII.GetBytes(sKey); des.IV = ASCIIEncoding.ASCII.GetBytes(sKey); CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); StringBuilder ret = new StringBuilder(); return System.Text.Encoding.Default.GetString(ms.ToArray()); }
  • 相关阅读:
    经典背景音乐集(转)
    商业模式的思考
    PHP5.4的变化关注What has changed in PHP 5.4.x
    yii模版中的写法
    设计模式(一)工厂模式Factory(创建型)
    yii模版中的判断方法
    Yacc 与 Lex 快速入门(词法分析和语法分析)
    Windows PHP 中 VC6 X86 和 VC9 X86 的区别及 Non Thread Safe 的意思
    金融系列1《借贷记卡介绍》
    设计模式概论
  • 原文地址:https://www.cnblogs.com/Xanthus/p/9578202.html
Copyright © 2011-2022 走看看