Fork me on GitHub

Programming, Internet & more

Openkeepass: Feature release with write support

Openkeepass is around for a while now and has proved to be a major player when it comes to reading of KeePass files (especially KeePass 2.x).

However, since its initial release I’ve received a lot of requests regarding write support of KeePass files. After a lot of work this is finally done and has found its way into a new feature release of openkeepass.

Writing KeePass files

The major feature of version 0.5.0 is to support writing of KeePass files. To simply create a new KeePass file from scratch you can use the fluent API of the builders. A very simple example to write a KeePass file with only one single entry would be this:

// Create an entry
Entry entryOne = new EntryBuilder("First entry")
.password("Carls secret")

// Create the database file
KeePassFile keePassFile = new KeePassFileBuilder("myNewKeePassFile")

// Write database file to disk
KeePassDatabase.write(keePassFile, "masterPassword", "myNewKeePassFile.kdbx");

By nesting the available builders it is also possible to create much more complex structures in the KeePass file.

The code to create this structure looks like this:

// Create the more complex tree structure
Group root = new GroupBuilder("TestDb")
            .addEntry(new EntryBuilder("First entry").build())
            .addGroup(new GroupBuilder("Banking").build())
            .addGroup(new GroupBuilder("Internet")
                    .addGroup(new GroupBuilder("Shopping")
                            .addEntry(new EntryBuilder("Second entry").build())

// Create the database file    
KeePassFile keePassFile = new KeePassFileBuilder("TestDb")

// Write database file to disk     
KeePassDatabase.write(keePassFile, "masterPassword", "myNewKeePassFile.kdbx");

Modify existing KeePass files

There is also a new concept called zipper which comes into play when a KeePass structure should be modified instead of creating a new one from scratch. The zipper can be used to easily navigate through the tree structure of a KeePass file and can be compared with the concept of an iterator. There is always a pointer to an element in the tree and by navigating through the tree, the pointer to the current element will be shifted around.

This is very helpful if you want to replace some nodes in the tree. Please note that entries cannot be replaced directly in the tree, you have to modify the parent group of an entry if you want to modify or replace an entry.

The following example shows how to rename an existing group by using the zipper:

// Open keepass file
KeePassFile database = KeePassDatabase.getInstance("database.kdbx").openDatabase("password");
// Navigate through tree to group
GroupZipper zipper = new KeePassFileBuilder(database).getZipper()
// Rename group
Group group = zipper.getNode();
Group modifiedGroup = new GroupBuilder(group).name("test2").build();
// Replace old group with new one
KeePassFile modifiedDatabase = zipper.replace(modifiedGroup).close();

// Write database file to disk
KeePassDatabase.write(modifiedDatabase, "password", "modifiedDatabase.kdbx");

Where can I get it

You can download it with maven or directly from GitHub.

Category: open-source


  1. hassai
    Posted March 8, 2016 at 11:35 | Permalink

    thank you , i was searching for these examples of use.

  2. hassai
    Posted October 13, 2016 at 18:44 | Permalink

    hello ,
    i dont’t know how to add entry to existing group without overwrite old entries …


Post a Comment

Your email is kept private. Required fields are marked *