zoukankan      html  css  js  c++  java
  • C# DES 加密 解密

     //注意:密钥必须为8位
            private const string m_strEncryptKey = "abcd1234";
            /// <summary>
            /// 加密字符串
            /// </summary>
            /// <param name="p_strInput">明码</param>
            /// <returns>加密后的密码</returns>
            public static string DesEncrypt(string p_strInput)
            {
                byte[] byKey = null;
                byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
                try
                {
                    byKey = System.Text.Encoding.UTF8.GetBytes(m_strEncryptKey.Substring(0, 8));
                    DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                    byte[] inputByteArray = Encoding.UTF8.GetBytes(p_strInput);
                    MemoryStream ms = new MemoryStream();
                    CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);
                    cs.Write(inputByteArray, 0, inputByteArray.Length);
                    cs.FlushFinalBlock();
                    return Convert.ToBase64String(ms.ToArray());
                }
                catch (System.Exception ex)
                {
                    throw (ex);
                }
            }
    
            /// <summary>
            /// 解密字符串
            /// </summary>
            /// <param name="p_strInput">加了密的字符串</param>
            public static string DesDecrypt(string p_strInput)
            {
                byte[] byKey = null;
                byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
                byte[] inputByteArray = new Byte[p_strInput.Length];
    
                try
                {
                    byKey = System.Text.Encoding.UTF8.GetBytes(m_strEncryptKey.Substring(0, 8));
                    DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                    inputByteArray = Convert.FromBase64String(p_strInput);
                    MemoryStream ms = new MemoryStream();
                    CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
                    cs.Write(inputByteArray, 0, inputByteArray.Length);
                    cs.FlushFinalBlock();
                    System.Text.Encoding encoding = new System.Text.UTF8Encoding();
                    return encoding.GetString(ms.ToArray());
                }
                catch (System.Exception ex)
                {
                    throw (ex);
                }
            }
  • 相关阅读:
    .net编程扫盲(*)
    接口编程扫盲(多态)
    (转)栈与堆栈的区别
    (转).NET基础拾遗(5)多线程开发基础
    (转)你应该知道的计算机网络知识
    网络代理的基础知识
    某代理网站免费IP地址抓取测试
    常用Maven插件介绍
    Maven打jar发布包的常用配置
    Apache Commons CLI 开发命令行工具示例
  • 原文地址:https://www.cnblogs.com/Evelia/p/3678239.html
Copyright © 2011-2022 走看看