OpenSSLと証明書による暗号化、復号化
証明書、鍵はあらかじめ作っておく。
証明書から公開鍵を切り出すには、次のコマンドを実行する。
$ openssl x509 -in cert.pem -pubkey > pubkey.pem
- file_input
- 処理対象となる入力ファイル
- pubkey.prm
- 使用する公開鍵ファイル
- privkey.pem
- 使用する秘密鍵ファイル
- file_signed
- 暗号化されたデータ
- file_verify
- 復号化されたデータ
file_input:
$ hexdump -C file_input 00000000 54 68 69 73 20 69 73 20 61 20 74 65 73 74 20 69 |This is a test i| 00000010 6e 70 75 74 20 66 69 6c 65 2e |nput file.|
次のコマンドで、秘密鍵による暗号化が行われる。
$ openssl rsautl -in file_input -out file_signed -sign -inkey privkey.pem
出力結果は次のようになる。
$ hexdump -C file_signed 00000000 02 aa 01 11 91 9f fc 14 40 62 51 a1 e5 e0 02 6c |........@bQ....l| 00000010 f4 4e 54 7f 2d 7f 03 12 82 4c 01 f6 93 78 8c 7b |.NT.-....L...x.{| 00000020 2b ee 9e 6c 32 28 d3 63 0b b5 bd 6d 62 cc 41 4c |+..l2(.c...mb.AL| 00000030 93 24 8c 76 8d ea 99 ac f1 da 87 c0 5b 00 c9 86 |.$.v........[...| 00000040 5d 84 14 f0 8d 4d 00 dc 1e c7 15 3c bc d5 ea ea |]....M.....<....| 00000050 3a 32 de 32 33 74 cd ed 8c 7e cb 0c ac 79 fa 3f |:2.23t...~...y.?| 00000060 b6 66 f4 ce 2d 7e ab d9 3e 33 1e ff 41 2a 82 df |.f..-~..>3..A*..| 00000070 11 5d 11 59 14 f4 a8 ee bb 2b 13 4f 67 d2 be f7 |.].Y.....+.Og...|
次のコマンドで、公開鍵による復号化が行われる。
$ openssl rsautl -in file_signed -out file_verify -verify -pubin -inkey pubkey.pem
はい、元通り。
$ hexdump -C file_verify 00000000 54 68 69 73 20 69 73 20 61 20 74 65 73 74 20 69 |This is a test i| 00000010 6e 70 75 74 20 66 69 6c 65 2e |nput file.|