LoginSignup
14
14

More than 5 years have passed since last update.

Pretty Good Privacy[PGP]を使ってみる

Last updated at Posted at 2015-08-11

メールやファイルの暗号化で国際的に使われているのが「PGP」(Pretty Good Privacy)という暗号化プログラムです。
PGPが普及しないのは公開鍵の受け渡しがめんどくさい!からだと思います。

環境

ubuntu 14.04.1
VirtualBox-4.3.30-101610-Win

公開鍵と秘密鍵を作成

  • GnuPGを使用します。

VMやVirtualBox環境の場合、下記を実施する。

$ sudo aptitude install rng-tools
$ sudo rngd -r /dev/urandom

公開鍵と秘密鍵を作成(パスワードは復号化時に入力を求められるので、失念しないこと)

# gpg --gen-key                                                                                                                                     
gpg (GnuPG) 1.4.16; Copyright (C) 2013 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.
gpg: directory `/root/.gnupg' created
gpg: new configuration file `/root/.gnupg/gpg.conf' created
gpg: WARNING: options in `/root/.gnupg/gpg.conf' are not yet active during this run
gpg: keyring `/root/.gnupg/secring.gpg' created
gpg: keyring `/root/.gnupg/pubring.gpg' created
Please select what kind of key you want:
(1) RSA and RSA (default)
(2) DSA and Elgamal
(3) DSA (sign only)
(4) RSA (sign only)
Your selection? 1
RSA 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: hoge
Email address: hoge@hoge.ne.jp
Comment: 
You selected this USER-ID:
"hoge <hoge@hoge.ne.jp>"
Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? o
You need a Passphrase to protect your secret key.
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 --list-secret-keys
# gpg --list-public-keys

公開鍵のアスキーファイルを作成

# gpg -o hoge.asc -a --export <UID>

hoge.ascを公開する

ユーザAの公開鍵を公開鍵サーバに登録し、それをユーザBがインポートする

ユーザAの公開鍵を公開鍵サーバに公開

1.gpgコマンドを使って公開する。
userA@host:~# gpg --keyserver <公開鍵サーバ> --send-keys <Keyname>

公開鍵サーバ

2.JPINCが運用する公開鍵サーバをJPNICが用意しているGUIフロントエンドを使って登録してみる。
PGP公開鍵サーバー

  • アスキーファイル化した公開鍵を貼り付ける。

jpnic1.JPG

  • 登録された事を確認

jpnic2.JPG

  • 登録した公開鍵をUSERIDを指定して検索してみる

pgp0.png


ユーザAが公開した公開鍵をユーザBがインポートする

userB@host:~# gpg --recv-key F5DE1E41
gpg: 鍵F5DE1E41をhkpからサーバーkeys.gnupg.netに要求
gpg: /root/.gnupg/trustdb.gpg: 信用データベースができました
gpg: 鍵F5DE1E41: 公開鍵“hoge <hoge@hoge.ne.jp>”を読み込みました
gpg: 処理数の合計: 1
gpg:               読込み: 1  (RSA: 1)

ユーザBがユーザAが公開した公開鍵を使用して暗号化したデータをユーザAが復号化する

暗号化されたB.txt.ascを復号化するとBを.txtが生成される。

userA@host:~#gpg B.txt.asc
userA@host:~# cat B.txt
ユーザBが暗号したデータです
20150811
  • ユーザBの公開鍵を使用してユーザAが復号化する場合、上記対応を繰り返す。

ファイル暗号化・復号化

a.txtを暗号化するとa.txt.ascが生成される。

# gpg -ea a.txt
# cat a.txt.asc
-----BEGIN PGP MESSAGE-----
Version: GnuPG v1
hQEMA+fbOqc6srAgAQf+MMfoFfTRiaLMKibjMpgygrEXcnKAO5VXWMglIImdyxDQ
6MRKNCgxwnsHEpNceaSMgo3JrbXL+F/vNC1Bp+EF8m16GwyON15bDCsdFMKL7g04
F2uXEb9qZLCQ0IFVKoTpTyc3sQMAyLoHAcgKWuk4KFhQQWetIpRfIjzKwlrWhcad
2FFbxt+sxiIL2QDLw6YOqKaCslVChk/0Sf+U3or2ZqGmcnuQu7afJOYUEa5wddG+
x7+8LPbG2VCK0bxDtGKUczlXStDX8yj6dvytriTpHpxeInykYJSx/gAe0mxrHkU0
vk9S1Ukexawgil52bTWsDl871M9HOOn87b4lPJihOtJOAeFi2lTykJr0LVfNVKhx
+iSfZKyOui/KmuwinWtO/q/0zucTOJjxOXF0q8ICAwsSJXbCiS2UHR1/F9F2xGLc
VRxcWbIy/7j0DzlGxWQf
=zSVL
-----END PGP MESSAGE-----

a.txt.ascを復号化(鍵ペア作成時に指定したパスワードを入力)

# gpg a.txt.asc
You need a passphrase to unlock the secret key for
user: "hoge <hoge@hoge.ne.jp>"
2048-bit RSA key, ID 3AB2B020, created 2015-07-09 (main key ID F5DE1E41)
gpg: gpg-agent is not available in this session
Enter passphrase: 
gpg: encrypted with 2048-bit RSA key, ID 3AB2B020, created 2015-07-09
"hoge <hoge@hoge.ne.jp>"
# cat a.txt
test
201508011
14
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
14