opensslを使うと秘密鍵,公開鍵を作ってファイルを暗号化したり,復号化したりできる.
とても面白いのだけど,オプションが多くてややこしいのでメモしておく.
秘密鍵を作る
seckey.pemという名の秘密鍵を作る場合.
$ openssl genrsa -out seckey.pem -aes256 2048 # 秘密鍵のパスワードとパスワードの確認入力が求められます
公開鍵を作る
seckey.pemという名の秘密鍵を使って,pubkey.pemという名の公開鍵を作る場合.
$ openssl rsa -in seckey.pem -pubout -out pubkey.pem
ファイルを暗号化する
secret.txtを暗号化して,secret.txt.secretを作ってみます.
とりあえずsecret.txtを作ります.
$ cat > secret.txt <<EOS this is a secret text. EOS
続いて暗号化
$ openssl rsautl -pubin -inkey pubkey.pem -in secret.txt -encrypt -out secret.txt.secret
よくわからない内容のファイルができています.
$ cat secret.txt.secret K>Fp] 4{^ F'ݿqgs&@ߟ%[jÜ I50@1{._j?@w"BkAX+' 6>IgޓM=Fv^1v p+zUޟ/Nؽ{$QRІ+C 0>jB~*
ファイルを復号化する
seckey.pemを使ってsecret.txt.secretを復号化します.
cat secret.txt.secret | openssl
実行結果
$ cat secret.txt.secret | openssl rsautl -decrypt -inkey seckey.pem Enter pass phrase for seckey.pem: # 秘密鍵のパスワードを入力 this is a secret text.
割と簡単にできるんですね.