.NET学习爱好者

热爱编程事业

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

.NET里对字符串的加密与解密

最近一直在找加密与解密的算法,弄了MD5但是发现解密是不行的,今天网友发了一些代码给我,加以整合,具体代码如下:using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Security.Cryptography;
using System.Text;
using System.IO;

namespace WebApplication3
{
 /// <summary>
 /// MyCord 的摘要说明。
 /// </summary>
 public class MyCord : System.Web.UI.Page
 {
  protected System.Web.UI.WebControls.TextBox name;
  protected System.Web.UI.WebControls.Button Button2;
  protected System.Web.UI.WebControls.Label Label1;
  protected System.Web.UI.WebControls.Label Label2;
  protected System.Web.UI.WebControls.TextBox Key;
  protected System.Web.UI.WebControls.Button Button1;
 
  private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此处放置用户代码以初始化页面
  }
  //加密方法 
  public string Encrypt1(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());
  }   
  #region Web 窗体设计器生成的代码
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {   
   this.Button1.Click += new System.EventHandler(this.Button1_Click);
   this.Button2.Click += new System.EventHandler(this.Button2_Click);
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion

  private void Button1_Click(object sender, System.EventArgs e)
  {
  string a=Encrypt1(name.Text.Trim(),Key.Text.Trim());
   Response.Write(a);
  }

  private void Button2_Click(object sender, System.EventArgs e)
  {
   string a=Decrypt(name.Text.Trim(),Key.Text.Trim());
   Response.Write(a);
  }
 }
}

posted on 2007-03-15 15:19  风景  阅读(346)  评论(0)    收藏  举报