Save an encrypted person class

This commit is contained in:
Tim Young 2017-08-09 16:20:05 -05:00
parent e4edd9bf5a
commit 66657507ed

View File

@ -7,6 +7,8 @@ using System.Xml;
using System.IO; using System.IO;
using System.Windows.Forms; using System.Windows.Forms;
using System.Drawing; using System.Drawing;
using System.Security.Cryptography;
using System.Security.Cryptography.Xml;
namespace EduNetworkBuilder namespace EduNetworkBuilder
{ {
@ -30,7 +32,7 @@ namespace EduNetworkBuilder
/// </summary> /// </summary>
public string FullName = ""; public string FullName = "";
List<string> Passwords = new List<string>(); string Password = "";
string PasswordHint = ""; string PasswordHint = "";
/// <summary> /// <summary>
@ -79,22 +81,9 @@ namespace EduNetworkBuilder
} }
public string Password(int index = 0)
{
if (Passwords.Count == 0) return "";
if (index < 0) return "";
if (index >= Passwords.Count) return "";
return Passwords[index];
}
public void ChangePassword(string NewPassword) public void ChangePassword(string NewPassword)
{ {
Passwords.Insert(0, NewPassword); Password = NewPassword;
int maxPWs = 1;
if (isAdmin)
maxPWs = 10; //Admins store the last 10 passwords. If the admin changed his PW, we still need to decrypt the student's files
for (int a = Passwords.Count - 1; a >= maxPWs; a--)
Passwords.RemoveAt(a); //Remove all but the one password
} }
#region Load and Save #region Load and Save
@ -149,7 +138,7 @@ namespace EduNetworkBuilder
ChangePassAtFirstLogin = changepw; ChangePassAtFirstLogin = changepw;
break; break;
case "password": case "password":
Passwords.Add(Individual.InnerText); Password = Individual.InnerText;
break; break;
case "settings": case "settings":
UserSettings = NB.Deserialize<NBSettings>(Individual.InnerText); UserSettings = NB.Deserialize<NBSettings>(Individual.InnerText);
@ -198,7 +187,12 @@ namespace EduNetworkBuilder
writer.WriteEndElement(); writer.WriteEndElement();
writer.WriteEndDocument(); writer.WriteEndDocument();
} }
if(TryToEncrypt(doc) != null)
doc.Save(filename); doc.Save(filename);
else
{
//We should blow up gracefully. Not sure why we failed.
}
} }
@ -222,8 +216,7 @@ namespace EduNetworkBuilder
string settingsstring = NB.SerializeObject<NBSettings>(UserSettings); string settingsstring = NB.SerializeObject<NBSettings>(UserSettings);
writer.WriteElementString("Settings", settingsstring); writer.WriteElementString("Settings", settingsstring);
foreach(string One in Passwords) writer.WriteElementString("Password", Password);
writer.WriteElementString("Password", One);
foreach (PersonClass PC in Students) foreach (PersonClass PC in Students)
PC.Save(writer, true); //Save as a student entry PC.Save(writer, true); //Save as a student entry
//Save all the devices //Save all the devices
@ -233,6 +226,43 @@ namespace EduNetworkBuilder
} }
writer.WriteEndElement(); writer.WriteEndElement();
} }
private XmlDocument TryToEncrypt(XmlDocument What)
{
string UserPassword = UserName + Password;
if (UserPassword == "") return null; //This should never happen
string salt = TrippleDESDocumentEncryption.GenSalt(NB.GetRandom());
TripleDES tDESkey = TrippleDESDocumentEncryption.GenKey(UserPassword, salt);
TrippleDESDocumentEncryption xmlTDES = new TrippleDESDocumentEncryption(What, tDESkey);
try
{
// Encrypt the "user" element.
xmlTDES.Encrypt("User");
//make the entries for the key
XmlNode tNode = xmlTDES.Doc.CreateElement("EncryptedKey");
tNode.InnerText = UserPassword;
XmlElement inputElement = xmlTDES.Doc.GetElementsByTagName("EncryptedData")[0] as XmlElement;
xmlTDES.Doc.DocumentElement.InsertAfter(tNode, inputElement);
//add the salt
tNode = xmlTDES.Doc.CreateElement("Salt");
tNode.InnerText = salt;
xmlTDES.Doc.DocumentElement.InsertAfter(tNode, inputElement);
//encrypt the user key with the admin key
xmlTDES.SetKey(AltPassword, salt);
xmlTDES.Encrypt("EncryptedKey");
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return null;
}
return xmlTDES.Doc;
}
#endregion #endregion
public bool AddHomework(SchoolworkClass ToAdd) public bool AddHomework(SchoolworkClass ToAdd)