码迷,mamicode.com
首页 > 编程语言 > 详细

DES加密解决算法

时间:2018-09-03 15:02:36      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:foreach   sde   esc   nal   pts   解决   pre   new   加密算法   

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

 

DES加密解决算法

标签:foreach   sde   esc   nal   pts   解决   pre   new   加密算法   

原文地址:https://www.cnblogs.com/Xanthus/p/9578202.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!