In my application I have the following code for the encryption of some data as string:
private string EncryptPermission(User user)
{
Encoding encoding = Encoding.ASCII;
Cryptography cryptography = CreateCryptography(user);
byte[] val = encoding.GetBytes(user.Permission.ToString());
byte[] encrypted = cryptography.Encrypt(val);
string result = encoding.GetString(encrypted);
return result;
}
That works like I want. Now I want to write that data to an xml-file like:
internal void WriteUser(User user)
{
XElement userElement = new XElement("user",
new XAttribute("id", user.UserId.ToString("N")),
new XElement("name", user.Username),
new XElement("password", user.Password),
new XElement("permission", EncryptPermission(user)));
XDocument document = GetDocument();
if (document.Root != null)
{
document.Root.Add(userElement);
SaveDocument(document);
}
}
At the moment I call SaveDocument
I get an Exception
like:
A first chance exception of type 'System.ArgumentException' occurred in System.Xml.dll
Additional information: ' ', hexadecimal value 0x06, is a invalid char.
(Translated from german)
How can I solve this? I've already tried to use Converter.ToBase64String(..)
and Converter.FromBase64String(..)
but there I get an exception that the data is too long.