Reputation: 59929
For a scoreboard, i store the scores in an xml file. (WPF application) Of course, i dont want a user to just edit that xml file, so i was looking for a way to encrypt the xml file. this works trough the method i found here: http://srinivasganaparthi.blogspot.com/2011/04/encrypt-and-decrypt-xml-file-in-c.html
So, i have put this in a class file in my project:
public static void EncryptAndSerialize(Object obj)
{
UnicodeEncoding aUE = new UnicodeEncoding();
byte[] key = aUE.GetBytes("password");
RijndaelManaged RMCrypto = new RijndaelManaged();
using (FileStream fs = File.Open(@"ScoreData.xml", FileMode.Create))
{
using (CryptoStream cs = new CryptoStream(fs, RMCrypto.CreateEncryptor(key, key), CryptoStreamMode.Write))
{
XmlSerializer xmlser = new XmlSerializer(obj.GetType());
xmlser.Serialize(cs, obj);
}
fs.Close();
}
}
public static DataSet DecryptAndDeserialize(string filename)
{
DataSet ds = new DataSet();
FileStream aFileStream = new FileStream(filename, FileMode.Open);
StreamReader aStreamReader = new StreamReader(aFileStream);
UnicodeEncoding aUE = new UnicodeEncoding();
byte[] key = aUE.GetBytes("password");
RijndaelManaged RMCrypto = new RijndaelManaged();
CryptoStream aCryptoStream = new CryptoStream(aFileStream, RMCrypto.CreateDecryptor(key, key), CryptoStreamMode.Read);
//Restore the data set to memory.
ds.ReadXml(aCryptoStream);
aStreamReader.Close();
aFileStream.Close();
return ds;
}
And it works very good indeed, the XML file gets encrypted in the part where i write the xml file here:
private void saveScore_Click(object sender, RoutedEventArgs e)
{
if (!File.Exists("ScoreData.xml")) //als file nog niet bestaat
{
XmlTextWriter textWritter = new XmlTextWriter("ScoreData.xml", null);
textWritter.WriteStartDocument();
textWritter.WriteStartElement("Data");
textWritter.WriteEndElement();
textWritter.Close();
}
XmlDocument xmlDoc = new XmlDocument();
dweMethods.DecryptAndDeserialize("ScoreData.xml");
xmlDoc.Load("ScoreData.xml");
XmlElement subRoot = xmlDoc.CreateElement("Persons");
//Naam
XmlElement appendedElementNaam = xmlDoc.CreateElement("Name");
XmlText xmlTextNaam = xmlDoc.CreateTextNode(nameOfPerson.Text);
appendedElementNaam.AppendChild(xmlTextNaam);
subRoot.AppendChild(appendedElementNaam);
xmlDoc.DocumentElement.AppendChild(subRoot);
//Score
XmlElement appendedElementScore = xmlDoc.CreateElement("Score");
XmlText xmlTextScore = xmlDoc.CreateTextNode(Convert.ToString(endScore));
appendedElementScore.AppendChild(xmlTextScore);
subRoot.AppendChild(appendedElementScore);
xmlDoc.DocumentElement.AppendChild(subRoot);
//Date
XmlElement appendedElementDate = xmlDoc.CreateElement("Date");
XmlText xmlTextDate = xmlDoc.CreateTextNode(DateTime.Now.ToString("d/M/yyyy"));
appendedElementDate.AppendChild(xmlTextDate);
subRoot.AppendChild(appendedElementDate);
xmlDoc.DocumentElement.AppendChild(subRoot);
xmlDoc.Save("ScoreData.xml");
dweMethods.EncryptAndSerialize("ScoreData.xml");
}
But here comes the part i just dont understand how to do: To view the "scoreboard", the user opens a window were a datagrid is, where the datagrid just reads the xml file. Now, because the xml file is encrypted, the datagrid can't just read the xml file. (also here above, when it loads the xml, it has the same issue)
The reading is done like this:
public ScoreBoard()
{
InitializeComponent();
dweMethods.DecryptAndDeserialize("ScoreData.xml");
XElement TrackList = XElement.Load("ScoreData.xml");
LibraryView.DataContext = TrackList;
}
in that second line i have dweMethods.DecryptAndDeserialize("ScoreData.xml");
but im not sure what to bind it to, and then how i can make the Xelement.Load, load the decrypted file.
The code above, makes the application crash now (of course) because it tries to read an unreadable xml file now.
Could anyone help me out a bit here? I think im just forgetting some very small, but logical step here.
Thank you very much in advance.
========================
Edit1 (after first answer)
To open the xml to write to, the code is now like this (edited from above) private void saveScore_Click(object sender, RoutedEventArgs e)
{
if (!File.Exists("ScoreData.xml")) //als file nog niet bestaat
{
XmlTextWriter textWritter = new XmlTextWriter("ScoreData.xml", null);
textWritter.WriteStartDocument();
textWritter.WriteStartElement("Data");
textWritter.WriteEndElement();
textWritter.Close();
dweMethods.EncryptAndSerialize("ScoreData.xml");
}
XmlDocument xmlDoc = new XmlDocument();
DataSet ds = dweMethods.DecryptAndDeserialize("ScoreData.xml");
xmlDoc.Load(ds.GetXml());
XmlElement subRoot = xmlDoc.CreateElement("Persons");
//Naam
XmlElement appendedElementNaam = xmlDoc.CreateElement("Name");
XmlText xmlTextNaam = xmlDoc.CreateTextNode(nameOfPerson.Text);
appendedElementNaam.AppendChild(xmlTextNaam);
subRoot.AppendChild(appendedElementNaam);
xmlDoc.DocumentElement.AppendChild(subRoot);
//Score
XmlElement appendedElementScore = xmlDoc.CreateElement("Score");
XmlText xmlTextScore = xmlDoc.CreateTextNode(Convert.ToString(endScore));
appendedElementScore.AppendChild(xmlTextScore);
subRoot.AppendChild(appendedElementScore);
xmlDoc.DocumentElement.AppendChild(subRoot);
//Date
XmlElement appendedElementDate = xmlDoc.CreateElement("Date");
XmlText xmlTextDate = xmlDoc.CreateTextNode(DateTime.Now.ToString("d/M/yyyy"));
appendedElementDate.AppendChild(xmlTextDate);
subRoot.AppendChild(appendedElementDate);
xmlDoc.DocumentElement.AppendChild(subRoot);
xmlDoc.Save("ScoreData.xml");
dweMethods.EncryptAndSerialize("ScoreData.xml");
}
the error now is in the line xmlDoc.Load(ds.GetXml());
where it now says to have Illegal characters in path. but this should not be possible if it has been decrypted correct i guess :/
Upvotes: 1
Views: 2741
Reputation: 4708
You can use XElement.Parse()
to load xml from a string
public ScoreBoard()
{
InitializeComponent();
DataSet ds = dweMethods.DecryptAndDeserialize("ScoreData.xml")
XElement TrackList = XElement.Parse(ds.GetXml());
LibraryView.DataContext = TrackList;
}
Upvotes: 2