Store sensitive information inside keepass database from c#

前端 未结 3 598
深忆病人
深忆病人 2020-12-13 07:58

I have a project where I have to handle sensitive data.

How do I open a keepass database from C# to use the data?

I have downloaded the source.

相关标签:
3条回答
  • 2020-12-13 07:59

    Here is an extension of the original answer from Ronnie - walking the keepass tree recursively. This outputs a format that jsTree can use by the way.

        public static void JsonData() {
            var dbpath = Web.MapPath(@"your-password-file.kdbx");
            var masterpw = "Your$uper$tr0ngMst3rP@ssw0rd";
            var ioConnInfo = new IOConnectionInfo { Path = dbpath };
            var compKey = new CompositeKey();
            compKey.AddUserKey(new KcpPassword(masterpw));
            var db = new KeePassLib.PwDatabase();
            db.Open(ioConnInfo, compKey, null);
    
            //get everything
            var kpdata = from entry in db.RootGroup.GetEntries(true)
                                     select new {
                                         Group = entry.ParentGroup.Name,
                                         Title = entry.Strings.ReadSafe("Title"),
                                         Username = entry.Strings.ReadSafe("UserName"),
                                         Password = entry.Strings.ReadSafe("Password"),
                                         URL = entry.Strings.ReadSafe("URL"),
                                         Notes = entry.Strings.ReadSafe("Notes")
                                     };
            var kproot = db.RootGroup.Groups;
            string lastGroup = "#";
            uint sc = 0;
            int depth = 0;
            var parent = "#"; //root is # parent
            foreach (var entry in kproot) {
                PwGroup pwGroup = db.RootGroup.Groups.GetAt(sc);
                Web.Write(" { \"id\" : \"" + (sc) + "\", \"parent\" : \"" + parent + "\", \"text\" : \"" + pwGroup.Name.HtmlEncode() + "\" },\n");
    
                WriteChildren(pwGroup,sc+"", depth + 1);
                sc++;
            }
            db.Close();
        }
    
        public static void WriteChildren(PwGroup pwGroup, string parentID,int depth) {
            uint sc = 0;
            //if(depth>3)return;  //used to prevent too much recursion
            foreach (var entry in pwGroup.Groups) {
                var subGroup = pwGroup.Groups.GetAt(sc);
                var curID = (parentID+"_"+sc);
                Web.Write(" { \"id\" : \"" + curID + "\", \"parent\" : \"" + parentID + "\", \"text\" : \"" + subGroup.Name.JsEncode() + "\"},\n");
                WriteChildren(subGroup, curID, depth+1);
                WriteLeaves(subGroup, curID, depth);
                sc++;
            }
        }
        public static void WriteLeaves(PwGroup pwGroup, string parentID,int depth) {
            uint sc = 0;
            //if(depth>3)return;
            var entryList = pwGroup.GetEntries(false);
            foreach (var entry in entryList) {
                var curID = (parentID+"_"+sc);
                Web.Write(" { \"id\" : \"" + curID + "\", \"parent\" : \"" + parentID + "\", \"text\" : \"" + entry.Strings.ReadSafe("Title").JsEncode() + "\", \"password\" : \"" + entry.Strings.ReadSafe("Password").JsEncode() + "\", \"type\" : \"file\"},\n");
                sc++;
            }
        }
    
    0 讨论(0)
  • 2020-12-13 08:15

    I thought about reading a KeyPass 2 database so I added a reference to KeyPass.exe in Linqpad and started to experiment. To my surprise and without any outside help (a testament to a good API), I was reading the database after only a few minutes. Here's how I did it:

    var dbpath = @"C:\path\to\passwords.kdbx";
    var masterpw = "Your$uper$tr0ngMst3rP@ssw0rd";
    
    var ioConnInfo = new IOConnectionInfo { Path = dbpath };
    var compKey = new CompositeKey();
    compKey.AddUserKey(new KcpPassword(masterpw));
    
    var db = new KeePassLib.PwDatabase();
    db.Open(ioConnInfo, compKey, null);
    
    var kpdata = from entry in db.RootGroup.GetEntries(true)
                    select new
                    {
                        Group = entry.ParentGroup.Name,
                        Title = entry.Strings.ReadSafe("Title"),
                        Username = entry.Strings.ReadSafe("UserName"),
                        Password = entry.Strings.ReadSafe("Password"),
                        URL = entry.Strings.ReadSafe("URL"),
                        Notes = entry.Strings.ReadSafe("Notes")
    
                    };                                                                                  
    
    kpdata.Dump(); // this is how Linqpad outputs stuff
    db.Close();
    
    0 讨论(0)
  • 2020-12-13 08:25

    Check : KeePass Password Safe (For how keepass works)

    Rather use the C# System.Cryptography classes and store you data enrypted in a database or txt file...

    There is a KeePass-2.05-Alpha-Source.zip,The latest version of KeePass. C# source code,1919KB

    • http://s.pudn.com/upload_log_en.asp?e=1781366
    • http://en.pudn.com/downloads175/sourcecode/windows/other/detail816102_en.html
    0 讨论(0)
提交回复
热议问题