A framework provides transparent 256-bit AES full database encryption, additional information and documentation is available on the official SQLCipher site. Special thanks for Zetetic.
Swift 5 and beyond.
import SQLCipher
func main() {
var db: COpaquePointer = nil
let key = "key"
let sql = "CREATE TABLE test(id INTEGER, field1 TEXT, field2 TEXT)"
sqlite3_open_v2("db.sqlite3", &db, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE, nil)
sqlite3_key(db, key, Int32(key.utf8.count))
if sqlite3_exec(db, sql, nil, nil, nil) != SQLITE_OK {
print("Error on execute")
}
}
To encrypt a database programmatically you can use the sqlite3_key
function.
The data provided in pKey
is converted to an encryption key according to the
same rules as PRAGMA key
.
int sqlite3_key(sqlite3 *db, const void *pKey, int nKey)