码迷,mamicode.com
首页 > 其他好文 > 详细

DES加密解密

时间:2014-08-21 16:34:14      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   color   os   io   ar   div   

   /// <summary>
        /// DES加密
        /// </summary>
        /// <param name="sourceString">需要加密字符串</param>
        /// <param name="key">加密密钥</param>
        /// <param name="iv"></param>
        /// <returns></returns>
        public static string Encrypt(string sourceString, string key, string iv)
        {
            try
            {
                key = key.Substring(0,8);
                iv = iv.Substring(0, 8);
                byte[] btKey = Encoding.UTF8.GetBytes(key);

                byte[] btIV = Encoding.UTF8.GetBytes(iv);

                DESCryptoServiceProvider des = new DESCryptoServiceProvider();

                using (MemoryStream ms = new MemoryStream())
                {
                    byte[] inData = Encoding.UTF8.GetBytes(sourceString);
                    try
                    {
                        using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(btKey, btIV), CryptoStreamMode.Write))
                        {
                            cs.Write(inData, 0, inData.Length);

                            cs.FlushFinalBlock();
                        }

                        return Convert.ToBase64String(ms.ToArray());
                    }
                    catch
                    {
                        return sourceString;
                    }
                }
            }
            catch(Exception ex) { return ex+"cath DES加密出错"; }

        }
        /// <summary>
        /// DES解密字符串
        /// </summary>
        /// <param name="encryptedString"></param>
        /// <param name="key"></param>
        /// <param name="iv"></param>
        /// <returns></returns>
        public static string Decrypt(string encryptedString, string key, string iv)
        {
            key = key.Substring(0, 8);
            iv = iv.Substring(0, 8);
            byte[] btKey = Encoding.UTF8.GetBytes(key);

            byte[] btIV = Encoding.UTF8.GetBytes(iv);

            DESCryptoServiceProvider des = new DESCryptoServiceProvider();

            using (MemoryStream ms = new MemoryStream())
            {
                byte[] inData = Convert.FromBase64String(encryptedString);
                try
                {
                    using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(btKey, btIV), CryptoStreamMode.Write))
                    {
                        cs.Write(inData, 0, inData.Length);

                        cs.FlushFinalBlock();
                    }

                    return Encoding.UTF8.GetString(ms.ToArray());
                }
                catch
                {
                    return encryptedString;
                }
            }
        }  

 

DES加密解密,布布扣,bubuko.com

DES加密解密

标签:des   style   blog   color   os   io   ar   div   

原文地址:http://www.cnblogs.com/yhdkzy/p/3927165.html

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