将密码编码/解码为xml

时间:2015-07-13 21:49:01

标签: c# xml encryption

我正在开发一个需要您登录的应用程序,不需要复杂的东西,所以我已经创建了一个名为users.xml的XML文件,并保存了用户/传递节点,现在将它们编码为base64时工作正常,我可以看到它在xml文件中编码,但是当我使用decrypt函数时,它崩溃了错误" Base-64字符数组的长度无效。"

这是我的adduser代码:

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)
        {


        }

    }
}

有人可以指导我吗?

1 个答案:

答案 0 :(得分:0)

您将base64编码数据存储在xml中,因此在Form2(您的登录表单)中,您应编码文本(textBox7)并将其与xml中的值进行比较,或者解码 xml中的值,并将其与textBox7(纯文本格式)进行比较。

您当前的代码采用textBox7中的值对其进行解码(未编码)并与xml中的值进行比较

PS:我认为此代码仅用于学习目的......