Esempio n. 1
0
        /// <summary>
        /// Reads the high scores XML file and returns a collection of PlayerScore objects, each representing a high score
        /// As the high scores XML file is encrypted, this method decypts it.
        /// </summary>
        /// <returns>A collection of PlayerScore objects representing the high scores</returns>
        /// <exception cref="System.Exception">Error occured while reading data.</exception>
        private List <PlayerScore> LoadScores()
        {
            if (File.Exists(FileName))
            {
                try
                {
                    //read the XML file
                    XmlDocument xmlDoc = new XmlDocument
                    {
                        PreserveWhitespace = true
                    };
                    xmlDoc.Load(FileName);

                    // Create a new RSA key.  This key will encrypt a symmetric key,
                    // which will then be imbedded in the XML document.
                    //need to move the key into the webconfig
                    CspParameters parameters = new CspParameters
                    {
                        KeyContainerName = ConfigurationSettings.AppSettings["ScoresFileEncryptionKey"]
                                           //KeyContainerName = "XML_ENC_RSA_KEY"
                    };
                    RSACryptoServiceProvider rsaKey = new RSACryptoServiceProvider(parameters);
                    // Decrypt the "Player" element.
                    TrippleDESDocumentEncryption.Decrypt(xmlDoc, rsaKey, "rsaKey");

                    List <PlayerScore> list = new List <PlayerScore>();         //Create a new list
                    XmlNodeList        elementsByTagName = null;
                    elementsByTagName = xmlDoc.GetElementsByTagName(HIGHSCORE); //find each score entry in the XML file
                    foreach (XmlNode node in elementsByTagName)
                    {
                        //for each score found in the XML file, create a new PlayerScore object with the data
                        PlayerScore item = new PlayerScore
                        {
                            Player = node[PLAYER].InnerText,
                            Score  = Convert.ToInt32(node[SCORE].InnerText),
                            Date   = Convert.ToDateTime(node[DATE].InnerText)
                        };
                        list.Add(item); //and add it to the collection
                    }
                    return(list);       //when finished, return this collection
                }
                catch (XmlException)
                {
                    //throw an exception if an error occured
                    throw new Exception("Error occured while reading data.");
                }
            }
            return(new List <PlayerScore>()); //if no XML file exists, return an empty list
        }
Esempio n. 2
0
        /// <summary>
        /// Creates a new high scores XML file using a collection of PlayerScore objects.  If the file already exists, it will be overwritten.
        /// This method encrypts the name of the player in the XML file.
        /// </summary>
        /// <param name="scores">A collection of PlayerScore objects, representing all the high scores needed to be stored</param>
        private void SaveScores(List <PlayerScore> scores)
        {
            try
            {
                //create the XML file
                XmlDocument   xmlDoc = new XmlDocument();
                XmlTextWriter writer = new XmlTextWriter(FileName, Encoding.UTF8)
                {
                    Formatting = Formatting.Indented
                };
                writer.WriteProcessingInstruction("xml", "version='1.0' encoding='UTF-8'");
                writer.WriteStartElement(SCORES);
                writer.Close();


                //Blank the file
                xmlDoc.PreserveWhitespace = true;
                xmlDoc.Load(FileName);

                // Create a new RSA key.  This key will encrypt a symmetric key,
                // which will then be imbedded in the XML document.
                CspParameters parameters = new CspParameters
                {
                    KeyContainerName = ConfigurationSettings.AppSettings["ScoresFileEncryptionKey"]
                                       //KeyContainerName = "XML_ENC_RSA_KEY"
                };
                RSACryptoServiceProvider rsaKey = new RSACryptoServiceProvider(parameters);
                // Decrypt the "Player" element.
                TrippleDESDocumentEncryption.Decrypt(xmlDoc, rsaKey, "rsaKey");

                //Add each high score to the XML file
                foreach (PlayerScore score in scores)
                {
                    //get the main node
                    XmlNode documentNode = xmlDoc.DocumentElement;

                    //create a new child node for each high score
                    XmlElement highScoreChild = xmlDoc.CreateElement(HIGHSCORE);

                    //create new elements for the name, score and date/time
                    XmlElement ScoreElement  = xmlDoc.CreateElement(SCORE);
                    XmlElement PlayerElement = xmlDoc.CreateElement(PLAYER);
                    XmlElement DateElement   = xmlDoc.CreateElement(DATE);

                    XmlText scoreText  = xmlDoc.CreateTextNode("score");
                    XmlText playerText = xmlDoc.CreateTextNode("player");
                    XmlText dateText   = xmlDoc.CreateTextNode("date");

                    //add the new high score child node to the main node
                    documentNode.AppendChild(highScoreChild);

                    //and add all the data to the high score child node
                    highScoreChild.AppendChild(ScoreElement);
                    highScoreChild.AppendChild(PlayerElement);
                    highScoreChild.AppendChild(DateElement);

                    ScoreElement.AppendChild(scoreText);
                    PlayerElement.AppendChild(playerText);
                    DateElement.AppendChild(dateText);

                    scoreText.Value  = score.Score.ToString();
                    playerText.Value = score.Player;
                    dateText.Value   = score.Date.ToString();

                    // Encrypt the "Player" element.
                    TrippleDESDocumentEncryption.Encrypt(xmlDoc, PLAYER, rsaKey, "rsaKey");
                }
                xmlDoc.Save(FileName); //save the file
            }
            catch (Exception exception4)
            {
            }
        }