Length of the Data to Decrypt is Invalid

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
So, I have an xml file that I BELIEVE Ive written properly. The problem is when I try to read it, it wont work. I get an exception: "Length of the data to decrypt is invalid.". Heres my code below:

<pre class="prettyprint using DBProcWriter.Data.Events;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Security.Cryptography.Xml;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

namespace Database_Proc_Writer
{
public partial class ConnectionWindow : Form
{
public event DBConnected Connected;

public ConnectionWindow()
{
InitializeComponent();
RijndaelManaged key = null;

try
{
key = new RijndaelManaged();
XmlDocument xml = new XmlDocument();
xml.Load("connections.xml");

XmlElement encryptedElement = xml.GetElementsByTagName("EncryptedData")[0] as XmlElement;
EncryptedData edElement = new EncryptedData();

edElement.LoadXml(encryptedElement);

EncryptedXml exml = new EncryptedXml();
byte[] rgbOutput = exml.DecryptData(edElement, key);
exml.ReplaceData(encryptedElement, rgbOutput);
}
catch (FileNotFoundException fnfe)
{
string error = fnfe.ToString();
}
}

private void button1_Click(object sender, EventArgs e)
{
RijndaelManaged key = null;
TripleDESCryptoServiceProvider cryptoProvider = new TripleDESCryptoServiceProvider();

try
{
string server, user, pass;
server = ServerTextbox.Text.Trim();
user = UsernameTextbox.Text.Trim();
pass = PasswordTextbox.Text.Trim();

key = new RijndaelManaged();
XmlDocument xml = new XmlDocument();

//Xml File Body
XmlDeclaration declaration = xml.CreateXmlDeclaration("1.0", "UTF-8", null);
XmlElement root = xml.CreateElement("Connection");
XmlElement userElement = xml.CreateElement("Username");
userElement.InnerText = user;
XmlElement passElement = xml.CreateElement("Password");
passElement.InnerText = pass;
XmlElement serverElement = xml.CreateElement("Server");
serverElement.InnerText = server;

root.AppendChild(userElement);
root.AppendChild(passElement);
root.AppendChild(serverElement);

xml.AppendChild(root);

//EncryptedXml
EncryptedXml eXml = new EncryptedXml();
byte[] encryptedUserElement = eXml.EncryptData(userElement, cryptoProvider, false);
byte[] encryptedPassElement = eXml.EncryptData(passElement, cryptoProvider, false);
byte[] encryptedServerElement = eXml.EncryptData(serverElement, cryptoProvider, false);

EncryptedData eUserElement = new EncryptedData();
EncryptedData ePassElement = new EncryptedData();
EncryptedData eServerElement = new EncryptedData();

eUserElement.Type = EncryptedXml.XmlEncElementUrl;
ePassElement.Type = EncryptedXml.XmlEncElementUrl;
eServerElement.Type = EncryptedXml.XmlEncElementUrl;

string encryptionMethod = null;

switch (key.KeySize)
{
case 128:
encryptionMethod = EncryptedXml.XmlEncAES128Url;
break;

case 192:
encryptionMethod = EncryptedXml.XmlEncAES192Url;
break;

case 256:
encryptionMethod = EncryptedXml.XmlEncAES256Url;
break;
}

eUserElement.EncryptionMethod = new EncryptionMethod(encryptionMethod);
eUserElement.CipherData.CipherValue = encryptedUserElement;
EncryptedXml.ReplaceElement(userElement, eUserElement, false);

ePassElement.EncryptionMethod = new EncryptionMethod(encryptionMethod);
ePassElement.CipherData.CipherValue = encryptedPassElement;
EncryptedXml.ReplaceElement(passElement, ePassElement, false);

eServerElement.EncryptionMethod = new EncryptionMethod(encryptionMethod);
eServerElement.CipherData.CipherValue = encryptedServerElement;
EncryptedXml.ReplaceElement(serverElement, eServerElement, false);

XmlTextWriter writer = new XmlTextWriter("connections.xml", Encoding.UTF8);
xml.WriteTo(writer);
writer.Close();
key.Clear();

SqlConnection connection = new SqlConnection(String.Format("Server={0};User Id={1};Password={2};", server, user, pass));
Connected(connection);
Close();
}
catch (Exception ex)
{
MessageBox.Show("Connection Error:" + ex.Message, "Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

private void UsernameTextbox_KeyUp(object sender, KeyEventArgs e)
{
if ((UsernameTextbox.Text == "") || (PasswordTextbox.Text == "") || (ServerTextbox.Text == ""))
ConnectButton.Enabled = false;
else if ((UsernameTextbox.Text != "") && (PasswordTextbox.Text != "") && (ServerTextbox.Text != ""))
ConnectButton.Enabled = true;
}

private void PasswordTextbox_KeyUp(object sender, KeyEventArgs e)
{
if ((UsernameTextbox.Text == "") || (PasswordTextbox.Text == "") || (ServerTextbox.Text == ""))
ConnectButton.Enabled = false;
else if ((UsernameTextbox.Text != "") && (PasswordTextbox.Text != "") && (ServerTextbox.Text != ""))
ConnectButton.Enabled = true;
}

private void ServerTextbox_KeyUp(object sender, KeyEventArgs e)
{
if ((UsernameTextbox.Text == "") || (PasswordTextbox.Text == "") || (ServerTextbox.Text == ""))
ConnectButton.Enabled = false;
else if ((UsernameTextbox.Text != "") && (PasswordTextbox.Text != "") && (ServerTextbox.Text != ""))
ConnectButton.Enabled = true;
}
}
}
[/code]
Any help I could get would be appreciated. Thanks!<br/>
<hr class="sig Jack Schaible Student, DMIT Northern Albert Institute of Technology Edmonton, Alberta, Canada

View the full article
 
Back
Top