Exemplo n.º 1
0
        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);
        }