public void LoadFromFile(string fileName) { IsLocked = true; FileName = fileName; var full = File.ReadAllBytes(fileName); var type = DecideTemplateOperations(fileName); if (type > 0) { Logger.Debug("LoadFromFile Type : " + type); var zipContent = TemplateOperations.ExtractDataContentFromNLock(full); try { Template = TemplateOperations.ExtractTemplateFromNLock(full); Template = _encryptionStrategy.Decrypt(zipContent, Template); } catch (Exception) { throw; } UnlockForm loginFrom; if (type == 2) { var hash = TemplateOperations.GetHashFromNLock(full); Logger.Debug("LoadFromFile Hash : " + Encoding.UTF8.GetString(hash)); loginFrom = new UnlockForm(Template, hash, fileName); } else { loginFrom = new UnlockForm(Template, fileName) { PwHash = null }; } loginFrom.ShowDialog(); var result = loginFrom.DialogResult; if (result != DialogResult.Cancel) { _verified = loginFrom.Verified; if (Unlock() == VerificationStatus.Failed) { throw new Exception("Loading from file failed"); } } loginFrom.Dispose(); } else { throw new Exception("Loading from file failed"); } }
public List <T> DeserializeObject() { try { EnsureFolder(_configuration.StorageLocation); if (File.Exists(_configuration.GetFullPath()) == false) { return(new List <T>()); } string encryptedFile = null; using (StreamReader file = File.OpenText(_configuration.GetFullPath())) { encryptedFile = file.ReadToEnd(); } string decriptedText = _encryptionStrategy.Decrypt(encryptedFile, _configuration.DecryptedMasterKey); return(JsonConvert.DeserializeObject <List <T> >(decriptedText)); } catch (Newtonsoft.Json.JsonReaderException ex) { _logger.Error(ex.Message); throw new EncryptedDeserializationException(ex.Message); } }
public string Decrypt() { try { return(_strategy.Decrypt(_message)); } catch (NullReferenceException e) { System.Windows.Forms.MessageBox.Show("Strategy for encryption not set." + e.ToString(), "Strategy not set.", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error, System.Windows.Forms.MessageBoxDefaultButton.Button1); return(null); } }
public string Decrypt(string content) { return(_strategy.Decrypt(content)); }