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

推荐订阅源

月光博客
月光博客
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
阮一峰的网络日志
阮一峰的网络日志
罗磊的独立博客
T
Tailwind CSS Blog
博客园_首页
博客园 - 司徒正美
Google DeepMind News
Google DeepMind News
Hugging Face - Blog
Hugging Face - Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
V
V2EX
J
Java Code Geeks
量子位
D
DataBreaches.Net
MongoDB | Blog
MongoDB | Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Microsoft Azure Blog
Microsoft Azure Blog
P
Proofpoint News Feed
C
Check Point Blog
V
Visual Studio Blog
H
Help Net Security
Recent Announcements
Recent Announcements
Engineering at Meta
Engineering at Meta

博客园 - joinsky

asp.net下载代码 - joinsky - 博客园 必须会的十种语言 df 有关程序员的传说 网站标准 ASp.net中的定位问题. - joinsky - 博客园 文字的竖排与横排 - joinsky - 博客园 两个常用的存储过程(转) iframe自适应网页内容的高度 - joinsky - 博客园 IErrorInfo.GetDescription 因 E_FAIL(0x80004005) 而失败 javascript中object转换为string 数据库中游标的使用.....用于循环 网页技巧之如何隐藏状态栏里出现的LINK信息 随页面滚动的层(转) 页面过长自动进行分页(转) 自定义组件的执行周期(转) 调用DLL中的方法(转) vs2003问题集锦 HTTP/1.1 500 Internal Server Error 解决方法
asp.net中的DES加密
joinsky · 2006-08-22 · via 博客园 - joinsky

1using System.Security.Cryptography;
2using System.Text;
3using System.IO;

#region   加密 DES
        
public string Encrypt(string pToEncrypt, string sKey)
        
{
            DESCryptoServiceProvider des 
= new DESCryptoServiceProvider();
            
//把字符串放到byte数组中
            
//原来使用的UTF8编码,我改成Unicode编码了,不行
            byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);

            
//建立加密对象的密钥和偏移量
            
//使得输入密码必须输入英文文本
            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);

            cs.Write(inputByteArray, 
0, inputByteArray.Length);
            cs.FlushFinalBlock();
            StringBuilder ret 
= new StringBuilder();
            
foreach(byte b in ms.ToArray())
            
{
                ret.AppendFormat(
"{0:X2}", b);
            }

            ret.ToString();
            
return ret.ToString();
        }

        
#endregion

    
        
#region 解密方法
        
//pToDecrypt为需要解密字符串,sKey为密钥
        public string Decrypt(string pToDecrypt, string sKey)
        
{
            DESCryptoServiceProvider des 
= new DESCryptoServiceProvider();
            
byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
            
for(int x = 0; x < pToDecrypt.Length / 2; x++)
            
{
                
int i = (Convert.ToInt32(pToDecrypt.Substring(x * 22), 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);
            cs.Write(inputByteArray, 
0, inputByteArray.Length);
            cs.FlushFinalBlock();
            
//建立StringBuild对象,CreateDecrypt使用的是流对象,必须把解密后的文本变成流对象
            StringBuilder ret = new StringBuilder();
            
return System.Text.Encoding.Default.GetString(ms.ToArray());

        }

            
#endregion