KeePass is a well known password safe for your passwords. It has a lot of features, proved security and is really a good way of storing your passwords and login information. Personally, I’m using KeePass for a long time now.
Java API for KeePass
However, sometimes there is a need that you have to access the KeePass database programmatically. And there comes the problem. There are really good frameworks available for C# but if you’ve ever looked for an opensource java library that is capable of reading KeePass 2.x databases you were probably surprised that there are only libraries out there that can read KeePass 1.x databases.
I guess this has to do with the fact that the file format of KeePass 2.x has dramatically changed since the old 1.x version and is now based on XML. However, since there is no alternative available I’ve managed to write my own java API for KeePass 2.x files.
Openkeepass for reading KeePass files
With the library you will get a quick way to access the database files. I’ve tried to make the API as simple as possible to use.
If you want to read all password entries from a KeePass database you can achieve that with the following code:
KeePassFile database = KeePassDatabase.getInstance("Database.kdbx").openDatabase("MasterPassword");
// Retrieve all entries
List<Entry> entries = database.getEntries();
If you want to search for a specific entry you can do that as well:
Entry sampleEntry = database.getEntryByTitle("Sample Entry");
Looking for entries that contain a specific string? No problem:
List<Entry> entries = keePassFile.getEntriesByTitle("entry", false);
You could also work with groups:
List<Group> groups = keePassFile.getTopGroups();
There are more examples available on GitHub.
If you want to use openkeepass, you can grab it directly from GitHub or even simpler just add it as a maven dependency.
There is one pitfall that you could fall into while using the library. While KeePass is using strong cryptography, you have to make sure that you’ve installed the Java Cryptography Extension (JCE) on your system. You can download it directly from Oracle.
If you have not installed it, you will run into an InvalidKeyException:
As always the whole source code is opensource and available on GitHub.