jorabin / KeePassJava2

Java API for KeePass Password Databases - Read/Write 2.x (File versions 3 and 4), Read 1.x
Apache License 2.0
251 stars 70 forks source link

Open database only with key file #6

Closed pablobmnobrega closed 8 years ago

pablobmnobrega commented 8 years ago

Hi. I don't have password to open my database, so how I make this operation with only the key file?

In the code below, I get NullPointerException.

InputStream inputStreamDB = new FileInputStream("C:\TESTE.kdbx"); InputStream inputStreamKeyFile = new FileInputStream("C:\Teste.key"); Credentials credentials = new KdbxCredentials.KeyFile(null, inputStreamKeyFile); Database database = DomDatabaseWrapper.load(credentials, inputStreamDB);

jorabin commented 8 years ago

If you mean that there is no master password, instead of

new KdbxCredentials.KeyFile(null, inputStreamKeyFile);

put

new KdbxCredentials.KeyFile(new byte[0], inputStreamKeyFile);

I will put @NotNull annotations and add some Javadoc.

pablobmnobrega commented 8 years ago

Hi, Jorabin. Yes, I mean Master Password.

I modified the code and the error now is 'Exception in thread "main" java.lang.IllegalStateException: Inconsistent stream bytes.'

Credentials credentials = new KdbxCredentials.KeyFile(new byte[0], inputStreamKey);

jorabin commented 8 years ago

I think that would be because the key file doesn't belong to the database or that the database does in fact have a password after all. If you look at the example at https://github.com/jorabin/KeePassJava2/blob/master/src/test/java/org/linguafranca/pwdb/kdbx/KdbxKeyFileTest.java and look at the test testNoPasswordLoad() you'll see that a KDBX database and key file with no master password loads correctly ...

pablobmnobrega commented 8 years ago

Hi Jorabin. The key file belongs to the database and it doesn't have a password (see the pictures).

I downloaded your project, imported in my Eclipse, and when I run the Maven -> Install, the error happens too.

Tests run: 2, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.022 sec <<< FAILURE! - in org.linguafranca.pwdb.kdbx.KdbxKeyFileTest testNoPasswordLoad(org.linguafranca.pwdb.kdbx.KdbxKeyFileTest) Time elapsed: 0.017 sec <<< ERROR! java.lang.IllegalStateException: Inconsistent stream bytes at org.linguafranca.pwdb.kdbx.KdbxKeyFileTest.testNoPasswordLoad(KdbxKeyFileTest.java:46)

Could it be a problem with my environment? I'm running the project in a machine with Windows 7 64 bits, Eclipse Mars 2 64 bits and JDK 7 64 bits.

capturar 1 capturar 2

jorabin commented 8 years ago

Oh dear, that does seem a problem. I am reopening this Issue.

I have built it in both Java 8 and Java 7 and it works fine on OS X.

1) can you please run mvn clean install from to command line 2) Can you please submit a pull request with the .kdbx file and the .key file in the test resources directory (alongside the other test databases) 3) Can you please test a file with a password 4) I don't use eclipse so can't comment on that. But here is my Maven and Java info:

$ mvn -v
Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-10T16:41:47+00:00)
Maven home: /usr/local/Cellar/maven/3.3.9/libexec
Java version: 1.7.0_72, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.7.0_72.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.11.6", arch: "x86_64", family: "mac"

$ java -version
java version "1.7.0_72"
Java(TM) SE Runtime Environment (build 1.7.0_72-b14)
Java HotSpot(TM) 64-Bit Server VM (build 24.72-b04, mixed mode)
pablobmnobrega commented 8 years ago

Hi Jorabin. I have no problem testing databases with password.

I made the pull request. Thank you for the interest,

jorabin commented 8 years ago

Hmmm. That's interesting. So here is what I have found.

At lines 53 and 73 of KdbxCredentials a second digest is taken of the password. If I remove line 53 for the files you sent over they work fine (in KdbxKeyFileTest#testWindowsNoPasswordLoad) but KdbxKeyFileTest#testNoPasswordLoad now fails. The files the latter uses were created using KeepassX.

So the way it looks to me, is that in the original (Windows) code a digest needs to be taken twice if the password is non-empty and once if it is empty. Odd, but that's the way it is. Presumably we can assume that the original Keepass is "correct" and KeepassX is wrong.

Probably an idea to look at the Keepass C source code on this, and seeing whether that analysis is correct. It's getting a bit late here in London, so I'm not going to do that - at least not today!

I will push the amended tests etc to Github. Perhaps you'll try commenting out and in the lines I reference above and see if it works for you on Windows?

[I suppose I could wonder why there is no master password ... but that's not the point. Is it :-)]

pablobmnobrega commented 8 years ago

Jorabin, this is a strange behavior. But why my tests have failed on Windows with the embedded key file (I suppose it was created on OS X)? Anyway, I can test in Windows. It will be a pleasure.

Em Quinta-feira, 8 de Setembro de 2016 16:10, Jo <notifications@github.com> escreveu:

Hmmm. That's interesting. So here is what I have found.At lines 53 and 73 of KdbxCredentials a second digest is taken of the password. If I remove line 53 for the files you sent over they work fine (in KdbxKeyFileTest#testWindowsNoPasswordLoad) but KdbxKeyFileTest#testNoPasswordLoad now fails. The files the latter uses were created using KeepassX.So the way it looks to me, is that in the original (Windows) code a digest needs to be taken twice if the password is non-empty and once if it is empty. Odd, but that's the way it is. Presumably we can assume that the original Keepass is "correct" and KeepassX is wrong.Probably an idea to look at the Keepass C source code on this, and seeing whether that analysis is correct. It's getting a bit late here in London, so I'm not going to do that - at least not today!I will push the amended tests etc to Github. Perhaps you'll try commenting out and in the lines I reference above and see if it works for you on Windows?[I suppose I could wonder why there is no master password ... but that's not the point. Is it :-)]— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

pablobmnobrega commented 8 years ago

Jorabin, I commented the line 53 in KdbxCredentials and the test is OK.

jorabin commented 8 years ago

Hi Pablo.

Here's the actual answer.

You have a file with "no master password" which is different to "empty master password". Silly me for not realising. I've now added a method and a test for KdbxCredentials with no password.

I have renamed the test files to make it clearer what they test. The one created by KeepassX has an empty password and it is correct. I installed a Windows 10 VM and tested loading it into Keepass Windows.

I also rebuilt the project and ran all the tests under Windows, and everything seems to pass under both Java 7 and Java 8.

It would be nice if you were able to update your copy of the repo and verify that all is now well. If so please close the issue.

Thanks! Jo

mhagnumdw commented 8 years ago

Jorabin, in Windows 7 with JDK 1.7 and 1.8 the case of Pablo worked fine.

pablobmnobrega commented 8 years ago

Jorabin, thanks again by the interest. According to my friend @mhagnumdw, the test was succesful. I will close the issue.