Python3 conversion from pysqlcipher3 to sqlite3 module has error when attaching database

20 views Asked by At

Since pysqlicpher3 is not actively maintained I am switching to sqlite3.

The problem that I'm facing is in the following code when I'm trying to create a decrypted database from an encrypted one.

I'm using:

  • Python3.10
  • pysqlcipher3 version 1.2
input_db = sqlite.connect('input.db')


output = os.path.join('decrypted', 'output.db')

c = input_db.cursor()
c.execute("PRAGMA key = '" + key + "';")
c.execute("PRAGMA cipher_compatibility = 3")
c.execute("PRAGMA cipher_use_hmac = OFF;")
c.execute("PRAGMA kdf_iter = '4000';")

c.execute("ATTACH DATABASE '" + output + "' AS db KEY '';")
c.execute("SELECT sqlcipher_export('db');" )
c.execute("DETACH DATABASE db;" )
c.close()

The code works while using pysqlicipher3 but it fails when I switch to sqlite3 when executing the query to attach the output database. The error I'm getting is DatabaseError('file is not a database').

I've tried creating the database beforehand:

input_db = sqlite.connect('input.db')


output = os.path.join('decrypted', 'output.db')
with sqlite.connect(output):
    pass

c = input_db.cursor()
c.execute("PRAGMA key = '" + key + "';")
c.execute("PRAGMA cipher_compatibility = 3")
c.execute("PRAGMA cipher_use_hmac = OFF;")
c.execute("PRAGMA kdf_iter = '4000';")

c.execute("ATTACH DATABASE '" + output + "' AS db KEY '';")
c.execute("SELECT sqlcipher_export('db');" )
c.execute("DETACH DATABASE db;" )
c.close()

But I still get the same error, any ideas on how to make this work using sqlite3?

0

There are 0 answers