架构师_程序员_码农网

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

搜索
查看: 18293|回复: 3

[资料] c#程序密码用des加密源代码

[复制链接]
发表于 2015-6-8 09:31:47 | 显示全部楼层 |阅读模式
DES全称为Data Encryption Standard,即数据加密标准,是一种使用密钥加密的块算法,1976年被美国联邦政府的国家标准局确定为联邦资料处理标准(FIPS),随后在国际上广泛流传开来。
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Security.Cryptography;
  6. using System.Text;

  7. namespace TestWeb
  8. {
  9.     public class DESEncrypt
  10.     {
  11.         #region ========加密========

  12.         /// <summary>
  13.         /// 加密
  14.         /// </summary>
  15.         /// <param name="Text"></param>
  16.         /// <returns></returns>
  17.         public static string Encrypt(string Text)
  18.         {
  19.             return Encrypt(Text, "Test");
  20.         }
  21.         /// <summary>
  22.         /// 加密数据
  23.         /// </summary>
  24.         /// <param name="Text"></param>
  25.         /// <param name="sKey"></param>
  26.         /// <returns></returns>
  27.         public static string Encrypt(string Text, string sKey)
  28.         {
  29.             DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  30.             byte[] inputByteArray;
  31.             inputByteArray = Encoding.Default.GetBytes(Text);
  32.             des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
  33.             des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
  34.             System.IO.MemoryStream ms = new System.IO.MemoryStream();
  35.             CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
  36.             cs.Write(inputByteArray, 0, inputByteArray.Length);
  37.             cs.FlushFinalBlock();
  38.             StringBuilder ret = new StringBuilder();
  39.             foreach (byte b in ms.ToArray())
  40.             {
  41.                 ret.AppendFormat("{0:X2}", b);
  42.             }
  43.             return ret.ToString();
  44.         }

  45.         #endregion
  46.         #region ========解密========

  47.         /// <summary>
  48.         /// 解密
  49.         /// </summary>
  50.         /// <param name="Text"></param>
  51.         /// <returns></returns>
  52.         public static string Decrypt(string Text)
  53.         {
  54.             return Decrypt(Text, "Test");
  55.         }
  56.         /// <summary>
  57.         /// 解密数据
  58.         /// </summary>
  59.         /// <param name="Text"></param>
  60.         /// <param name="sKey"></param>
  61.         /// <returns></returns>
  62.         public static string Decrypt(string Text, string sKey)
  63.         {
  64.             DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  65.             int len;
  66.             len = Text.Length / 2;
  67.             byte[] inputByteArray = new byte[len];
  68.             int x, i;
  69.             for (x = 0; x < len; x++)
  70.             {
  71.                 i = Convert.ToInt32(Text.Substring(x * 2, 2), 16);
  72.                 inputByteArray[x] = (byte)i;
  73.             }
  74.             des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
  75.             des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
  76.             System.IO.MemoryStream ms = new System.IO.MemoryStream();
  77.             CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
  78.             cs.Write(inputByteArray, 0, inputByteArray.Length);
  79.             cs.FlushFinalBlock();
  80.             return Encoding.Default.GetString(ms.ToArray());
  81.         }

  82.         #endregion
  83.     }
  84. }
复制代码






上一篇:asp.net获取当前页面的url地址
下一篇:ASP.NET无法识别的配置节&quot;connectionStrings&quot;的解决方法
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
发表于 2017-8-1 06:48:32 | 显示全部楼层
谢谢楼主分享
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
发表于 2017-8-4 15:59:44 | 显示全部楼层
thanks for sharing :)
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
发表于 2017-8-16 06:44:58 | 显示全部楼层
thanks for sharing :)
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

免责声明:
码农网所发布的一切软件、编程资料或者文章仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。如有侵权请邮件与我们联系处理。

Mail To:help@itsvse.com

QQ|手机版|小黑屋|架构师 ( 鲁ICP备14021824号-2 )|网站地图

GMT+8, 2025-7-2 12:20

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表