GPG (GNU Privacy Guard) はフリーの暗号化ソフトです. 単にファイルを暗号化, 復号化出来るだけでなく, ファイルに電子署名をつけることもできます.
ここからの実習は二人一組になって行ってもらいます.
公開鍵と秘密鍵のペアを生成します(この作業は二人とも行う). 以下は, アカウント名 kintore が鍵を生成する例です.
まずは, gpg で鍵のペアを生成するためのコマンドを打ちます.
kintore$ gpg --gen-key |
鍵の設定に関して何項目か質問されるので, 本実習では以下のように設定しましょう.
gpg (GnuPG) 1.4.9; Copyright (C) 2008 Free Software Foundation, Inc. This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Please select what kind of key you want: (1) DSA and Elgamal (default) (2) DSA (sign only) (5) RSA (sign only) Your selection? 1 DSA keypair will have 1024 bits. ELG-E keys may be between 1024 and 4096 bits long. What keysize do you want? (2048) 2048 Requested keysize is 2048 bits Please specify how long the key should be valid. 0 = key does not expire <n> = key expires in n days <n>w = key expires in n weeks <n>m = key expires in n months <n>y = key expires in n years Key is valid for? (0) 0 Key does not expire at all Is this correct? (y/N) y You need a user ID to identify your key; the software constructs the user ID from the Real Name, Comment and Email Address in this form: "Heinrich Heine (Der Dichter) <heinrichh@duesseldorf.de>" Real name: Takeaki BABA Email address: kintore@gym.com Comment: test You selected this USER-ID: "Takeaki BABA (test) <kintore@gym.com>" Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? O You need a Passphrase to protect your secret key. Enter passphrase: (password) Repeat passphrase: (password) We need to generate a lot of random bytes. It is a good idea to perform some other action (type on the keyboard, move the mouse, utilize the disks) during the prime generation; this gives the random number generator a better chance to gain enough entropy. ...+++++++++++++++.++++++++++.+++++++++++++++.+++++++++++++++.++++++++++.. +++++..++++++++++.++++++++++++++++++++++++++++++.++++++++++++++++++++>..++ +++..+++++.............>.+++++......................<+++++...+++++ gpg: key 75DCB592 marked as ultimately trusted public and secret key created and signed. gpg: checking the trustdb gpg: 3 marginal(s) needed, 1 complete(s) needed, PGP trust model gpg: depth: 0 valid: 2 signed: 0 trust: 0-, 0q, 0n, 0m, 0f, 2u pub XXXXX/XXXXXXXX 2010-07-14 Key fingerprint = XXXX XXXX XXXX XXXX XXXX XXXX XXXX XXXX XXXX XXXX uid Takeaki BABA (test) <kintore@gym.com> sub XXXXX/XXXXXXXX 2010-07-14 |
--> 鍵の種類を選択 -->鍵のサイズを選択 -->鍵の有効期限を選択 |
今生成した公開鍵が自分の所有する公開鍵リストに登録されていることを確認しましょう.
kintore$ gpg --list-key ------------------------------------------------------------- pub XXXXX/XXXXXXXX 2010-07-14 uid Takeaki BABA (test) <kintore@gym.com> sub XXXXX/XXXXXXXX 2010-07-14 |
今生成した秘密鍵も自分の所有する秘密鍵リストに登録されていることを確認しましょう.
kintore$ gpg --list-secret-key ------------------------------------------------------------- sec XXXXX/XXXXXXXX 2010-07-14 uid Takeaki BABA (test) <kintore@gym.com> ssd XXXXX/XXXXXXXX 2010-07-14 |
これで公開鍵・秘密鍵の設定は終了です.ペアの受講生も同様にして 鍵を作成しましょう.以下では kintore さんの相方のアカウント名を dumbell として進めていきます.
dumbell$ gpg --gen-key "Kazuko SAITOU (test) <dumbell@gym.com>" |
お互いの公開鍵を登録します. 以下の例は dumbell さんが kintore さんの公開鍵を登録する手順です.
まず kintore さんが自分の公開鍵を kintore_pub-key.asc という名前のアスキー形式ファイルとしてエクスポートします.
kintore$ gpg -a --export kintore@gym.com > kintore_pub-key.asc |
次に dumbell さんが kintore_pub-key.asc をインポートします.
dumbell$ gpg --import /home/kintore/kintore_pub-key.asc |
正しくインポートされたか確認.
dumbell$ gpg --list-keys |
それでは, いよいよファイルを暗号化, 復号化する実習をしましょう. 以下の例では, dumbell さんが kintore さんの公開鍵で暗号化し, kintore さんが自分の秘密鍵で復号化する例です.
まず, dumbell さんが暗号化するためのテキストファイルを作成します.
dumbell$ echo "hello World" > hello.txt |
hello.txt を kintore さんの公開鍵で暗号化します.
dumbell$ gpg -a -r kintore@gym.com -e hello.txt |
hello.txt.asc というファイルが作成されているはずなので, 本当に暗号化されているか 中身を見てみましょう!
次に kintore さんが hello.txt.asc を自分のホームディレクトリにコピーする.
kintore$ cp /home/dumbell/hello.txt.asc . |
最後に kintore さんが hello.txt.asc を自身の秘密鍵で復号化する.
kintore$ gpg hello.txt.asc |
hello.txt が生成され, 中身がもとのものと一致すれば成功!
> 時間が余った人は [付録] へ最終更新日: 2010/07/15 馬場 健聡 | Copyright © 2010 inex |