XML
<?xml version="1.0" standalone="yes"?>
<Users>
<User>
<Username>Mudassar</Username>
<Password>Ao5ZnFYo344iWqv/Jr9euw==</Password>
</User>
<User>
<Username>Ravi</Username>
<Password>4rAIjA6LNuLd0kV7rYjTKQ==</Password>
</User>
<User>
<Username>Joseph</Username>
<Password>yCJzArS8YHJWprAmiBIJ9Q==</Password>
</User>
<User>
<Username>Kaldon</Username>
<Password>BarzPZWhqD7TeO7TSYwHbg==</Password>
</User>
</Users>
HTML
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
Username:
</td>
<td>
<asp:TextBox ID="txtUsername" runat="server" Text="" />
</td>
</tr>
<tr>
<td>
Password:
</td>
<td>
<asp:TextBox ID="txtPassword" runat="server" TextMode="Password" />
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="btnSubmit" OnClick="Submit" Text="Submit" runat="server" />
</td>
</tr>
</table>
<hr />
<asp:GridView ID="gvUsers" runat="server" AutoGenerateColumns="false" HeaderStyle-BackColor="#3AC0F2"
HeaderStyle-ForeColor="White" RowStyle-BackColor="#A1DCF2" OnRowDataBound="OnRowDataBound">
<Columns>
<asp:BoundField DataField="Username" HeaderText="Username" />
<asp:BoundField DataField="Password" HeaderText="Encrypted Password" />
<asp:BoundField DataField="Password" HeaderText="Decrypted Password" />
</Columns>
</asp:GridView>
Code
using System.IO;
using System.Text;
using System.Data;
using System.Security.Cryptography;
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
using (DataSet ds = new DataSet())
{
ds.ReadXml(Server.MapPath("~/XML/Users.xml"));
gvUsers.DataSource = ds;
gvUsers.DataBind();
}
}
}
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[2].Text = Decrypt(e.Row.Cells[2].Text);
}
}
protected void Submit(object sender, EventArgs e)
{
using (DataSet ds = new DataSet())
{
ds.ReadXml(Server.MapPath("~/XML/Users.xml"));
ds.Tables[0].Rows.Add(txtUsername.Text.Trim(), Encrypt(txtPassword.Text.Trim()));
ds.WriteXml(Server.MapPath("~/XML/Users.xml"));
}
Response.Redirect(Request.Url.AbsoluteUri);
}
private string Encrypt(string clearText)
{
string EncryptionKey = "MAKV2SPBNI99212";
byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);
using (Aes encryptor = Aes.Create())
{
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
encryptor.Key = pdb.GetBytes(32);
encryptor.IV = pdb.GetBytes(16);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(clearBytes, 0, clearBytes.Length);
cs.Close();
}
clearText = Convert.ToBase64String(ms.ToArray());
}
}
return clearText;
}
private string Decrypt(string cipherText)
{
string EncryptionKey = "MAKV2SPBNI99212";
byte[] cipherBytes = Convert.FromBase64String(cipherText);
using (Aes encryptor = Aes.Create())
{
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
encryptor.Key = pdb.GetBytes(32);
encryptor.IV = pdb.GetBytes(16);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(cipherBytes, 0, cipherBytes.Length);
cs.Close();
}
cipherText = Encoding.Unicode.GetString(ms.ToArray());
}
}
return cipherText;
}
Username | Encrypted Password | Decrypted Password |
Mudassar |
Ao5ZnFYo344iWqv/Jr9euw== |
1234 |
Ravi |
4rAIjA6LNuLd0kV7rYjTKQ== |
pass0rd |
Joseph |
yCJzArS8YHJWprAmiBIJ9Q== |
ping211 |
Kaldon |
BarzPZWhqD7TeO7TSYwHbg== |
wadali |