一、在网站项目中添加公共类endecode.cs
代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security.Cryptography;
using System.Text;
/// <summary>
/// endecode 的摘要说明
/// </summary>
public class endecode
{
public endecode()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public static string encodedecodestr(string code,bool flag)
{
string restr = string.Empty;
string[] keymin = {"math","computer","physical","Chinese" };//数组来字典存储
string[] keypwd = {"3602","8431","4179","3806" };
string oldkey = string.Empty;
string newkey = string.Empty;
for (int i = 0; i < keymin.Length;i++ )
{
if (flag == true)
{
oldkey = keymin[i];
newkey = keypwd[i];
}
else
{
oldkey = keypwd[i];
newkey = keymin[i];
}
code = code.Replace(oldkey,newkey);
}
restr = code;
return restr;
}
}
二、创建pwddic窗体
设计前端界面:
运行结果:
加密显示:
解密显示:
后台pwddic.aspx.cs代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Security.Cryptography;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
TextBox2.Text =reversetstr(endecode.encodedecodestr(TextBox1.Text.Trim(),true));
TextBox1.Text=String.Empty;
}
protected void Button2_Click(object sender, EventArgs e)
{
TextBox1.Text = endecode.encodedecodestr(reversetstr(TextBox2.Text.Trim()),false);
TextBox2.Text = String.Empty;
}
public string reversetstr(string codestr)
{
string restr = "";
char[] arraychar = codestr.ToCharArray();//把一个字符串转化成一个字符数组
Array.Reverse(arraychar);//把字符数组倒置
restr = new string(arraychar);//把一个字符数组转化为字符串0
return restr;
}<span style="display: none; width: 0px; height: 0px;" id="transmark"></span>
}
这里加密使用数组倒置,加密的时候明文的TextBox1就隐藏,解密的时候TextBox1就显示。