title: Encode/Decode password to xml
body: <p>I'm working on an App that requires you to login, doesn't need something complicated, so I've made an XML file called users.xml and saved user/pass in nodes, now when encoding them to base64 it works fine, I can see it encoded in the xml file, but when I use the decrypt function, it crashes with the error "Invalid length for a Base-64 char array."</p>
  
  <p>Here is my adduser code:</p>
  
  <pre><code>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);
          }
      }
  }
  </code></pre>
  
  <p>Here is my login code:</p>
  
  <pre><code>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() &amp;&amp; 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)
          {
  
  
          }
  
      }
  }
  </code></pre>
  
  <p>can someone please guide me?</p>