Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLite Encryption differing between platforms

Tags:

java

c

c#

sqlite

I have built a couple of dlls from source. They are mainly from the same source, sqlite-3.6.22, Open SSL 0.9.8 and sqlcipher.

The reason I built two was that I needed one for working with a C++ project, and one as a JDBC driver for java (so a java jni dll).

Now I have the two DLLs, I can write and create databases in C++ and C# (using the C one), and the same in java. Both of these respond as I would expect, you can open them if you give the right password, it fails if you give the wrong one, etc. Both in theory should be the default for sqlcipher, which is hardcoded to aes-256-cbc. However, I can't open a database made with one with the other. I can open a database made with the c version in SQLite3 Management studio, but only if I put the password with RSA selected, I can't open the one created in java in anything else I have.

The other thing is that if I make two databases with identical contents with the c database, they are byte for byte identical, whereas the java ones aren't.

I am sure I am doing something obvious wrong, but I really can't see it - as far as I can see the java one is working fine (easier to debug for some reason).

like image 228
Woody Avatar asked Nov 15 '22 04:11

Woody


1 Answers

After leaving this a while, as it was quite frustrating, and got back to it today. Debugging the sqlite3.dll gives me the answer.

If you are accessing it via JDBC in Java, you don't have the key functions for a byte[] password, so need to enter the password via PRAGMA key='' in a statement. It seems from the documentation that these are equivalent and that if you need a binary key, you hex encode it and put "x'" in front. So in java I had:

PRAGMA key="x'899e7c6475756d887a759a93a0a3979a62827e85919898a49394a29c7b88aa8a"

whereas in C# I was using those binary digits in a byte array.

Turns out that the string is being used as a password literally, a 42 character password.

So I can make a database in java and read it in C# and vice versa, using the pragma or key methods, as long as I don't want a binary password, which I can't use in java.

Guess which method I needed!

like image 50
Woody Avatar answered Dec 09 '22 03:12

Woody