电脑爱好者,提供IT资讯信息及各类编程知识文章介绍,欢迎大家来本站学习电脑知识。 最近更新 | 联系我们 RSS订阅本站最新文章
电脑爱好者
站内搜索: 
当前位置:首页>> 编程算法>>asp.net中DES加密及解密的算法[常用密钥算法]的应用例子:

asp.net中DES加密及解密的算法[常用密钥算法]的应用例子

来源:网络 | 2013-4-20 | (有1532人读过)

DES加密及解密的算法[常用密钥算法] 

简单的使用: 
//--导入所需要的包 
using System.IO; 
using System.Text; 
using System.Security.Cryptography; 
public static string Key = "DKMAB5DE";//加密密钥必须为8位 
//加密算法 
public static string MD5Encrypt(string pToEncrypt) 
       { 
           DESCryptoServiceProvider des = new DESCryptoServiceProvider(); 
           byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt); 
           des.Key = ASCIIEncoding.ASCII.GetBytes(Key); 
           des.IV = ASCIIEncoding.ASCII.GetBytes(Key); 
           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(); 

       } 


//解密算法 
public static string MD5Decrypt(string pToDecrypt) 
       { 
           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 * 2, 2), 16)); 
               inputByteArray[x] = (byte)i; 
           } 
           des.Key = ASCIIEncoding.ASCII.GetBytes(Key); 
           des.IV = ASCIIEncoding.ASCII.GetBytes(Key); 
           MemoryStream ms = new MemoryStream(); 
           CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write); 
           cs.Write(inputByteArray, 0, inputByteArray.Length); 
           cs.FlushFinalBlock(); 
           StringBuilder ret = new StringBuilder(); 
           return System.Text.Encoding.ASCII.GetString(ms.ToArray()); 

       } 
编程算法热门文章排行
网站赞助商
购买此位置

 

关于我们 | 网站地图 | 文档一览 | 友情链接| 联系我们

Copyright © 2003-2024 电脑爱好者 版权所有 备案号:鲁ICP备09059398号