3

I have a number of LiteDB database files. Some of them have password. when I open these files in the application, for those one which have password I will get the Invalid database password exception.

How I can check the database file before opened it to find if it has password protected or not? So I can show an input box to the user to get the password.

I'm using LiteDB in Windows WPF application (C#).

Thanks and sorry for my English. :)

Parham.D
  • 1,158
  • 2
  • 12
  • 31

2 Answers2

3

For LiteDB v5.0.x datafiles (more specifically, v8 datafile format), the first byte is used to indicate whether the file is encrypted: 0 means not encrypted, 1 means encrypted.

  • Thanks a lot - the code that was successful: bool IsDbPasswordProtected(string path) { using (FileStream fs = File.OpenRead(path)) { return fs.ReadByte() > 0; } } – Declan Taylor May 21 '20 at 16:01
2

there is no direct option to test if a datafile are encrypted or nor. But you can test some header bytes to check if datafile contains hash password (this works for v4).

You can open datafile and tests 20 bytes (start in position 67) - if all bytes are 0 means this datafile has no password.

mbdavid
  • 1,076
  • 7
  • 8
  • For files generated by version 5 of LiteDb - the 20 bytes - starting at position 67 - are non 0. However, I compared two files - one password-protected - the other not - and found the non-password-protected one had human readable text like 'This is a LiteDB file' which the other lacked. Would this be sufficient for determining the encryption? Thankyou – Declan Taylor May 19 '20 at 22:03