Reputation: 21
I'm trying to encrypt an SQLite3 database using pysqlsimplecipher. When I try to encrypt it, it gives an error:
raise RuntimeError('needs reserved space at the end of each page.')
RuntimeError: needs reserved space at the end of each page.
I Don't know how to do that. I'm using DB browser for sqlite. Is there any SQL statement I should execute, any setting i should change in DB browser or any python function from its SQLite module to reserve space at the end of each page? And how much of space would be enough?
Or any workaround? Its a part of larger program so the constraint here is that I should use python in order to encrypt. However,I can use any method to reserve space as it had to be done only once.
My platform is windows 8.1 and python version is 3.6 x32. If that matters.
I'm asking about a pythonic way or an SQL statement to do it. I've found an answer using c++ already but that's in c++. I don't know much about c++ I've read that you've to compile sqlite in order to use with c++. I don't know about it. I would appreciate if someone posts the entire code snippet to do it using c++.
Upvotes: 1
Views: 365
Reputation: 21
So after days of research and trying tons of methods. I was successful in doing it. The method sqlite3_test_control(SQLITE_TESTCTRL_RESERVE, sqlite3 *db, int N) does not work any more. So I Used a HEX editor to change the specific byte used for indicating how much of space is to be reserved at the end of each page in a sqlite Database. Here Goes:
1- Create an empty database: do not create any table or anything yet. create and save an sqlite database file.
2- Use a Hex editor to change the byte at offset 20. It will be 00 Which means no bytes are reserved. So for instance ,if you want to reserve 36 bytes. change the value of byte at offset 20 to '24' (36 when converted to HEX).
3-check the bytes at offset 105 and 106. Their value indicate the starting position of content area. if reserved space is 0 The value will be page size. For example in this case its 0400 which is 1024 in decimal. Now subtract the bytes you previously set to reserve; 36. 1024-36 =988. In HEX its 03DC. Set the value at 105 and 106 offset to 03DC. Save It and Now you are good to go. Check the image i've underlined the concerned bytes and settings of editor.
I needed to use pysqlsimplecipher for encryption and decryption of database which requires some bytes to be reserved at the end of each page. Hope it helps someone.
Upvotes: 1
Reputation: 180030
The documentation you linked to says:
Needs reserved space at the end of each page of the database file.
Otherwise, use sqlcipher to encrypt.
Encrypt with sqlcipher
Open plain db
./sqlcipher plain.db
Encrypt to enc.db
ATTACH DATABASE 'enc.db' as encrypted key 'testkey'; SELECT sqlcipher_export('encrypted'); DETACH DATABASE encrypted;
Upvotes: 1