Hello, I created a class called "ClassA" and I write its content to hard drive as an XML file like this:
static void Write(ClassA cA)
{
var serializer = new XmlSerializer(typeof(ClassA));
using (var stream = new FileStream(System.IO.Path.Combine("C:\\","AFile.XML"), FileMode.Create))
{
serializer.Serialize(stream, cA);
stream.Close();
stream.Dispose();
}
}
And I read it like this:
static ClassA Load()
{
var serializer = new XmlSerializer(typeof(ClassA));
using (FileStream stream = new FileStream(System.IO.Path.Combine("C:\\","AFile.XML"), FileMode.Open))
{
var container = serializer.Deserialize(stream) as ClassA;
stream.Close();
stream.Dispose();
return container;
}
}
The problem is, the XML file is - obviously - plain text and everyone can see and change its values. How can I encrypt the whole XML file so that people can't easily manipulate it? I would be glad if you help step by step.
Thanks.
What I have tried:
I tried this for encrypting, but it kept saying "Wrong Key length" and I didn't actually get it whether it encrypts whole XML file or just the values. Overall, it just did not work and kept saying the error.
string sKey =
var serializer = new XmlSerializer(obj.GetType());
var stream = new FileStream(path, FileMode.Create);
DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
ICryptoTransform desencrypt = DES.CreateEncryptor();
using(CryptoStream cStream = new CryptoStream(stream, desencrypt, CryptoStreamMode.Write)){
serializer.Serialize(cStream, obj);
}
stream.Close ();
The sample code is in the link below, You can find the other part (reading, decrypting) there.
Encrypting an XML File : Unity3D[
^]