BLOGTIMES
::
cles::blog > 256-bit AES で暗号化された SQLite
2012/01/25

256-bit AES で暗号化された SQLite

 

SQLite について調べていたら、SQLCipher という 256-bit AES で暗号化する機能がついた SQLite を見つけたのでメモ。
パスフレーズをどうやって管理するのかという問題はありますが、データを透過的に暗号化できるのは便利そうです。

Home - SQLCipher - Open Source Full Database Encryption for SQLite

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.

以下、インストールメモ。

ビルドしてみる

基本的には README に書いてあるとおりですんなりビルド出来ます。Windows 版の prebuild が有料になっているようなので、Windows の場合は一筋縄ではいかないのかもしれませんが。

wget --no-check-certificate -O SQLCipher1.1.9.tar.gz https://github.com/sqlcipher/sqlcipher/tarball/v1.1.9
tar zxvf SQLCipher1.1.9.tar.gz
cd sqlcipher-sqlcipher-30c2f2f/
./configure --enable-tempstore=yes CFLAGS="-DSQLITE_HAS_CODEC" LDFLAGS="/usr/lib/libcrypto.a"
make

これでディレクトリに sqlite という SQLCipher のバイナリが生成されます。

動作テストしてみる

まず、 passphrase というパスワードでデータベースを初期化して、テーブルを作成し、データを入れておきます。

$ ./sqlite3 test.db
SQLite version 3.7.9 2011-11-01 00:52:41
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> PRAGMA key = 'passphrase';
sqlite> create table test ( a string, b string, c string );
sqlite> insert into test values ('aaa','bbb','ccc');
sqlite> select * from test;
aaa|bbb|ccc
sqlite> .q

パスワードを設定しないで、select するとエラーになります。

$ ./sqlite3 test.db
SQLite version 3.7.9 2011-11-01 00:52:41
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> select * from test;
Error: file is encrypted or is not a database
sqlite> .q

パスワードをきちんと設定してやれば、きちんと select できるようになります。

$ ./sqlite3 test.db
SQLite version 3.7.9 2011-11-01 00:52:41
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> PRAGMA key = 'passphrase';
sqlite> select * from test;
aaa|bbb|ccc
sqlite> .q

ちなみに PRAGMA は一番最初に必ず実行しなければならないようです。一度でも Error: file is encrypted or is not a database というエラーが出るようになってしまうと、データベースを閉じるまで PRAGMA を設定してもデータが取り出せませんでした。さらに気をつけなければ行けない点としては、コマンドラインから直接 PRAGMA を設定しまうとパスワードがヒストリーファイルに残ってしまうことでしょうか。運用上のポカミスになりやすいので、このあたりも気をつけなければいけません。

[5年前][4年前][3年前][2年前][1年前] | by hsur at 20:49, Category: Software | | Views: 1544
このエントリは役に立ちましたか?
     
トラックバックについて
Trackback URL:
お気軽にどうぞ。トラックバック前にポリシーをお読みください。[policy]
このエントリへのTrackbackにはこのURLが必要です→http://blog.cles.jp/item/4727
Trackbacks
このエントリにトラックバックはありません
Comments
愛のあるツッコミをお気軽にどうぞ。[policy]
古いエントリについてはコメント制御しているため、即時に反映されないことがあります。
コメントはありません
Comments Form

コメントは承認後の表示となります。
OpenIDでログインすると、即時に公開されます。

OpenID を使ってログインすることができます。

Identity URL: Yahoo! JAPAN IDでログイン

★下記に2つの英単語をスペースで区切って入力してください
::
cles::blog > 256-bit AES で暗号化された SQLite