Get the load func in place

This commit is contained in:
Tim Young 2017-08-09 16:34:42 -05:00
parent 8574d61349
commit 1e598470ef

View File

@ -88,15 +88,20 @@ namespace EduNetworkBuilder
#region Load and Save #region Load and Save
//Load and save functions //Load and save functions
public void Load(string filename) public bool Load(string filename, string password = "")
{ {
XmlDocument xmlDoc = new XmlDocument(); XmlDocument xmlDoc = new XmlDocument();
if (File.Exists(filename)) if (File.Exists(filename))
{ {
xmlDoc.Load(filename); xmlDoc.Load(filename);
if (TryToDecrypt(xmlDoc, password) != null)
{
Load(xmlDoc); Load(xmlDoc);
return true;
} }
} }
return false;
}
public void Load(XmlNode TheNode) public void Load(XmlNode TheNode)
{ {
@ -263,6 +268,61 @@ namespace EduNetworkBuilder
} }
return xmlTDES.Doc; return xmlTDES.Doc;
} }
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 #endregion
public bool AddHomework(SchoolworkClass ToAdd) public bool AddHomework(SchoolworkClass ToAdd)