【Openssl】OpenSSL ~ 重要データをやり取りする ~

■ はじめに

重要なファイルのやり取りをするのに、ZIPファイルの暗号化だけでは心許ない。
送信者・受信者どちらもOpensslを使えば、よりセキャアにやり取りできるので
その方法を記録しておく

目次

【1】OpenSSLがインストールされているか確認
【2】受信者側の作業
 1)秘密鍵を生成する
 2)公開鍵を生成する
 3)受信者は、公開鍵(2))をメールなどで送信者に渡す
【3】送信者側の作業
 1)公開鍵で送信対象を暗号化する
 2)送信者は、暗号化したファイル(1))をメールなどで受信者に渡す
【3】受信者側の作業
 1)暗号化されたファイルを秘密鍵で複合化する(元に戻す)
【4】トラブル:エラー「data too large for key size」が表示される

【1】OpenSSLがインストールされているか確認

openssl version

【2】受信者側の作業

1)秘密鍵を生成する

openssl genrsa 【鍵長(キーサイズ)】 > 【秘密鍵】

例:鍵長 = 2024バイトの場合

openssl genrsa 2024 > private-key.pem

Generating RSA private key, 2024 bit long modulus
...........+++
...........................+++
e is 65537 (0x10001)

2)公開鍵を生成する

openssl rsa -in 【秘密鍵】 -pubout -out 【公開鍵】

openssl rsa -in private-key.pem -pubout -out public-key.pem

writing RSA key

【3】送信者側の作業

1)公開鍵で送信対象を暗号化する

openssl rsautl -encrypt -pubin -inkey 【公開鍵】 -in 【送信対象】 -out 【公開鍵で暗号化した送信対象】

例:送信対象 = helloworld.txt

openssl rsautl -encrypt -pubin -inkey public-key.pem -in helloworld.txt -out encrypted-helloworld.out

# 確認
cat helloworld.txt

Hello World!!

# 確認
hexdump encrypted-helloworld.out

0000000 6a4c 023c dd7b 1801 5d1d b695 d1b2 4cc2
0000010 113b 195a 27ac 38c1 1f4a f22c a978 7ace
0000020 17e5 19b9 b494 77dd bef0 4338 722a 86f7
0000030 fbb1 bd36 60f4 6f76 50bc 6939 4568 b013
0000040 2b94 3d5a 7c37 fa52 7846 be88 186c 5f20
0000050 330e 4af9 3886 6d0d 6ffa 3f30 5b24 17b5
0000060 7ebf 74ba d1dd dbec 3bfe 3e00 8109 962d
0000070 29d7 5a1b ec1b 8f81 4b2e acf1 870d ae32
0000080 b1fa 2d4f 7952 ceab 6b9e 9550 d35e f684
0000090 8d14 582c 42f3 5d5a ba24 f904 758e c30f
00000a0 9c03 85d4 6b9a 0d7a 4bad 0bfd 51b0 e3fa
00000b0 e6e7 9b13 aa2a 2ea4 5ee9 e6b5 ef9a 2428
00000c0 3c2d 7057 87f6 4269 8e6a eff7 1c79 4314
00000d0 c4ff f8ca c166 043f 2ef7 4928 25c0 723c
00000e0 c280 7d58 1a19 1084 ced3 2ff3 033a ae1a
00000f0 d288 aa4e c1e9 0aba 298f 7d7b 00fa     
00000fd

【3】受信者側の作業

1)暗号化されたファイルを秘密鍵で複合化する

openssl rsautl -decrypt -inkey 【秘密鍵】 -in 【公開鍵で暗号化した送信対象】 > 【複合化したファイル】

openssl rsautl -decrypt -inkey private-key.pem -in encrypted-helloworld.out > decrypted-helloworld.txt

# 確認
cat decrypted-helloworld.txt

Hello World!!

【4】トラブル:エラー「data too large for key size」が表示される

1)エラー内容

$ openssl rsautl -encrypt -pubin -inkey public-key.pem -in bigdata.txt -out encrypted-helloworld.out

RSA operation error
140708605077392:error:0406D06E:rsa routines:RSA_padding_add_PKCS1_type_2:data too large for key size:rsa_pk1.c:153:

原因

鍵長以上の長さのデータを暗号化された場合、処理できなくなる

解決案

 * openssl smime コマンドを使って S/MIME形式にする

コマンド例

# 共通鍵と秘密鍵を生成
openssl req -x509 -nodes -newkey rsa:2048 -keyout private-key.pem -out public-key.pem -subj '/'

Generating a 2048 bit RSA private key
...................................+++
.........................................................................................+++
writing new private key to 'private-key.pem'
 ----- 

# 暗号化
openssl smime -encrypt -aes256 -in bigdata.txt -binary -outform DEM -out encrypted-bigdata.out public-key.pem

# 復号化
openssl smime -decrypt -in encrypted-bigdata.out -binary -inform DEM -inkey private-key.pem -out encrypted-bigdata.txt

参考文献

https://qiita.com/kunichiko/items/3c0b1a2915e9dacbd4c1
https://www.laddy.info/2014/02/27497/

関連記事

OpenSSL ~ Linux / 初期設定編 ~
https://dk521123.hatenablog.com/entry/2014/08/21/001440
OpenSSL ~ 証明書作成 ~
https://dk521123.hatenablog.com/entry/2016/11/12/011536
Windows上での Opensslの設定と自己証明書作成
https://dk521123.hatenablog.com/entry/2013/12/07/103903
Windowsで Openssl を使って認証局を立てる
https://dk521123.hatenablog.com/entry/2014/02/28/000100
ApacheLinux / SSL設定編 ~
https://dk521123.hatenablog.com/entry/2016/11/11/230045