public KeePassLib.PwGroup getGroup(string name) { KeePassLib.PwGroup group = new KeePassLib.PwGroup(); var ioconninfo = new KeePassLib.Serialization.IOConnectionInfo(); if (!(string.IsNullOrEmpty(KeepassDBFilePath))) { ioconninfo.Path = base64Decode(KeepassDBFilePath); KeePassLib.Keys.CompositeKey compkey = new KeePassLib.Keys.CompositeKey(); if (string.IsNullOrEmpty(KeepassKeyFilePath) && string.IsNullOrEmpty(KeepassMasterPassword)) { throw new Exception("A Key file or Master Password has not been set!"); } else { if (!(string.IsNullOrEmpty(KeepassKeyFilePath))) { compkey.AddUserKey(new KeePassLib.Keys.KcpKeyFile(base64Decode(KeepassKeyFilePath))); } if (!(string.IsNullOrEmpty(KeepassMasterPassword))) { compkey.AddUserKey(new KeePassLib.Keys.KcpPassword(base64Decode(KeepassMasterPassword))); } var db = new KeePassLib.PwDatabase(); try { db.Open(ioconninfo, compkey, null); KeePassLib.Collections.PwObjectList <KeePassLib.PwGroup> groups = db.RootGroup.GetGroups(true); group = groups.First(i => i.Name == name); } catch { throw; } finally { if (db.IsOpen) { db.Close(); db = null; } } } } else { throw new Exception("Keepass DB Path has not been set!"); } return(group); }
private static List <CredentialSet> LoadKeePass() { try { var ioConnInfo = new KeePassLib.Serialization.IOConnectionInfo { Path = Main.Settings.Settings.KeePassPath }; var compKey = new KeePassLib.Keys.CompositeKey(); compKey.AddUserKey(new KeePassLib.Keys.KcpPassword(Main.Settings.Settings.KeePassPassword)); var db = new KeePassLib.PwDatabase(); db.Open(ioConnInfo, compKey, null); var entries = db.RootGroup.GetEntries(true); List <CredentialSet> list = new List <CredentialSet>(); foreach (var entry in entries) { string title = entry.Strings.ReadSafe("Title"); string userName = entry.Strings.ReadSafe("UserName"); string domain = entry.Strings.ReadSafe("Domain"); if (!string.IsNullOrEmpty(title) && !string.IsNullOrEmpty(userName)) { CredentialSet credentialSet = new CredentialSet { Name = title, Username = string.IsNullOrEmpty(domain) && userName.Contains("\\") ? userName.Split(new string[] { "\\" }, StringSplitOptions.None)[1] : userName, Domain = string.IsNullOrEmpty(domain) && userName.Contains("\\") ? userName.Split(new string[] { "\\" }, StringSplitOptions.None)[0] : domain, Password = entry.Strings.ReadSafe("Password") }; list.Add(credentialSet); string id = entry.Uuid.ToHexString(); if (!keyPassCredentialsById.ContainsKey(id)) { keyPassCredentialsById.Add(id, credentialSet); } } } db.Close(); return(list); } catch (Exception ex) { Log.Error("Error loading KeePass-File due to the following reason: " + ex.Message, ex); return(new List <CredentialSet>()); } }
private static bool Export(KeePassLib.PwDatabase database, Uri filePath, KeePassLib.Security.ProtectedString password, KeePassLib.Interfaces.IStatusLogger logger) { Exception argumentError = CheckArgument(database, filePath, password); if (!ReferenceEquals(argumentError, null)) { throw argumentError; } if (string.Equals(database.IOConnectionInfo.Path, filePath.LocalPath, StringComparison.InvariantCultureIgnoreCase)) { return(false); //Don't export myself } //Create new database in temporary file KeePassLib.PwDatabase exportedDatabase = new KeePassLib.PwDatabase(); exportedDatabase.Compression = KeePassLib.PwCompressionAlgorithm.GZip; KeePassLib.Serialization.IOConnectionInfo connectionInfo = new KeePassLib.Serialization.IOConnectionInfo(); string storageDirectory = Path.GetDirectoryName(filePath.LocalPath); string tmpPath = Path.Combine(storageDirectory, string.Format("{0}{1}", Guid.NewGuid(), KeePassDatabaseExtension)); connectionInfo.Path = tmpPath; connectionInfo.CredSaveMode = KeePassLib.Serialization.IOCredSaveMode.SaveCred; KeePassLib.Keys.CompositeKey exportedKey = new KeePassLib.Keys.CompositeKey(); exportedKey.AddUserKey(new KeePassLib.Keys.KcpPassword(password.ReadString())); exportedDatabase.New(connectionInfo, exportedKey); exportedDatabase.RootGroup.Name = database.RootGroup.Name; //Merge current database in temporary file exportedDatabase.MergeIn(database, KeePassLib.PwMergeMethod.OverwriteExisting, logger); exportedDatabase.Save(logger); exportedDatabase.Close(); //Move temporary file into target backup path if (File.Exists(filePath.LocalPath)) { File.Delete(filePath.LocalPath); } File.Move(tmpPath, filePath.LocalPath); return(true); }
private static List<CredentialSet> LoadKeePass() { try { var ioConnInfo = new KeePassLib.Serialization.IOConnectionInfo { Path = Main.Settings.Settings.KeePassPath }; var compKey = new KeePassLib.Keys.CompositeKey(); compKey.AddUserKey(new KeePassLib.Keys.KcpPassword(Main.Settings.Settings.KeePassPassword)); var db = new KeePassLib.PwDatabase(); db.Open(ioConnInfo, compKey, null); var entries = db.RootGroup.GetEntries(true); List<CredentialSet> list = new List<CredentialSet>(); foreach (var entry in entries) { string title = entry.Strings.ReadSafe("Title"); string userName = entry.Strings.ReadSafe("UserName"); string domain = entry.Strings.ReadSafe("Domain"); if (!string.IsNullOrEmpty(title) && !string.IsNullOrEmpty(userName)) { list.Add(new CredentialSet { Name = title, Username = string.IsNullOrEmpty(domain) && userName.Contains("\\") ? userName.Split(new string[] {"\\"}, StringSplitOptions.None)[1] : userName, Domain = string.IsNullOrEmpty(domain) && userName.Contains("\\") ? userName.Split(new string[] {"\\"}, StringSplitOptions.None)[0] : domain, Password = entry.Strings.ReadSafe("Password") }); } } db.Close(); return list; } catch (Exception ex) { Log.Error("Error loading KeePass-File due to the following reason: " + ex.Message, ex); return new List<CredentialSet>(); } }
public string getData(string value, string kpColumn2Search = "Title", string kpColumn2Return = "Password") { string returnValue = string.Empty; var ioconninfo = new KeePassLib.Serialization.IOConnectionInfo(); if (!(string.IsNullOrEmpty(KeepassDBFilePath))) { ioconninfo.Path = base64Decode(KeepassDBFilePath); KeePassLib.Keys.CompositeKey compkey = new KeePassLib.Keys.CompositeKey(); if (string.IsNullOrEmpty(KeepassKeyFilePath) && string.IsNullOrEmpty(KeepassMasterPassword)) { throw new Exception("A Key file or Master Password has not been set!"); } else { if (!(string.IsNullOrEmpty(KeepassKeyFilePath))) { compkey.AddUserKey(new KeePassLib.Keys.KcpKeyFile(base64Decode(KeepassKeyFilePath))); } if (!(string.IsNullOrEmpty(KeepassMasterPassword))) { compkey.AddUserKey(new KeePassLib.Keys.KcpPassword(base64Decode(KeepassMasterPassword))); } var db = new KeePassLib.PwDatabase(); try { db.Open(ioconninfo, compkey, null); KeePassLib.Collections.PwObjectList <KeePassLib.PwEntry> entries = db.RootGroup.GetEntries(true); //var data = from entry in db.rootgroup.getentries(true) where entry.strings.readsafe("title") == "tyler-u-client-id" select entry; KeePassLib.PwEntry pw = entries.FirstOrDefault(i => i.Strings.ReadSafe(kpColumn2Search) == value); if (pw != null) { returnValue = pw.Strings.ReadSafe(kpColumn2Return); } else { returnValue = string.Empty; } pw = null; } catch { throw; } finally { if (db.IsOpen) { db.Close(); db = null; } } } } else { throw new Exception("Keepass DB Path has not been set!"); } return(returnValue); }