From 1e598470efbf915f30815274db6d9d3150e39a21 Mon Sep 17 00:00:00 2001 From: Tim Young Date: Wed, 9 Aug 2017 16:34:42 -0500 Subject: [PATCH] Get the load func in place --- EduNetworkBuilder/PersonClass.cs | 68 ++++++++++++++++++++++++++++++-- 1 file changed, 64 insertions(+), 4 deletions(-) diff --git a/EduNetworkBuilder/PersonClass.cs b/EduNetworkBuilder/PersonClass.cs index 078a12b..88b11a7 100644 --- a/EduNetworkBuilder/PersonClass.cs +++ b/EduNetworkBuilder/PersonClass.cs @@ -88,14 +88,19 @@ namespace EduNetworkBuilder #region Load and Save //Load and save functions - public void Load(string filename) + public bool Load(string filename, string password = "") { XmlDocument xmlDoc = new XmlDocument(); if (File.Exists(filename)) { xmlDoc.Load(filename); - Load(xmlDoc); + if (TryToDecrypt(xmlDoc, password) != null) + { + Load(xmlDoc); + return true; + } } + return false; } public void Load(XmlNode TheNode) @@ -263,9 +268,64 @@ namespace EduNetworkBuilder } return xmlTDES.Doc; } - #endregion - public bool AddHomework(SchoolworkClass ToAdd) + private XmlDocument TryToDecrypt(XmlDocument What, string PassToTry) + { + bool first_failed = false; + XmlElement inputElement = What.GetElementsByTagName("Salt")[0] as XmlElement; + if (inputElement == null) return What; //No salt means it was never encrypted. Load as normal + string salt = inputElement.InnerText; + // Create a new TripleDES key. + TripleDES tDESkey = TrippleDESDocumentEncryption.GenKey(PassToTry, salt); + TrippleDESDocumentEncryption xmlTDES = new TrippleDESDocumentEncryption(What, tDESkey); + + //First, try to decrypt it using the user password. + try + { + xmlTDES.Decrypt("User"); + } + catch (LoginException le) + { + //we ignore this. Just try it again. + first_failed = true; + } + catch(Exception e) //This is bad. Blow up and exit the function + { + MessageBox.Show(e.ToString()); + return null; + } + + if (first_failed) + { //try a second one, decrypting the alt key and then using that. The admin does this. + // Create a new instance of the TrippleDESDocumentEncryption object + // defined in this sample. + try + { + xmlTDES.Decrypt("EncryptedKey"); + + inputElement = xmlTDES.Doc.GetElementsByTagName("EncryptedKey")[0] as XmlElement; + + string userpw = inputElement.InnerText; + xmlTDES.SetKey(userpw, salt); + + xmlTDES.Decrypt("User"); + } + catch (LoginException le) + { + //Neither password worked. Exit out so we can prompt for an appropriate password + return null; + } + catch (Exception e) //This is bad. Blow up and exit the function + { + MessageBox.Show(e.ToString()); + return null; + } + } + return xmlTDES.Doc; + } + #endregion + + public bool AddHomework(SchoolworkClass ToAdd) { //Check to see if we already exist foreach(SchoolworkClass one in Projects)