加密和解密算法 Asp.net

1.aspx页面

 1     <div>
 2         <table>
 3             <tr>
 4                 <td>
 5                     <asp:Label ID="Label1" runat="server" Text="字符串" Font-Size="10pt"></asp:Label>
 6                 </td>
 7                 <td>
 8                     <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
 9                 </td>
10             </tr>
11             <tr>
12                 <td>
13                     <asp:Label ID="Label2" runat="server" Text="加密解密" Font-Size="10pt"></asp:Label>
14                 </td>
15                 <td>
16                     <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
17                 </td>
18             </tr>
19             <tr>
20                 <td colspan="2" align="center">
21                     <asp:Button ID="Button1" runat="server" Text="加密" onclick="Button1_Click" />&nbsp;&nbsp;&nbsp;&nbsp;
22                     <asp:Button ID="Button2" runat="server" Text="解密" onclick="Button2_Click" />
23                 </td>
24             </tr>
25         </table>
26     </div>          

2.cs页面

 1   protected void Button1_Click(object sender, EventArgs e)
 2     {
 3         TextBox2.Text = Encrypt(TextBox1.Text);
 4     }
 5     protected void Button2_Click(object sender, EventArgs e)
 6     {
 7         TextBox2.Text = Decryptor(TextBox2.Text);
 8     }
 9 
10     private string Encrypt(string s)
11     {
12         Encoding ascii = Encoding.ASCII;
13         string EncryptString;
14         EncryptString = "";
15         for (int i = 0; i < s.Length; i++)
16         {
17             int j;
18             byte[] b = new byte[1];
19             j = Convert.ToInt32(ascii.GetBytes(s[i].ToString())[0]);
20             j = j + 5;
21             b[0] = Convert.ToByte(j);
22             EncryptString = EncryptString + ascii.GetString(b);
23         }
24         return EncryptString;
25     }
26     private string Decryptor(string s)
27     {
28         Encoding ascii = Encoding.ASCII;
29         string DecryptorString;
30         DecryptorString = "";
31         for (int i = 0; i < s.Length; i++)
32         {
33             int j;
34             byte[] b = new byte[1];
35             j = Convert.ToInt32(ascii.GetBytes(s[i].ToString())[0]);
36             j = j - 5;
37             b[0] = Convert.ToByte(j);
38             DecryptorString = DecryptorString + ascii.GetString(b);
39         }
40         return DecryptorString;  41     } 


posted @ 2013-05-18 15:10  yellowshorts  阅读(204)  评论(0编辑  收藏  举报