2013年1月19日土曜日

GnuPG での暗号化と復号化

GNU Privacy Guard (GnuPG) での暗号化と復号化の方法

まずは共通鍵暗号方式での暗号化
-c で共通鍵暗号を指定するが初期値は CAST5 となっている
CAST5 は CAST-128 とも呼ばれるブロック暗号
nビット安全性は AES128 と同様の 128bit となっている、はず
コマンドは以下の通りで、ここにパスフレーズを入力するとファイル名に .pgp がついたファイルが作成される
root@bt:~# gpg -c input_file.txt 
暗号化されたファイルの中身は下記のような状態
root@bt:~# hexdump -C input_file.txt
00000000  54 68 69 73 20 69 73 20  74 65 73 74 20 66 69 6c  |This is test fil|
00000010  65 0a 31 32 33 34 35 0a                           |e.12345.|
00000018
root@bt:~# hexdump -C input_file.txt.gpg 
00000000  8c 0d 04 03 03 02 83 a3  03 d6 a5 b5 44 65 60 c9  |............De`.|
00000010  38 4e 7a e1 5b 05 b3 0d  e6 4d ec 7c 6d 11 32 a2  |8Nz.[....M.|m.2.|
00000020  c8 27 0c 3c a9 cf be a2  9b 6c 83 6c 45 32 80 3c  |.'.<.....l.lE2.<|
00000030  92 fa c8 b5 a9 40 94 7d  07 d1 94 0d b2 e3 97 45  |.....@.}.......E|
00000040  ec 4c 63 42 d6 05 83 4d  78                       |.LcB...Mx|
00000049
root@bt:~# 
復号化するには下記のコマンド
root@bt:~# gpg input_file.txt.gpg 
次に公開鍵暗号方式での暗号化
その前に鍵を生成する
root@bt:~# gpg --gen-key
gpg (GnuPG) 1.4.10; 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) RSA and RSA (default)
   (2) DSA and Elgamal
   (3) DSA (sign only)
   (4) RSA (sign only)
Your selection? 
RSA keys may be between 1024 and 4096 bits long.
What keysize do you want? (2048) 
Requested keysize is 2048 bits
RSA の長さを選択
1024 だと 80ビット安全性で 2048 だと 112ビット安全性が確保できるらしいので特に理由がなければデフォルトそのままにしておく
Please specify how long the key should be valid.
         0 = key does not expire
        = key expires in n days
      w = key expires in n weeks
      m = key expires in n months
      y = key expires in n years
Key is valid for? (0) 
Key does not expire at all
Is this correct? (y/N) y
証明書の有効期限だが、検証用なので0を選択
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) "

Real name: BackTrack
Email address: BackTrack@local.com
Comment: 
You selected this USER-ID:
    "BackTrack "

Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? 0
Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? O
You need a Passphrase to protect your secret key.

gpg: gpg-agent is not available in this session
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: /root/.gnupg/trustdb.gpg: trustdb created
gpg: key BCE10D31 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:   1  signed:   0  trust: 0-, 0q, 0n, 0m, 0f, 1u
pub   2048R/BCE10D31 2013-01-19
      Key fingerprint = 4A65 8013 36B1 4B16 9070  FEB6 5DB9 15ED BCE1 0D31
uid                  BackTrack 
sub   2048R/BC2B0ED6 2013-01-19

root@bt:~# 
あとはパスフレーズと求められた情報を入力して作成完了
下記コマンドで作成された鍵のリストが確認できる
 
root@bt:~# gpg --list-keys
/root/.gnupg/pubring.gpg
------------------------
pub   2048R/BCE10D31 2013-01-19
uid                  BackTrack 
sub   2048R/BC2B0ED6 2013-01-19
実際にはホームディレクトリに.gnupgディレクトリが作成されて、そこに公開鍵と秘密鍵が格納されている
root@bt:~# ls -l /root/.gnupg/
total 32
-rw------- 1 root root 9364 2013-01-19 12:35 gpg.conf
-rw------- 1 root root 1190 2013-01-19 13:03 pubring.gpg
-rw------- 1 root root 1190 2013-01-19 13:03 pubring.gpg~
-rw------- 1 root root  600 2013-01-19 13:03 random_seed
-rw------- 1 root root 2568 2013-01-19 13:03 secring.gpg
-rw------- 1 root root 1280 2013-01-19 13:03 trustdb.gpg
root@bt:~# 
root@bt:~# 
他の公開鍵を取得した場合には下記のコマンドでインポートする必要がある
gpg --import FILENAME
鍵のペアをインポートするには以下のコマンド
gpg --import --allow-secret-key-import FILENAME
秘密鍵で暗号化するには -r で鍵を指定する必要がある
また -e を指定した場合にはファイル名に gpg がついたバイナリファイルが生成され、-aを指定した場合にはファイル名に asc がついたアスキーファイルが生成される
root@bt:~# gpg -e -r BackTrack@local.com -e input_file.txt 
root@bt:~# 
root@bt:~# hexdump -C input_file.txt.gpg 
00000000  85 01 0c 03 2e 43 89 fa  bc 2b 0e d6 01 07 ff 5b  |.....C...+.....[|
00000010  51 fa b9 9b b3 de 19 d0  0d 18 e6 fa 3f fd 19 f3  |Q...........?...|
00000020  dd a4 9d 97 12 b0 cb 81  51 32 ef 8f 48 e3 95 13  |........Q2..H...|
00000030  42 00 e0 1b c5 7b 48 17  95 07 38 ee a6 e7 4d d9  |B....{H...8...M.|
00000040  cb c8 9a 38 46 33 81 bb  b1 6c 1e 1a ba 0d 5e bc  |...8F3...l....^.|
00000050  19 7b 36 73 b4 cf 72 06  08 9b 2b 2c cf 71 4b d4  |.{6s..r...+,.qK.|
00000060  ee 00 8c fd f6 61 88 70  ac be dc 00 0b f7 11 4f  |.....a.p.......O|
00000070  76 e4 4f 25 59 f1 e0 f8  f7 d8 34 98 c6 f8 33 bd  |v.O%Y.....4...3.|
00000080  f0 92 62 65 82 f3 8f d3  10 51 b3 38 aa 41 a9 bf  |..be.....Q.8.A..|
00000090  81 85 ea 3e 77 4f 24 09  75 8a 40 e1 d1 37 80 87  |...>wO$.u.@..7..|
000000a0  8d 74 84 1a 59 1b d1 94  68 f1 c6 47 fd ed 46 ba  |.t..Y...h..G..F.|
000000b0  80 29 89 1f 69 95 c3 34  61 4a 76 cb c5 9c e4 71  |.)..i..4aJv....q|
000000c0  c8 87 c3 93 88 13 00 d9  d1 84 f4 6e d1 7c 87 36  |...........n.|.6|
000000d0  30 6e 6d ee cf 77 5c 93  c1 06 8e a3 bf 25 fd b4  |0nm..w\......%..|
000000e0  b9 b5 81 ac f9 b4 57 31  b3 3b 82 8f 46 90 ef 24  |......W1.;..F..$|
000000f0  2d 62 58 2a b8 14 08 6a  b1 af 1b 55 4f 54 63 05  |-bX*...j...UOTc.|
00000100  84 ef 8c 1d 33 37 e1 53  5a 4b 09 09 45 a3 df d2  |....37.SZK..E...|
00000110  5d 01 2e 71 61 09 f2 80  be 14 92 5f 56 2e c8 33  |]..qa......_V..3|
00000120  81 7e 0d 66 49 3f 93 25  bc cd 15 9d e8 9f 6d 87  |.~.fI?.%......m.|
00000130  d1 30 50 d8 9f 2b 04 82  d1 29 34 d1 6e 1d 72 64  |.0P..+...)4.n.rd|
00000140  67 f7 5e 3c a3 af 2d b3  06 c6 73 98 bb 0a 21 63  |g.^<..-...s...!c|
00000150  1f 84 ac 81 c4 70 79 ad  24 d0 fd 8c 21 2c 5a bb  |.....py.$...!,Z.|
00000160  1a 3b a5 ff c0 5d 88 cd  47 d4 22 24 88 5a        |.;...]..G."$.Z|
0000016e
root@bt:~# 
root@bt:~# gpg -a -r BackTrack@local.com -e input_file.txt 
root@bt:~# hexdump -C input_file.txt.asc 
00000000  2d 2d 2d 2d 2d 42 45 47  49 4e 20 50 47 50 20 4d  |-----BEGIN PGP M|
00000010  45 53 53 41 47 45 2d 2d  2d 2d 2d 0a 56 65 72 73  |ESSAGE-----.Vers|
00000020  69 6f 6e 3a 20 47 6e 75  50 47 20 76 31 2e 34 2e  |ion: GnuPG v1.4.|
00000030  31 30 20 28 47 4e 55 2f  4c 69 6e 75 78 29 0a 0a  |10 (GNU/Linux)..|
00000040  68 51 45 4d 41 79 35 44  69 66 71 38 4b 77 37 57  |hQEMAy5Difq8Kw7W|
00000050  41 51 66 2b 4b 76 70 52  52 4f 42 33 67 6a 79 5a  |AQf+KvpRROB3gjyZ|
00000060  4e 71 72 5a 61 68 43 70  54 48 6a 67 68 42 71 74  |NqrZahCpTHjghBqt|
00000070  33 4c 72 41 71 44 56 4b  35 71 67 68 51 4b 75 4a  |3LrAqDVK5qghQKuJ|
00000080  0a 38 55 4e 7a 4d 7a 4b  35 33 63 47 70 37 79 68  |.8UNzMzK53cGp7yh|
00000090  4d 2b 4c 6c 38 37 64 79  6c 51 4e 45 78 51 59 68  |M+Ll87dylQNExQYh|
000000a0  2f 56 4d 78 71 41 50 75  76 59 75 6a 49 55 37 6d  |/VMxqAPuvYujIU7m|
000000b0  46 4c 6d 73 2b 58 69 78  2b 68 4f 35 54 71 43 67  |FLms+Xix+hO5TqCg|
000000c0  55 0a 37 4e 34 31 76 54  47 49 47 34 73 42 50 67  |U.7N41vTGIG4sBPg|
000000d0  59 4e 38 79 6c 38 79 56  64 6e 33 42 6c 30 72 42  |YN8yl8yVdn3Bl0rB|
000000e0  65 6c 6c 6d 30 66 2f 43  33 6b 4f 52 5a 46 58 48  |ellm0f/C3kORZFXH|
000000f0  6b 30 7a 71 75 33 41 64  57 4e 45 6d 62 4a 55 4c  |k0zqu3AdWNEmbJUL|
00000100  42 5a 0a 2b 38 41 6c 6d  6e 6e 74 30 44 71 68 58  |BZ.+8Almnnt0DqhX|
00000110  2b 31 77 65 45 35 43 37  57 57 6d 36 46 2f 79 30  |+1weE5C7WWm6F/y0|
00000120  49 4a 54 55 42 58 49 50  54 77 49 39 6d 5a 75 51  |IJTUBXIPTwI9mZuQ|
00000130  7a 6a 2b 73 4f 69 4c 75  69 46 6e 38 41 4b 54 77  |zj+sOiLuiFn8AKTw|
00000140  6f 4d 73 0a 70 36 61 56  67 39 76 4d 30 69 53 6c  |oMs.p6aVg9vM0iSl|
00000150  71 34 6a 53 4a 55 45 48  36 6a 52 6a 39 70 48 65  |q4jSJUEH6jRj9pHe|
00000160  77 57 63 53 2b 77 50 45  58 69 74 2b 45 7a 43 52  |wWcS+wPEXit+EzCR|
00000170  64 32 54 6e 66 74 54 6e  6f 57 58 36 62 54 74 76  |d2TnftTnoWX6bTtv|
00000180  48 63 43 63 0a 39 49 44  6d 6c 58 6c 2b 48 72 78  |HcCc.9IDmlXl+Hrx|
00000190  59 49 46 66 55 43 42 66  5a 6c 41 73 75 41 6d 74  |YIFfUCBfZlAsuAmt|
000001a0  38 4e 78 4f 77 71 79 67  77 43 78 4d 45 6c 74 4a  |8NxOwqygwCxMEltJ|
000001b0  64 41 57 71 71 50 78 76  53 6f 65 39 77 67 52 41  |dAWqqPxvSoe9wgRA|
000001c0  6d 54 34 58 4a 0a 6f 58  67 32 2b 54 78 45 74 59  |mT4XJ.oXg2+TxEtY|
000001d0  2f 43 34 42 35 38 2b 48  7a 61 64 43 6a 5a 47 47  |/C4B58+HzadCjZGG|
000001e0  4d 4a 6f 50 47 6e 76 75  4a 71 36 46 6a 35 65 62  |MJoPGnvuJq6Fj5eb|
000001f0  37 50 55 66 34 75 4c 42  45 2f 52 46 34 44 55 78  |7PUf4uLBE/RF4DUx|
00000200  73 39 59 41 4f 44 0a 69  31 43 37 62 6b 4d 30 54  |s9YAOD.i1C7bkM0T|
00000210  77 66 66 2b 57 7a 64 39  76 50 75 33 78 49 6f 35  |wff+Wzd9vPu3xIo5|
00000220  73 32 71 57 45 58 6e 6b  2f 49 31 34 34 71 6c 0a  |s2qWEXnk/I144ql.|
00000230  3d 71 36 70 33 0a 2d 2d  2d 2d 2d 45 4e 44 20 50  |=q6p3.-----END P|
00000240  47 50 20 4d 45 53 53 41  47 45 2d 2d 2d 2d 2d 0a  |GP MESSAGE-----.|
00000250
root@bt:~# 
暗号化と復号化の流れは下記のとおり
root@bt:~# gpg -e -r BackTrack@local.com -e input_file2.txt 
root@bt:~# 
root@bt:~# 
root@bt:~# hexdump -C input_file2.txt.gpg 
00000000  85 01 0c 03 2e 43 89 fa  bc 2b 0e d6 01 07 ff 66  |.....C...+.....f|
00000010  f2 77 fa 14 08 16 65 8c  1a c8 87 40 34 ed 82 0f  |.w....e....@4...|
00000020  6b 75 ea 87 d6 99 7c e1  a0 fe 92 ef 0c 4a 97 6d  |ku....|......J.m|
00000030  91 1d f1 04 9c 98 58 98  a9 e0 f1 f1 67 16 79 f8  |......X.....g.y.|
00000040  90 c0 69 74 d2 4d c1 41  d7 4d 89 00 c9 ec e8 56  |..it.M.A.M.....V|
00000050  cd 26 86 13 3e 97 04 17  b8 a6 4d ea 4f 78 a7 df  |.&..>.....M.Ox..|
00000060  c8 61 11 4c 72 ab 55 32  23 57 34 74 16 59 30 7f  |.a.Lr.U2#W4t.Y0.|
00000070  de 25 4f 53 d0 d3 04 d0  99 b4 52 ac b6 84 6e 1d  |.%OS......R...n.|
00000080  bf ab a2 cb f4 67 4d 8f  7e bf 61 ac 6a 3d 4e e2  |.....gM.~.a.j=N.|
00000090  d2 2f cc f9 9e e9 d8 0a  57 74 bb e6 1b 49 39 25  |./......Wt...I9%|
000000a0  58 d2 e4 9c 39 a8 da 9c  f3 83 dc a0 de b3 89 2d  |X...9..........-|
000000b0  6c f3 68 a3 da 03 80 18  6b 79 34 50 d6 3d 24 73  |l.h.....ky4P.=$s|
000000c0  4a 3f c5 8b 64 3a c9 c3  38 bf db 5e 92 f0 5e 0e  |J?..d:..8..^..^.|
000000d0  7e e7 35 10 68 79 08 fe  fc 97 e8 34 ca 70 fc ba  |~.5.hy.....4.p..|
000000e0  03 f3 9c b7 f9 d8 8c 5f  40 c2 94 d8 08 15 72 a6  |......._@.....r.|
000000f0  ba 67 30 8b 14 df 91 7d  93 ca 38 24 43 05 41 18  |.g0....}..8$C.A.|
00000100  f4 80 6e a7 7f c5 ed 66  3a 7e 63 f0 01 49 cc d2  |..n....f:~c..I..|
00000110  5e 01 f4 2a 85 03 34 19  27 e6 76 50 c1 05 7f 32  |^..*..4.'.vP...2|
00000120  2b bd 08 e0 bd 6a 4e 3a  1d 05 4e 02 77 79 f4 eb  |+....jN:..N.wy..|
00000130  66 57 25 b0 53 16 a5 c3  0d 3e fb 36 7f 79 5a 80  |fW%.S....>.6.yZ.|
00000140  72 29 63 58 3a a5 f6 c0  0b f7 a3 84 bf c4 4d 34  |r)cX:.........M4|
00000150  63 1d 2c d2 93 f5 08 58  8b 3d ba 16 5a 6a 9b 87  |c.,....X.=..Zj..|
00000160  07 11 dd 85 0c 29 81 06  9a c6 5f 40 c0 dd 63     |.....)...._@..c|
0000016f
root@bt:~# 
root@bt:~# gpg -d input_file2.txt.gpg 

You need a passphrase to unlock the secret key for
user: "BackTrack "
2048-bit RSA key, ID BC2B0ED6, created 2013-01-19 (main key ID BCE10D31)

gpg: gpg-agent is not available in this session
gpg: encrypted with 2048-bit RSA key, ID BC2B0ED6, created 2013-01-19
      "BackTrack "
test case 2
just try
root@bt:~# 
暗号化されたファイルの先頭 8bit は 10 + type + パケット長依存となっているようなので、ある程度は先頭ビットで暗号の判断ができると思う
そのためには、暗号化されたファイルのパケットリストを表示させる
root@bt:~# gpg --list-packets input_file.txt.gpg 
:pubkey enc packet: version 3, algo 1, keyid 2E4389FABC2B0ED6
        data: [2047 bits]

You need a passphrase to unlock the secret key for
user: "BackTrack "
2048-bit RSA key, ID BC2B0ED6, created 2013-01-19 (main key ID BCE10D31)

gpg: gpg-agent is not available in this session
:encrypted data packet:
        length: 93
        mdc_method: 2
gpg: encrypted with 2048-bit RSA key, ID BC2B0ED6, created 2013-01-19
      "BackTrack "
:compressed packet: algo=2
:literal data packet:
        mode b (62), created 1358569386, name="input_file.txt",
        raw data: 24 bytes
root@bt:~# 
最初のパケットは pubkey enc packet となっている
ソースコードの定義だと1になっている
typedef enum 
  {
    PKT_NONE          = 0,
    PKT_PUBKEY_ENC    = 1,  /* Public key encrypted packet. */
    PKT_SIGNATURE     = 2,  /* Secret key encrypted packet. */
    PKT_SYMKEY_ENC    = 3,  /* Session key packet. */
    PKT_ONEPASS_SIG   = 4,  /* One pass sig packet. */
    PKT_SECRET_KEY    = 5,  /* Secret key. */
    PKT_PUBLIC_KEY    = 6,  /* Public key. */
    PKT_SECRET_SUBKEY = 7,  /* Secret subkey. */
    PKT_COMPRESSED    = 8,  /* Compressed data packet. */
    PKT_ENCRYPTED     = 9,  /* Conventional encrypted data. */
    PKT_MARKER        = 10, /* Marker packet. */
    PKT_PLAINTEXT     = 11, /* Literal data packet. */
    PKT_RING_TRUST    = 12, /* Keyring trust packet. */
    PKT_USER_ID       = 13, /* User id packet. */
    PKT_PUBLIC_SUBKEY = 14, /* Public subkey. */
    PKT_OLD_COMMENT   = 16, /* Comment packet from an OpenPGP draft. */
    PKT_ATTRIBUTE     = 17, /* PGP's attribute packet. */
    PKT_ENCRYPTED_MDC = 18, /* Integrity protected encrypted data. */
    PKT_MDC           = 19, /* Manipulation detection code packet. */
    PKT_COMMENT       = 61, /* new comment packet (GnuPG specific). */
    PKT_GPG_CONTROL   = 63  /* internal control packet (GnuPG specific). */
  } 
そのため、10+0001+xx となり、84,85,86 が先頭ビットの場合は公開鍵暗号で暗号化されたファイルの可能性が高い
共通鍵暗号方式の場合は symkey enc packet となっている
root@bt:~# gpg --list-packets input_file.txt.gpg 
:symkey enc packet: version 4, cipher 3, s2k 3, hash 2
        salt 6c8c20e893de5006, count 65536 (96)
gpg: CAST5 encrypted data
gpg: gpg-agent is not available in this session
:encrypted data packet:
        length: 56
gpg: encrypted with 1 passphrase
:compressed packet: algo=1
:literal data packet:
        mode b (62), created 1358577733, name="input_file.txt",
        raw data: 24 bytes
gpg: WARNING: message was not integrity protected
root@bt:~# 
そのため、10+0011+xx となり 8C,8D,8E から始まる場合は共通鍵暗号方式で暗号化されている可能性が高い

0 件のコメント:

コメントを投稿