网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 手机学院 | 邮件系统 | 网络安全 | 认证考试
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > ASP.NET技巧
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
本月文章推荐
.关于URL传递日文字符乱码问题的解.
.ASP.NET2.0数据库入门之常见错误.
.一些关于SQL2005+ASP.NET2.0的问.
.教你制做Web实时进度条.
.改写即时消息的发送,包含同时给.
.如何在IE右键菜单中添加菜单项以.
.实现FCKeditor 多用户分文件夹上.
.一个读取扩展名为xml的资源文件的.
.ASP.NET Whidbey中实现Provider.
.ASP.NET页面的处理过程完全版.
.ASP.NET中为DataGrid添加合计字段.
.ASP.NET里常用的JS.
.Asp.Net基于forms的验证机制.
.ASP.Net2.0中自定义控件在page中.
.使用ASP.NET2.0的ReportViewer查.
.ASP.NET 2.0运行时简要分析.
..net回收机制的使用.
..NET框架下Oracle到SQL Server迁.
.Asp.net中多彩下拉框的实现.
.asp.net 生成图片验证码.

使用DES对称加密代码,支持中文

发表日期:2008-12-12

view plaincopy to clipboardprint?
//名称空间   
using System;   
using System.Security.Cryptography;   
using System.IO;   
using System.Text;   
   
//方法   
//加密方法   
public string Encrypt(string pToEncrypt, string sKey)   
{   
DESCryptoServiceProvider des = new DESCryptoServiceProvider();   
//把字符串放到byte数组中   
//原来使用的UTF8编码,我改成Unicode编码了,不行   
byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);   
//byte[] inputByteArray=Encoding.Unicode.GetBytes(pToEncrypt);   
   
//建立加密对象的密钥和偏移量   
//原文使用ASCIIEncoding.ASCII方法的GetBytes方法   
//使得输入密码必须输入英文文本   
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);   
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);   
MemoryStream ms = new MemoryStream();   
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(),CryptoStreamMode.Write);   
//Write the byte array into the crypto stream   
//(It will end up in the memory stream)   
cs.Write(inputByteArray, 0, inputByteArray.Length);   
cs.FlushFinalBlock();   
//Get the data back from the memory stream, and into a string   
StringBuilder ret = new StringBuilder();   
foreach(byte b in ms.ToArray())   
{   
//Format as hex   
ret.AppendFormat("{0:X2}", b);   
}   
ret.ToString();   
return ret.ToString();   
}   
   
//解密方法   
public string Decrypt(string pToDecrypt, string sKey)   
{   
DESCryptoServiceProvider des = new DESCryptoServiceProvider();   
   
//Put the input string into the byte array   
byte[] inputByteArray = new byte[pToDecrypt.Length / 2];   
for(int x = 0; x < pToDecrypt.Length / 2; x++)   
{   
int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));   
inputByteArray[x] = (byte)i;   
}   
   
//建立加密对象的密钥和偏移量,此值重要,不能修改   
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);   
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);   
MemoryStream ms = new MemoryStream();   
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(),CryptoStreamMode.Write);   
//Flush the data through the crypto stream into the memory stream   
cs.Write(inputByteArray, 0, inputByteArray.Length);   
cs.FlushFinalBlock();   
   
//Get the decrypted data back from the memory stream   
//建立StringBuild对象,CreateDecrypt使用的是流对象,必须把解密后的文本变成流对象   
StringBuilder ret = new StringBuilder();   
   
return System.Text.Encoding.Default.GetString(ms.ToArray());   
}   
   
//-------代码完毕--------------------  

//名称空间
using System;
using System.Security.Cryptography;
using System.IO;
using System.Text;
 
//方法
//加密方法
public string Encrypt(string pToEncrypt, string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
//把字符串放到byte数组中
//原来使用的UTF8编码,我改成Unicode编码了,不行
byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
//byte[] inputByteArray=Encoding.Unicode.GetBytes(pToEncrypt);
 
//建立加密对象的密钥和偏移量
//原文使用ASCIIEncoding.ASCII方法的GetBytes方法
//使得输入密码必须输入英文文本
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(),CryptoStreamMode.Write);
//Write the byte array into the crypto stream
//(It will end up in the memory stream)
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
//Get the data back from the memory stream, and into a string
StringBuilder ret = new StringBuilder();
foreach(byte b in ms.ToArray())
{
//Format as hex
ret.AppendFormat("{0:X2}", b);
}
ret.ToString();
return ret.ToString();
}
 
//解密方法
public string Decrypt(string pToDecrypt, string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
 
//Put the input string into the byte array
byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
for(int x = 0; x < pToDecrypt.Length / 2; x++)
{
int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
inputByteArray[x] = (byte)i;
}
 
//建立加密对象的密钥和偏移量,此值重要,不能修改
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(),CryptoStreamMode.Write);
//Flush the data through the crypto stream into the memory stream
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
 
//Get the decrypted data back from the memory stream
//建立StringBuild对象,CreateDecrypt使用的是流对象,必须把解密后的文本变成流对象
StringBuilder ret = new StringBuilder();
 
return System.Text.Encoding.Default.GetString(ms.ToArray());
}
 
//-------代码完毕--------------------
注意:sKey输入密码的时候,必须使用英文字符,区分大小写,且字符数量是8个,不能多也不能少,否则出错。

本人使用Windows2000 Server .Net Framework SP3 ,VS.Net下在asp.net下使用成功,加密解密正常!

上一篇:Asp.Net基于forms的验证机制 人气:226
下一篇:关于.net中页面CSS样式丢失的解决方案 人气:218
浏览全部DES对称加密代码的内容 Dreamweaver插件下载 网页广告代码 2009年新年快乐