我正在使用C#應用程序。我的XML文件中有兩個敏感數據,即用戶名和密碼。加密XML屬性值和元素?
I want to:
登錄,保存文件和加載xml時,加密和解密用戶名和密碼。任何人都可以幫助我嗎?
XML文件
<Users>
<user username="kelil2000">
<password>123</password>
<author>Home Owner</author>
<name>Kelil</name>
<mobile>0911</mobile>
</user>
<user username="usminuru">
<password>1234</password>
<author>Home Owner</author>
<name>Ismail K.</name>
<mobile>0910178976</mobile>
</user>
</Users>
登錄:
if (txtUserName.Text == "" || txtPassword.Text == "")
{
MessageBox.Show("Username or Passowrd field is empty, try again!");
ClearTextBoxes();
return;
}
int i = 0; // we use this variable to count if ther’s a user with this name
XmlDocument myXml=new XmlDocument();
myXml.Load(Application.StartupPath + "/AppUsers/Users.xml");
XmlNodeList userList = myXml.SelectNodes("Users/user");
foreach(XmlNode user in userList)
{
string userName = user.Attributes["username"].Value;
string userPassword = user["password"].InnerText;
string userAuthor = user["author"].InnerText;
if (userName == txtUserName.Text)
{
++i;
if (userPassword == txtPassword.Text)
{
Form panel;
this.Opacity = 0;
switch(userAuthor)
{
case "Home Owner":
panel = new MainWindow();
panel.Show();
break;
case "Member" :
panel = new Report();
panel.Show();
break;
}
}
else
{
MessageBox.Show("Wrong Password!");
ClearTextBoxes();
}
}
}
if (i == 0)
MessageBox.Show("No specified user with this name!");
ClearTextBoxes();
}
保存XML:
private void AddUser()
{
if (txtUserName.Text == "" || txtPassword.Text == "" || cmbAuthor.Text == "" || txtName.Text == "" || txtMobile.Text == "")
{
MessageBox.Show("Filed is empty");
return;
}
try
{
string _file = (Application.StartupPath + "/AppUsers/Users.xml");
XDocument doc;
if (!File.Exists(_file))
{
doc = new XDocument();
doc.Add(new XElement("Users"));
}
else
{
doc = XDocument.Load(_file);
}
doc.Root.Add(
new XElement("user",
new XAttribute("username", txtUserName.Text),
new XElement("password", txtPassword.Text),
new XElement("author", cmbAuthor.Text),
new XElement("name", txtName.Text),
new XElement("mobile", txtMobile.Text)
)
);
doc.Save(_file);
}
catch (Exception ex)
{
MessageBox.Show("Something Wrong!" + ex.ToString());
}
}
加載XML文件
private void loadXmlData()
{
listView1.Items.Clear();
XDocument doc = XDocument.Load(Application.StartupPath + "/AppUsers/Users.xml");
doc.Descendants("user").ToList()
.ForEach(x => listView1.Items.Add(
new ListViewItem(
new string[] {
x.Attribute("username").Value,
x.Element("password").Value,
x.Element("author").Value,
x.Element("name").Value,
x.Element("mobile").Value}))
);
}
您絕對*不想*保存用戶密碼,加密或其他方式,在服務器上的任何位置。如果有的話,你想存儲用戶密碼的[secure salted hashes](https://crackstation.net/hashing-security.htm)。如果你是「所有這些東西」的新手,我**強烈建議你不要觸摸它,直到你知道你在做什麼。密碼學很難。即使對於有經驗的人來說,滾動您自己的安全系統始終是一個非常糟糕的主意。不要這樣做。尋找現成的圖書館。 – Tomalak
千萬不要存儲密碼!當然不是純文本,也不是加密的。加密可以解密!始終存儲散列(和鹽漬)密碼。主要的區別是散列算法*不可*通過設計可逆。請參閱http://stackoverflow.com/a/401684/1336590(不僅與php相關)。 – Corak