-
Hello, We use a NativeDatabase with drift ^1.4.0 and sqlcipher_flutter_libs ^0.5.0. Is it possible to encrypt an existing database, that was created without a key? When I try to add the key:
this error occurs the first time the database is accessed:
Is it also possible to change the key a database was encrypted with? Thank you |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
This helpful article was linked on the SQLCipher docs: https://discuss.zetetic.net/t/how-to-encrypt-a-plaintext-sqlite-database-to-use-sqlcipher-and-avoid-file-is-encrypted-or-is-not-a-database-errors/868 Basically, what you need to do is:
If you want to rename the file in the process (e.g. use import 'package:sqlite3/sqlite3.dart';
void _encryptExisting(String existingPath, String encryptedPath) {
sqlite3.open(existingPath)
// Note: This assumes that there's no potential for SQL injections in the
// path. Cautiously use prepared statements if needed ;)
..execute("ATTACH '$encryptedPath' AS encrypted KEY 'your_new_key';")
..select("SELECT sqlcipher_export('encrypted')")
..dispose();
}
|
Beta Was this translation helpful? Give feedback.
This helpful article was linked on the SQLCipher docs: https://discuss.zetetic.net/t/how-to-encrypt-a-plaintext-sqlite-database-to-use-sqlcipher-and-avoid-file-is-encrypted-or-is-not-a-database-errors/868
Basically, what you need to do is:
sqlcipher_export
function to write the plain-text database into an encrypted fileNativeDatabase
with the key pragma.If you want to rename the file in the process (e.g. use
original.db
for the existing plain-text db andoriginal.db.enc
for the new one), you can just check if theoriginal.db
file exists, perform the conversion in that case and finally delete it. You can put this logic in t…