惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The GitHub Blog
The GitHub Blog
Martin Fowler
Martin Fowler
Vercel News
Vercel News
U
Unit 42
Engineering at Meta
Engineering at Meta
aimingoo的专栏
aimingoo的专栏
MyScale Blog
MyScale Blog
Y
Y Combinator Blog
阮一峰的网络日志
阮一峰的网络日志
爱范儿
爱范儿
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
B
Blog RSS Feed
N
Netflix TechBlog - Medium
GbyAI
GbyAI
F
Fortinet All Blogs
MongoDB | Blog
MongoDB | Blog
大猫的无限游戏
大猫的无限游戏
C
Check Point Blog
M
MIT News - Artificial intelligence
D
Docker
IT之家
IT之家
Stack Overflow Blog
Stack Overflow Blog

博客园 - hekeneng

ajax学习例子 SQLServer 存储过程简介与使用方法 二进制数据(Image类型)从一个表存入另一个表 像百度搜索结果一样得到一个关键字左右的若干字符 Javascript操作select控件大全 C#后台调用前台javascript的五种方法 SqlHelper DbHelperSQL 数据访问基础类(基于OleDb)DbHelper js打印局部页面 js刷新页面代码大全 50个网页常用小代码 JS全选反选脚本结合GridView javascript中不可或缺的脚本代码 一些有用的Javscript小技巧 JavaScript 获取runat server控件的控制权(点击Button触发Textbox和Dropdownlist联动为例) JavaScript实用技巧集锦 JavaScript网页特效5则 动态绘制柱状图饼状图
DES加密/解密类。
hekeneng · 2010-03-03 · via 博客园 - hekeneng

using System;
using System.Security.Cryptography; 
using System.Text;
namespace ROYcms.DB
{
 /// <summary>
 /// DES加密/解密类。
 /// </summary>
 public class DESEncrypt
 {
  public DESEncrypt()
  {   
  }

  #region ========加密========
 
        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="Text"></param>
        /// <returns></returns>
  public static string Encrypt(string Text)
  {
   return Encrypt(Text,"ROYcms");
  }
  /// <summary>
  /// 加密数据
  /// </summary>
  /// <param name="Text"></param>
  /// <param name="sKey"></param>
  /// <returns></returns>
  public static string Encrypt(string Text,string sKey)
  {
   DESCryptoServiceProvider des = new DESCryptoServiceProvider();
   byte[] inputByteArray;
   inputByteArray=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(sKey, "md5").Substring(0, 8));
   System.IO.MemoryStream ms=new System.IO.MemoryStream();
   CryptoStream cs=new CryptoStream(ms,des.CreateEncryptor(),CryptoStreamMode.Write);
   cs.Write(inputByteArray,0,inputByteArray.Length);
   cs.FlushFinalBlock();
   StringBuilder ret=new StringBuilder();
   foreach( byte b in ms.ToArray())
   {
    ret.AppendFormat("{0:X2}",b);
   }
   return ret.ToString();
  }

  #endregion
  
  #region ========解密========
  
 
        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="Text"></param>
        /// <returns></returns>
  public static string Decrypt(string Text)
  {
            return Decrypt(Text, "ROYcms");
  }
  /// <summary>
  /// 解密数据
  /// </summary>
  /// <param name="Text"></param>
  /// <param name="sKey"></param>
  /// <returns></returns>
  public static string Decrypt(string Text,string sKey)
  {
   DESCryptoServiceProvider des = new DESCryptoServiceProvider();
   int len;
   len=Text.Length/2;
   byte[] inputByteArray = new byte[len];
   int x,i;
   for(x=0;x<len;x++)
   {
    i = Convert.ToInt32(Text.Substring(x * 2, 2), 16);
    inputByteArray[x]=(byte)i;
   }
   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(sKey, "md5").Substring(0, 8));
   System.IO.MemoryStream ms=new System.IO.MemoryStream();
   CryptoStream cs=new CryptoStream(ms,des.CreateDecryptor(),CryptoStreamMode.Write);
   cs.Write(inputByteArray,0,inputByteArray.Length);
   cs.FlushFinalBlock();
   return Encoding.Default.GetString(ms.ToArray());
  }
 
  #endregion


 }
}