DES加密解密

本文提供了一个使用C#实现的DES加密与解密方法的示例代码。该示例展示了如何通过指定密钥和向量来加密字符串,并能够正确地进行解密还原。此外,还提供了完整的运行结果。
public class DESDemo
    {
        /// <summary>
        /// DES加密方法
        /// </summary>
        /// <param name="text">明文</param>
        /// <param name="sKey">密钥</param>
        /// <param name="iv">向量</param>
        /// <returns>加密后的密文</returns>
        public static string DESEncrypt(string text, string sKey,string iv)
        {
            using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
            {
                byte[] byteArray;
                byteArray = Encoding.Default.GetBytes(text);
                des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.
                    HashPasswordForStoringInConfigFile(sKey, "MD5").Substring(0, 8));
                des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.
                    HashPasswordForStoringInConfigFile(iv, "MD5").Substring(0, 8));
                using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
                {
                    using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(byteArray, 0, byteArray.Length);
                        cs.FlushFinalBlock();
                        //StringBuilder ret = new StringBuilder();
                        //foreach (byte b in ms.ToArray())
                        //{
                        //    ret.AppendFormat("{0:X2}", b);
                        //}
                        return Convert.ToBase64String(ms.ToArray());
                    }
                }
            } 
        }
        /// <summary>
        /// DES解密方法
        /// </summary>
        /// <param name="text">密文</param>
        /// <param name="sKey">密钥</param>
        /// <param name="iv">向量</param>
        /// <returns>解密后的明文</returns>
        public static string DESDecrypt(string text, string sKey,string iv)
        {
            using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
            {
                //int len;
                //len = text.Length / 2;
                //byte[] byteArray = new byte[len];
                //int x, i;
                //for (x = 0; x < len; x++)
                //{
                //    i = Convert.ToInt32(text.Substring(x * 2, 2), 16);
                //    byteArray[x] = (byte)i;
                //}
                byte[] byteArray;
                byteArray = Convert.FromBase64String(text);
                try
                {
                    des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.
                        HashPasswordForStoringInConfigFile(sKey, "MD5").Substring(0, 8));
                    des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.
                        HashPasswordForStoringInConfigFile(iv, "MD5").Substring(0, 8));
                    using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
                    {
                        using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write))
                        {
                            cs.Write(byteArray, 0, byteArray.Length);
                            cs.FlushFinalBlock();
                            string estring = Encoding.Default.GetString(ms.ToArray());
                            return estring;
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }
    }
static void Main(string[] args)
        {
            var text = DESDemo.DESEncrypt("你我他", "key", "key");
            DESDemo.DESDecrypt(text, "key","key");
            Console.WriteLine("加密后:"+text);
            Console.WriteLine("解密后:"+DESDemo.DESDecrypt(text, "key", "key"));
            Console.ReadKey();
        }

运行结果:

 

转载于:https://www.cnblogs.com/z-huan/p/7428303.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值