/// <summary> /// Loads a KDB file and stores all loaded entries in the current /// PwDatabase instance. /// </summary> /// <param name="strFilePath">Relative or absolute path to the file to open.</param> public void Load(string strFilePath) { Debug.Assert(strFilePath != null); if (strFilePath == null) { throw new ArgumentNullException("strFilePath"); } Kdb3Manager mgr = new Kdb3Manager(); Kdb3ErrorCode e; e = Kdb3File.SetDatabaseKey(mgr, m_pwDatabase.MasterKey); if (e != Kdb3ErrorCode.Success) { throw new Exception(KLRes.InvalidCompositeKey); } e = mgr.OpenDatabase(strFilePath, IntPtr.Zero); if (e != Kdb3ErrorCode.Success) { mgr.Unload(); throw new Exception(KLRes.FileLoadFailed); } // Copy properties m_pwDatabase.KeyEncryptionRounds = mgr.KeyTransformationRounds; // Read groups and entries Dictionary <UInt32, PwGroup> dictGroups = ReadGroups(mgr); ReadEntries(mgr, dictGroups); mgr.Unload(); }
/// <summary> /// Save the contents of the current <c>PwDatabase</c> to a KDB file. /// </summary> /// <param name="strSaveToFile">Location to save the KDB file to.</param> public void Save(string strSaveToFile, PwGroup pgDataSource) { Debug.Assert(strSaveToFile != null); if (strSaveToFile == null) { throw new ArgumentNullException("strSaveToFile"); } Kdb3Manager mgr = new Kdb3Manager(); Kdb3ErrorCode e = Kdb3File.SetDatabaseKey(mgr, m_pwDatabase.MasterKey); if (e != Kdb3ErrorCode.Success) { Debug.Assert(false); throw new Exception(KLRes.InvalidCompositeKey); } if (m_slLogger != null) { if (m_pwDatabase.Name.Length != 0) { m_slLogger.SetText(Kdb3Prefix + KPRes.FormatNoDatabaseName, LogStatusType.Warning); } if (m_pwDatabase.Description.Length != 0) { m_slLogger.SetText(Kdb3Prefix + KPRes.FormatNoDatabaseDesc, LogStatusType.Warning); } } // Set properties if (m_pwDatabase.KeyEncryptionRounds >= (ulong)UInt32.MaxValue) { mgr.KeyTransformationRounds = unchecked (uint.MaxValue - 1); } else { mgr.KeyTransformationRounds = (uint)m_pwDatabase.KeyEncryptionRounds; } PwGroup pgRoot = (pgDataSource ?? m_pwDatabase.RootGroup); // Write groups and entries Dictionary <PwGroup, UInt32> dictGroups = WriteGroups(mgr, pgRoot); WriteEntries(mgr, dictGroups, pgRoot); e = mgr.SaveDatabase(strSaveToFile); if (e != Kdb3ErrorCode.Success) { throw new Exception(KLRes.FileSaveFailed); } mgr.Unload(); }