Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I stuck in the problem may be some can help.

i write this code

XDocument doc = XDocument.Load(Filepath of any xml file");
string readxml = doc.ToString();
string swapnil = Encrypt(readxml);
XDocument doc1 = XDocument.Parse(swapnil);
string avi = Decrypt(swapnil);

using xdocument i load a xml file
then in readxml i am reading xdocument as string because i have methods for encrytion and decrytion that accept string.
in swapnil string i have encrypted the data. but again i want to convert the string into xdocument type. but when i am converting getting this error.

Data at the root level is invalid. Line 1, position 1.

below is methods for encrytion and decryption


private readonly Rfc2898DeriveBytes _DeriveBytes;
private readonly byte[] _InitVectorBytes;
private readonly byte[] _KeyBytes;
private const string InitVector = "T=A4rAzu94ez-dra";
private const int PasswordIterations = 1000; //2;
private const string SaltValue = "d=?ustAF=UstenAr3B@pRu8=ner5sW&h59_Xe9P2za-eFr2fa&ePHE@ras!a+uc@";

public string Decrypt(string encryptedText)
{
byte[] encryptedTextBytes = Convert.FromBase64String(encryptedText);
string plainText;

RijndaelManaged rijndaelManaged = new RijndaelManaged { Mode = CipherMode.CBC };

try
{
using (ICryptoTransform decryptor = rijndaelManaged.CreateDecryptor(_KeyBytes, _InitVectorBytes))
{
using (MemoryStream memoryStream = new MemoryStream(encryptedTextBytes))
{
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
{
//TODO: Need to look into this more. Assuming encrypted text is longer than plain but there is probably a better way
byte[] plainTextBytes = encryptedTextBytes; /*new byte[encryptedTextBytes.Length];*/

int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
plainText = Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
}
}
}
}
catch (CryptographicException exception)
{
plainText = string.Empty; // Assume the error is caused by an invalid password
}

return plainText;
}

public string Encrypt(string plainText)
{
string encryptedText;
byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);

RijndaelManaged rijndaelManaged = new RijndaelManaged { Mode = CipherMode.CBC };

using (ICryptoTransform encryptor = rijndaelManaged.CreateEncryptor(_KeyBytes, _InitVectorBytes))
{
using (MemoryStream memoryStream = new MemoryStream())
{
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
{
cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
cryptoStream.FlushFinalBlock();

byte[] cipherTextBytes = memoryStream.ToArray();
encryptedText = Convert.ToBase64String(cipherTextBytes);
}
}
}

return encryptedText;
}

using this method i want to encrypt and decrypt.

please help me.
Posted
Updated 11-Dec-13 18:27pm
v2
Comments
bowlturner 12-Dec-13 9:00am    
So you want to take an XML document, make it a string, encrypt the whole string and then turn the encrypted string back into an XML document?

Silly error. Setting the encryption/decryption aside, since that's not your question, your issue is that you encrypt your string, and then try to load that encrypted string (which is no longer XML) into an XDocument. This would give you the same result:
C#
XDocument doc1 = XDocument.Parse("this is nonsense");
 
Share this answer
 
Comments
Hamassss 12-Dec-13 9:11am    
Yeah, sounds like he want his encrypted string to be XML :D
Data at the root level is invalid. Line 1, position 1.

Of course, i doubt ur encoded string starts with
XML
<root><element1>
etc...

You cannot make Xml out of any string.
Here is tutorial about xml

http://www.w3schools.com/xml/[^]
hope it helps
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900