SQLCipher is an SQLite extension that provides transparent 256-bit AES encryption of database files. Pages are encrypted before being written to disk and are decrypted when read back. Due to the small footprint and great performance it’s ideal for protecting embedded application databases and is well suited for mobile development.
The official SQLCipher software site is http://sqlcipher.net
SQLCipher was initially developed by Stephen Lombardo at Zetetic LLC (sjlombardo@zetetic.net) as the encrypted database layer for Strip, an iPhone data vault and password manager (http://getstrip.com).
- Fast performance with as little as 5-15% overhead for encryption on many operations
- 100% of data in the database file is encrypted
- Good security practices (CBC mode, key derivation)
- Zero-configuration and application level cryptography
- Algorithms provided by the peer reviewed OpenSSL crypto library.
- Configurable crypto providers
We welcome contributions, to contribute to SQLCipher, a contributor agreement needs to be submitted. All submissions should be based on the prerelease
branch.
Building SQLCipher is almost the same as compiling a regular version of SQLite with two small exceptions:
- You must define SQLITE_HAS_CODEC and SQLITE_TEMP_STORE=2 when building sqlcipher.
- You need to link against a OpenSSL's libcrypto
Example Static linking (replace /opt/local/lib with the path to libcrypto.a). Note in this example, --enable-tempstore=yes is setting SQLITE_TEMP_STORE=2 for the build.
$ ./configure --enable-tempstore=yes CFLAGS="-DSQLITE_HAS_CODEC" \
LDFLAGS="/opt/local/lib/libcrypto.a"
$ make
Example Dynamic linking
$ ./configure --enable-tempstore=yes CFLAGS="-DSQLITE_HAS_CODEC" \
LDFLAGS="-lcrypto"
$ make
To specify an encryption passphrase for the database via the SQL interface you use a pragma. The passphrase you enter is passed through PBKDF2 key derivation to obtain the encryption key for the database
PRAGMA key = 'passphrase';
Alternately, you can specify an exact byte sequence using a blob literal. If you use this method it is your responsibility to ensure that the data you provide a 64 character hex string, which will be converted directly to 32 bytes (256 bits) of key data without key derivation.
PRAGMA key = "x'2DD29CA851E7B56E4697B0E1F08507293D761A05CE4D1B628663F411A8086D99'";
To encrypt a database programatically 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);
PRAGMA key or sqlite3_key should be called as the first operation when a database is open.
< 8000 div class="markdown-heading" dir="auto">