Using pysqlcipher with SqlAlchemy?

I am trying to add code to my program to encrypt the sqlite database in use. I'm trying to prompt the user for a password and use that password to create a new encrypted database if it doesn't exist, or to decrypt and load an existing database. There doesn't seem to be a lot of documentation I could find and I don't know how to do this. My code:

if encryption is True:
   print("***PYPER TIMESHEET UTILITY***")
   print("\nEnter encryption password below:")
   key = getpass.getpass()
   DB_NAME = ".timesheet.db"
   engine = create_engine('sqlite:///{}'.format(DB_NAME), module=sqlite)

else:

   print("WARNING: Unencrypted session. Install pysqlcipher3 to enable encryption\n")
   DB_NAME = ".timesheet.db?cipher=aes-256-cfb&kdf_iter=64000"
   engine = create_engine('sqlite:///{}'.format(DB_NAME))
   DBSession = sessionmaker(bind=engine)
   session = DBSession()

      

EDIT: Forgot to give additional information.

I've tried what sqlalchemy says . In the above example, I realized I left an important line,

from pysqlcipher import dbapi 2 as sqlite

      

Link to full code

+3


source to share


1 answer


You forgot to include the key in your DB connection, as the example says:

'sqlite+pysqlcipher://:testing@/foo.db?cipher=aes-256-cfb&kdf_iter=64000'

      



(key in example "testing"

), so try this by getting key

:

engine = create_engine(
    'sqlite+pysqlcipher://:{0}@/{1}?'
    'cipher=aes-256-cfb&kdf_iter=64000'.format(key, DB_NAME))

      

+3


source







All Articles