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,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)