OpenSSLを使って、ファイルを暗号化する方法のメモ。これで、OSに関わらず安全にデータを保管できるかな?
以下は、要約。
Cygwin や linux で、OpenSSL をインストール。
test.txt を暗号化して test.txt.bin に保存するには、以下のように記述
$ openssl enc -e -aes-128-cbc -in test.txt -out test.txt.bin
コマンド上でパスワードを入力します。
これを複合化して、out.txt に出力するには、次のように記述。
$ openssl enc -d -aes-128-cbc -in test.txt.bin > out.txt
しかし、せっかくなら、公開鍵暗号方式を利用したい。
(1) はじめに[秘密鍵]を生成
$ openssl genrsa -des3 -out private.pem
(2) 作成した[秘密鍵]と対となる[公開鍵]を生成
$ openssl rsa -pubout -in private.pem -out public.pem
(3) 公開鍵を使って、ファイルを暗号化
$ openssl rsautl -encrypt -pubin -inkey public.pem -in test.txt -out test.txt.bin
(4) 復号化する場合
out.txt に複合化した結果を保存する場合
$ openssl rsautl -decrypt -inkey private.pem -in test.txt.bin > out.txt
コマンドラインでパスワード hoge を指定する場合:
$ openssl rsautl -decrypt -inkey private.pem -in test.txt.bin -passin pass:hoge > out.txt