/// <summary> try load the database, could fail if password is incorrect </summary> private bool TryLoad(string password, out CredentialDatabaseEntity entity) { if (!File.Exists(this.fileName)) { entity = this.CreateEmptyEntity(password); return(true); } entity = null; try { var data = File.ReadAllBytes(this.fileName); AESCryptographer crypt = new AESCryptographer(); //TEMP var salt = UTF8Encoding.UTF8.GetBytes(password.PadRight(16, 'x')); var decryptedData = crypt.Decrypt(password, data, salt); return(this.TryDeserialize <CredentialDatabaseEntity>(decryptedData, out entity)); } catch (Exception) { return(false); } }
/// <summary/> public void Save(string password, List <CredentialEntity> credentials) { CredentialDatabaseEntity database = new CredentialDatabaseEntity(); database.Password = password; database.Version = Version; database.Credentials = credentials; XmlSerializer serializer = new XmlSerializer(typeof(CredentialDatabaseEntity)); using (var memoryStream = new MemoryStream()) { serializer.Serialize(memoryStream, database); AESCryptographer crypt = new AESCryptographer(); //TEMP var salt = UTF8Encoding.UTF8.GetBytes(password.PadRight(16, 'x')); var encryptedData = crypt.Encrypt(password, memoryStream.ToArray(), salt); File.WriteAllBytes(this.fileName, encryptedData); } }