resilar / sqleet

SQLite3 encryption that sucks less
The Unlicense
375 stars 55 forks source link

Anyway to fix broken encrypted database? #44

Closed almas closed 3 years ago

almas commented 3 years ago

I used a better-sqlite3-sqleet node package for encrypt database. It's working well. But sometimes some of my user's DB is unexpectedly broken. Is there anyway to fix a sqleet encrypted broken DB?

I could open unbroken DB with same key. (I changed some part of the key for security below)

./sqleet.exe ./my.db
SQLite version 3.31.1 2020-01-27 19:55:54
Enter ".help" for usage hints.
sqlite> PRAGMA key='AcQTjWnZy/B?E(hYq3t6waaaaABAH+AbPeS9z$C&F)J@r4uPdSgU7x!XP*G-';
ok
sqlite> SELECT * FROM config;
1|1|1|||0|0||1

But broken database can not open:

./sqleet.exe ./my-2.db
SQLite version 3.31.1 2020-01-27 19:55:54
Enter ".help" for usage hints.
sqlite> PRAGMA key='AcQTjWnZy/B?E(hYq3t6waaaaABAH+AbPeS9z$C&F)J@r4uPdSgU7x!XP*G-';
sqlite> SELECT * FROM config;
Error: file is not a database
sqlite> .quit

./hexdump.exe -C ./my-2.db
000000  47 ec ee 33 4c cf e5 c1 df fe fe a4 cf 20 f1 eb  G..3L........ ..
000010  00 01 3f 78 9d ac 3b ba 0b d6 fe cb 66 31 a2 8c  ..?x..;.....f1..
000020  51 57 51 f3 93 8e 0d e3 bf d4 c7 4c db 28 6a ae  QWQ........L.(j.
000030  16 34 f3 98 20 fc d6 02 a8 e5 b9 59 23 9c 11 ac  .4.. ......Y#...
000040  e3 c4 de 2c 45 a0 e7 45 0c d1 30 ad 85 eb 9b f4  ...,E..E..0.....
000050  1d 0d 19 d8 de d6 22 30 52 cf 71 dd 13 f4 eb b0  ......"0R.q.....
000060  ce 3f 86 25 49 ff 6c 4a cb 6a aa dd 4c 6f 94 95  .?.%I.lJ.j..Lo..
000070  b0 70 bf 83 71 2d ac 02 53 00 08 55 6c 3f 4a 11  .p..q-..S..Ul?J.
000080  08 64 95 f2 36 e9 16 8e 4e 45 74 91 10 bb 9f a2  .d..6...NEt.....
000090  0f 78 dd b2 34 39 53 16 95 39 90 c3 9b 3a e6 e1  .x..49S..9...:..
0000a0  88 94 c4 1b f4 8f 80 6a 2c 68 c7 93 45 5b 47 09  .......j,h..E[G.
0000b0  30 cf cd 3f 1d 0a 5c 2b 26 6b 28 17 aa 7c 55 a5  0..?..\+&k(..|U.
0000c0  1e 27 05 e2 ff 1e f7 0b c9 95 81 be f0 1c b1 74  .'.............t
0000d0  2c af 70 69 73 38 96 e9 d1 f4 98 ac 5b f7 c3 26  ,.pis8......[..&
0000e0  97 46 7d 37 1c be 42 fa 4c 0a 37 2c 3e b0 99 83  .F}7..B.L.7,>...
0000f0  71 a6 45 9d bd 7f 74 82 1f ef e9 c3 95 a1 91 b0  q.E...t.........
000100  0d 8b 58 97 8e 79 93 4b eb b8 4b 98 55 88 d0 74  ..X..y.K..K.U..t
000110  cd 9e a6 71 c4 c7 fa 6f 18 f4 41 d9 06 39 52 ed  ...q...o..A..9R.
000120  52 75 38 00 9b d2 cb 02 51 a8 25 93 4c 00 f7 25  Ru8.....Q.%.L..%
000130  8b d0 04 77 57 37 c0 50 42 96 a9 67 cd 1b 19 9e  ...wW7.PB..g....
000140  2c 24 48 06 2c b3 19 d5 80 b9 15 79 65 8f 54 71  ,$H.,......ye.Tq
000150  08 93 57 5a f2 6c 4c 46 7f 3e 05 4e 32 0a 7a 32  ..WZ.lLF.>.N2.z2
000160  f9 e5 20 f7 57 9b 25 78 61 0b 25 ec 7c f6 32 58  .. .W.%xa.%.|.2X
000170  0d 13 53 24 a5 d3 d1 c6 42 93 45 7e 28 8b 91 a1  ..S$....B.E~(...
000180  00 c6 16 0f c5 46 d7 63 f4 1d 03 98 b9 17 7a a5  .....F.c......z.
000190  ed 3b c8 fc ef cc 3a 4c d1 13 64 28 37 52 d7 01  .;....:L..d(7R..
0001a0  02 1e 84 79 4e 2d 37 39 35 41 06 fd 5b 9c 80 ee  ...yN-795A..[...
0001b0  76 2c 1a c3 03 f8 51 12 45 7b 5c 1a d9 93 e8 cd  v,....Q.E{\.....
0001c0  ec 6a 39 e3 4f f1 57 7b 06 2c a6 0b 10 b9 d3 22  .j9.O.W{.,....."
0001d0  48 b1 09 28 3c 88 85 5f 86 ca 95 c2 b6 c9 6d 15  H..(<.._......m.
0001e0  f5 fb 08 f1 db 6c d9 9f 2e 0a b5 01 1b d4 5e ab  .....l........^.
0001f0  4a e5 18 f1 69 25 25 05 c0 f0 c7 04 fd a5 2e d5  J...i%%.........
000200  ee e7 c6 52 0d 3e 26 2b a3 42 2d ca e3 c6 fb 7c  ...R.>&+.B-....|
000210  b0 c5 02 85 d2 67 c5 28 d9 11 6d dd 19 7f 83 f8  .....g.(..m.....
000220  aa 15 2d 4e a2 72 59 11 85 a2 d1 ed 80 0a 49 7e  ..-N.rY.......I~
000230  05 54 c0 50 98 3c 69 cc b9 5b c7 5c 5b dc 59 e6  .T.P.<i..[.\[.Y.
000240  ac f4 09 23 b7 4d dd 6c a7 a0 3e 56 fb 43 88 e1  ...#.M.l..>V.C..
000250  53 b6 06 a5 06 aa 2e 36 33 9d fc 2c bd 55 e4 8a  S......63..,.U..
000260  ac 23 4d 99 04 c4 8d bd f6 fc 14 3a 44 d4 8a 10  .#M........:D...
000270  7a d5 83 19 87 1b e1 55 9f ec 13 2a 1a 71 6d 4b  z......U...*.qmK
000280  68 d2 74 9e 59 7b 9b fd 0c 1f 90 ed 20 80 5f 2d  h.t.Y{...... ._-
...

Didn't run rekey again. Then it is not a key problem. After some insert query DB seems to broke. Please help me.

denwist commented 3 years ago

Try setting page_size before key / rekey. There was a similar problem when, when reaching a certain size, the database became broken. https://github.com/resilar/sqleet/issues/42

almas commented 3 years ago

Try setting page_size before key / rekey. There was a similar problem when, when reaching a certain size, the database became broken.

42

Thank you very much. I will try it.