ASP.NET RSA 加密

相信RSA加密大家都不陌生,MD5加密虽然也是一种加密手段,但是MD5加密是单向加密,不可以解密,在某些场合下显得非常不方便,所以今天特地MSDN上找了个例子来认识RSA加密

1.首先是前端代码

   1:     <table style="font-family: Calibri; width: 100%">
   2:              <tr>
   3:                  <td>
   4:                      <asp:Label ID="lbwords" runat="server" Text="Enter Some Words:"></asp:Label>
   5:                  </td>
   6:                  <td colspan="3">
   7:                      <asp:TextBox ID="tbData" runat="server" Width="300px"></asp:TextBox>
   8:                  </td>
   9:              </tr>
  10:              <tr>
  11:                  <td>
  12:                      <asp:Label ID="lbEncryptData" runat="server" Text="Encrypted Data:"></asp:Label>
  13:                  </td>
  14:                  <td>
  15:                      <asp:Button ID="btnEncrypt" runat="server" Text="Encrypt it" OnClick="Encrypt_Click"
  16:                          Height="30px" Width="90px" />
  17:                      <br />
  18:                      <asp:TextBox ID="tbEncryptData" runat="server" TextMode="MultiLine" Height="200px"
  19:                          Width="400px" ReadOnly="true"></asp:TextBox>
  20:                      <br />
  21:                  </td>
  22:                  <td>
  23:                      <asp:Label ID="lbDecryptedData" runat="server" Text="Decrypted Data:"></asp:Label>
  24:                  </td>
  25:                  <td>
  26:                      <asp:Button ID="btnDecrypt" runat="server" Text="Decrypt it" OnClick="Decrypt_Click"
  27:                          Height="30px" Width="90px" />
  28:                      <br />
  29:                      <asp:TextBox ID="tbDecryptData" runat="server" TextMode="MultiLine" Height="200px"
  30:                          Width="400px" ReadOnly="true"></asp:TextBox>
  31:                      <br />
  32:                  </td>
  33:              </tr>
  34:          </table>

2.后台代码

说明:因为是初次接触RSA加密再加上C#基础不是很好,看起来还是非常费劲,所以加了许多注释。可以单独把加密和解写到一个类里面,到时候方便调用,但是如果是想用来加密密码或是用户名之类的,那应该在设计时留足足够的长度。

   1:   protected void Page_Load(object sender, EventArgs e)
   2:          {
   3:              AddKeyUpEventOnTextControl();
   4:              //1.PreRender 在加载 System.Web.UI.Control 对象之后、呈现之前发生。
   5:              //2.将 btn.Decrypt控件的PreRender事件绑定到名为btnDecrypt_PreRender 的方法(动态绑定)
   6:              this.btnDecrypt.PreRender += new EventHandler(btnDecrypt_PreRender);
   7:              this.btnEncrypt.PreRender += new EventHandler(btnEncrypt_PreRender);
   8:          }
   9:   
  10:          private void RSAEncryption()
  11:          {
  12:   
  13:              //3.CspParameters包含传递给执行加密计算的加密服务提供程序 (CSP) 的参数。无法继承此类。
  14:              CspParameters param = new CspParameters();
  15:              //4. KeyContainerName 表示 System.Security.Cryptography.CspParameters 的密钥容器名称。
  16:              param.KeyContainerName = "MyKeyContainer";
  17:              //5.  使用加密服务提供程序 (CSP) 提供的 System.Security.Cryptography.RSA 算法的实现执行不对称加密和解密
  18:              using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(param))
  19:              {
  20:                  string plaintext = this.tbData.Text;
  21:                  byte[] plaindata = System.Text.Encoding.Default.GetBytes(plaintext);
  22:              //6. 如果为 true,则使用 OAEP 填充(仅在运行 Microsoft Windows XP 或更高版本的计算机上可用)执行直接的
  23:              //System.Security.Cryptography.RSA 加密;否则,如果为 false,则使用 PKCS#1 1.5 版填充。
  24:                  byte[] encryptdata = rsa.Encrypt(plaindata, false);
  25:              // 7.将 8 位无符号整数的数组转换为其用 Base64 数字编码的等效字符串表示形式
  26:                  string encryptstring = Convert.ToBase64String(encryptdata);//加密后长度为128
  27:                  this.tbEncryptData.Text = encryptstring;
  28:              }
  29:          }
  30:   
  31:          private void RSADecryption()
  32:          {
  33:              CspParameters param = new CspParameters();
  34:              param.KeyContainerName = "MyKeyContainer";
  35:              using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(param))
  36:              {
  37:                  //8.将指定的字符串(它将二进制数据编码为 Base64 数字)转换为等效的 8 位无符号整数数组
  38:                  byte[] encryptdata = Convert.FromBase64String(this.tbEncryptData.Text);
  39:                  //9.使用 System.Security.Cryptography.RSA 算法对数据进行解密。
  40:                  byte[] decryptdata = rsa.Decrypt(encryptdata, false);
  41:                  string plaindata = System.Text.Encoding.Default.GetString(decryptdata);
  42:                  this.tbDecryptData.Text = plaindata;
  43:              }
  44:          }
  45:   
  46:          protected void Encrypt_Click(object sender, EventArgs e)
  47:          {
  48:              RSAEncryption();
  49:          }
  50:   
  51:          protected void Decrypt_Click(object sender, EventArgs e)
  52:          {
  53:              RSADecryption();
  54:          }
  55:   
  56:          void btnDecrypt_PreRender(object sender, EventArgs e)
  57:          {
  58:              EnableDecryptButton();
  59:          }
  60:   
  61:          void btnEncrypt_PreRender(object sender, EventArgs e)
  62:          {
  63:              EnableEncryptButton();
  64:          }
  65:   
  66:          private void AddKeyUpEventOnTextControl()
  67:          {
  68:              string script = string.Format(@"function PressFn(sender) {{
  69:                                              document.getElementById('{0}').disabled = sender.value == '' ? true : false;
  70:                                              }}", btnEncrypt.ClientID);
  71:              tbData.Attributes["onkeyup"] = "PressFn(this)";
  72:              Page.ClientScript.RegisterStartupScript(this.GetType(), "DataKeyUp", script, true);
  73:          }
  74:   
  75:          /// <summary>
  76:          /// 动态设置加密按钮是否可用
  77:          /// </summary>
  78:          private void EnableDecryptButton()
  79:          {
  80:              btnDecrypt.Enabled = this.tbEncryptData.Text != string.Empty ? true : false;
  81:             //大概以前都不会想到用这种想法,如果是我我会用if...else...
  82:          }
  83:   
  84:          /// <summary>
  85:          /// 动态设置解密按钮是否可用
  86:          /// </summary>
  87:          private void EnableEncryptButton()
  88:          {
  89:              btnEncrypt.Enabled = this.tbData.Text != string.Empty ? true : false;
  90:          }
posted @ 2011-11-23 22:00  January  阅读(6897)  评论(3编辑  收藏  举报