C# 将密码编码/解码为xml
我正在开发一个需要你登录的应用程序,不需要复杂的东西,所以我创建了一个名为users.XML的XML文件,并保存了user/pass-in节点,现在当将它们编码到base64时,工作正常,我可以看到它编码在XML文件中,但是当我使用decrypt函数时,它会因错误而崩溃“Base-64字符数组的长度无效。” 这是我的adduser代码:C# 将密码编码/解码为xml,c#,xml,encryption,C#,Xml,Encryption,我正在开发一个需要你登录的应用程序,不需要复杂的东西,所以我创建了一个名为users.XML的XML文件,并保存了user/pass-in节点,现在当将它们编码到base64时,工作正常,我可以看到它编码在XML文件中,但是当我使用decrypt函数时,它会因错误而崩溃“Base-64字符数组的长度无效。” 这是我的adduser代码: using System; using System.Collections.Generic; using System.ComponentModel; usi
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
namespace k9record
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
loadComboUser();
}
public static string encode(string text)
{
byte[] mybyte = System.Text.Encoding.UTF8.GetBytes(text);
string returntext = System.Convert.ToBase64String(mybyte);
return returntext;
}
private void button4_Click(object sender, EventArgs e)
{
string PATH = "user.xml";
XmlDocument doc = new XmlDocument();
//If there is no current file, then create a new one
if (!System.IO.File.Exists(PATH))
{
//Create neccessary nodes
XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", "UTF-8", "yes");
XmlComment comment = doc.CreateComment("This is an XML Generated File - Ather");
XmlElement root = doc.CreateElement("users");
XmlElement user = doc.CreateElement("user");
XmlElement UserID = doc.CreateElement("UserID");
XmlElement Password = doc.CreateElement("Password");
//Add the values for each nodes
UserID.InnerText = textBox2.Text;
Password.InnerText = textBox1.Text;
//Construct the document
doc.AppendChild(declaration);
doc.AppendChild(comment);
doc.AppendChild(root);
root.AppendChild(user);
user.AppendChild(UserID);
user.AppendChild(Password);
doc.Save(PATH);
}
else //If there is already a file
{
//Load the XML File
doc.Load(PATH);
//Get the root element
XmlElement root = doc.DocumentElement;
XmlElement user = doc.CreateElement("user");
XmlElement UserID = doc.CreateElement("UserID");
XmlElement Password = doc.CreateElement("Password");
//Add the values for each nodes
UserID.InnerText = textBox2.Text;
// Password.InnerText = textBox1.Text;
Password.InnerText = encode(textBox1.Text.ToString());
//Construct the Person element
user.AppendChild(UserID);
user.AppendChild(Password);
//Add the New person element to the end of the root element
root.AppendChild(user);
//Save the document
doc.Save(PATH);
}
//Show confirmation message
MessageBox.Show("User Registered");
//Reset text fields for new input
textBox2.Text = String.Empty;
textBox1.Text = String.Empty;
}
private void button1_Click(object sender, EventArgs e)
{
XmlDocument xdoc1 = new XmlDocument();
xdoc1.Load("user.xml");
foreach (XmlNode node in xdoc1.SelectNodes("/users/user"))
{
if (node.SelectSingleNode("UserID").InnerText == comboBox1.SelectedItem.ToString())
{
node.ParentNode.RemoveChild(node);
}
}
xdoc1.Save("user.xml");
MessageBox.Show("User Deleted","Deleted");
comboBox1.Items.Clear();
loadComboUser();
}
private void loadComboUser()
{
comboBox1.Items.Clear();
XmlDocument doc = new XmlDocument();
doc.Load("user.xml");
XmlNodeList nodeList = doc.SelectNodes("/users/user");
foreach (XmlNode node in nodeList)
if (!comboBox1.Items.Contains(node.SelectSingleNode("UserID").InnerText))
comboBox1.Items.Add(node.SelectSingleNode("UserID").InnerText);
}
}
}
这是我的登录码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
namespace k9record
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
loadComboUser();
}
public static string decode(string text)
{
byte[] mybyte = System.Convert.FromBase64String(text);
string returntext = System.Text.ASCIIEncoding.ASCII.GetString(mybyte);
return returntext;
}
internal void Login()
{
XmlDocument doc = new XmlDocument();
doc.Load("user.xml");
foreach (XmlNode node in doc.SelectNodes("/users/user"))
{
String Username = node.SelectSingleNode("UserID").InnerText;
String Password = node.SelectSingleNode("Password").InnerText;
if (Username == comboBox1.SelectedItem.ToString() && Password == decode(textBox7.Text))
{
DialogResult = DialogResult.OK;
}
}
}
private void loadComboUser()
{
comboBox1.Items.Clear();
XmlDocument doc = new XmlDocument();
doc.Load("user.xml");
XmlNodeList nodeList = doc.SelectNodes("/users/user");
foreach (XmlNode node in nodeList)
if (!comboBox1.Items.Contains(node.SelectSingleNode("UserID").InnerText))
comboBox1.Items.Add(node.SelectSingleNode("UserID").InnerText);
}
private void button2_Click_1(object sender, EventArgs e)
{
Login();
}
private void button1_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void button4_Click_1(object sender, EventArgs e)
{
}
}
}
有人能指导我吗?您将base64编码的数据存储在xml中,因此在
表单2
(您的登录表单)中,您应该对文本(textBox7)进行编码并将其与xml中的值进行比较,或者解码xml中的值并将其与textBox7(纯文本)进行比较
您当前的代码采用textBox7中的值对其进行解码(未编码)并与xml中的值进行比较
PS:我假设此代码仅用于学习目的…我还尝试了'string returntext=System.Text.Encoding.UTF8.GetString(mybyte)你为什么在一个地方使用ASCII,在另一个地方使用UTF8?说真的,永远不要以允许密码被解码的形式存储密码。将它们存储为salt散列,并对输入的密码应用相同的散列来匹配存储的值。不,如果是;不,但是。@Ron这是一个错误,我在这两种情况下都使用utf,我得到了相同的结果error@DavidArno:有没有链接到一个例子?是的,这不是一个商业软件,只是为了一个照顾生病的狗的慈善机构,你能解释一下我该如何再次编码吗?不,我不能,因为我不明白你在问什么。我的答案中有什么不清楚的地方?“你应该编码文本(textBox7)并将其与xml中的值进行比较“@atherakber与Adduser类似,获取密码,应用相同的算法,并将结果与存储在xml中的结果进行比较。@atherakber确定,现在您可以阅读此内容了。”