How to query sqlcipher encrypted databases with Ruby
Last year I added support for building the sqlite3
ruby gem against sqlcipher allowing ruby apps to access and query sqlcipher
encrypted databases.
SQLCipher is simply a fork of sqlite, kept up-to-date with upstream, that provides 256 bit AES encrypted sqlite databases. It’s used heavily in mobile apps which was part of the inspiration for adding support as I wanted to be able to query my password manager Enpass which was the catalyst for adding sqlcipher support to the sqlite3
ruby gem.
Example usage (macOS)
$ brew install sqlcipher
$ gem install sqlite3 -- --with-sqlcipher
$ sqlcipher
sqlite> .open encrypted.db
sqlite> PRAGMA key='password';
sqlite> create table people (name text primary key);
sqlite> insert into people (name) values ('ace');
sqlite> .quit
Then accessing is as easy as passing in the key.
example.rb
require 'sqlite3'
db = SQLite3::Database.new 'encrypted.db'
db.execute "PRAGMA key='password';"
people = db.execute "SELECT * FROM people;"
puts "People: #{people}"
$ ruby example.rb
People: ace