16

我有一个项目,我必须处理敏感数据。

如何keepass从 C# 打开数据库以使用数据?

我已经下载了源码。我会查看它以获得我需要的东西。还有什么想法吗?

4

3 回答 3

40

我考虑过阅读 KeyPass 2 数据库,因此我在 Linqpad 中添加了对 KeyPass.exe 的引用并开始进行实验。令我惊讶的是,在没有任何外部帮助(良好 API 的证明)的情况下,我仅在几分钟后就读取了数据库。我是这样做的:

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();
于 2012-01-27T02:56:33.687 回答
1

这是 Ronnie 原始答案的扩展 - 递归地遍历 keepass 树。这输出了一个jsTree顺便可以使用的格式。

    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++;
        }
    }
于 2018-08-13T11:25:51.357 回答
0

检查:KeePass 密码安全(关于 keepass 的工作原理)

而是使用 C# System.Cryptography 类并将数据加密存储在数据库或 txt 文件中......

有一个 KeePass-2.05-Alpha-Source.zip,最新版本的 KeePass。C#源代码,1919KB

于 2011-01-13T13:20:18.083 回答