Linux下使用GPG加密解密的說明及樣本

來源:互聯網
上載者:User

Linux下使用GPG加密解密的說明及樣本

1、產生金鑰組:gpg --gen-key

為使用者產生新金鑰組。需提供:密鑰類型(預設為RSA/RSA);密鑰長度(以位為單位,越長越強);到期時間(以防密鑰損壞);(通常我都是一路斷行符號過去)接下來的還是要填一填的:名稱、電子郵箱、標識密鑰所有者的注釋;密碼短語(必須提供,如果私密金鑰被盜,將無法使用)。

2、列出公開金鑰:gpg --list-keys

列出所擁有的公開金鑰:他們自己的公開金鑰以及從與之通訊的其他人那裡匯入的任何公開金鑰。

3、匯出公開金鑰:gpg --export --armor key-id -o file.key

將公開金鑰匯出至檔案,以便於其他人使用。--armor選項以文本形式顯示輸出,而非二進位格式。key-id是電子郵箱地址或在--list-keys的pub行中列出的八位十六進位數。

4、匯入公開金鑰:gpg --import file.key

從發送給您的密鑰檔案中匯入其他人的公開金鑰

5、加密檔案:gpg --encrypt --armor -r key-id file

用key-id的公開金鑰加密訊息。如果未提供-r key-id,命令將提示收件者輸入。預設輸出檔案為file.asc.

6、解密檔案:gpg --decrypt file

用您的私密金鑰之一解密用公開金鑰加密的訊息。

樣本:

建立屬於您自己的公開金鑰/ 私密金鑰對。

[sjx@server1 ~]$ gpg --gen-key

gpg (GnuPG) 2.0.14; Copyright (C) 2009 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?    Enter

RSA keys may be between 1024 and 4096 bits long.

What keysize do you want? (2048)    Enter

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)    Enter

Key does not expire at all

Is this correct? (y/N)  y

GnuPG needs to construct a user ID to identify your key.

Real name: shangjx

Email address: shangjx13@gmail.com

Comment: Enter

You selected this USER-ID:

"xiyou<sjx@server1.example.com>"

Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit?    o

You need a Passphrase to protect your secret key.

can't connect to `/home/bkjia/.gnupg/S.gpg-agent': No such file or directory

gpg-agent[2008]: directory `/home/bkjia/.gnupg/private-keys-v1.d' created

(此時會彈出圖形應用程式,輸入並驗證密鑰)

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.

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 CA83F5AF 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/CA83F5AF 2011-08-15

Key fingerprint = F886 17A2 F832 B545 6E27 B424 E539 26BF CA83 F5AF

uid

xiyou<sjx@server1.example.com>

sub 2048R/DB58BFCE 2011-08-15

若要匯出密鑰,請在上面的輸出中尋找密鑰ID 。可以在上面的pub 2048R/ 輸出的後面找到。在給樣本中

密鑰ID 是CA83F5AF 。以下樣本將顯示使用該密鑰ID 的命令。

●匯出您的公開金鑰,與夥伴共用。

[bkjia@server1 ~]$ gpg -a -o ~/pub.key --export CA83F5AF

●將匯出的公開金鑰複製到夥伴的server2 上。

[bkjia@server1 ~]$ scp pub.key server2.example.com:~

●匯入夥伴的公開金鑰。

[bkjia@server2 ~]$ gpg --import pub.key

gpg: directory `/home/bkjia/.gnupg' created

gpg: new configuration file `/home/bkjia/.gnupg/gpg.conf' created

gpg: WARNING: options in `/home/bkjia/.gnupg/gpg.conf' are not yet active during this run

gpg: keyring `/home/bkjia/.gnupg/secring.gpg' created

gpg: keyring `/home/bkjia/.gnupg/pubring.gpg' created

gpg: /home/bkjia/.gnupg/trustdb.gpg: trustdb created

gpg: key CA83F5AF: public key "westos <bkjia@server1.example.com>" imported

gpg: Total number processed: 1

gpg:

imported: 1 (RSA: 1)

●建立一個文字檔,內含供夥伴閱讀的訊息。

[bkjia@server2 ~]$ echo "xi'an university of posts and telecommunications" > encrypt.txt

●用夥伴的公開金鑰加密檔案。www.2cto.com

[bkjia@server2 ~]$ gpg --encrypt --armor -r CA83F5AF encrypt.txt

gpg: CA83F5AF: There is no assurance this key belongs to the named user

pub 2048R/CA83F5AF 2011-08-15 xiyou <sjx@server1.example.com>

Primary key fingerprint: 7F44 7AE0 A7C2 6E89 6C68 6FE2 5572 8249 3F7B CEB5

Subkey fingerprint: 8FCB BF3E 2D51 563F 1C3F 2440 FC81 0D73 A353 A3BF

It is NOT certain that the key belongs to the person named

in the user ID. If you *really* know what you are doing,

you may answer the next question with yes.

Use this key anyway? (y/N) y

●將加密檔案複製夥伴。

[bkjia@server2 ~]$ scp encrypt.txt.asc server1.example.com:~

●解密夥伴已加密的檔案並驗證您可以查看他們所發送的訊息。

[bkjia@server1 ~]$ gpg --decrypt encrypt.txt.asc

You need a passphrase to unlock the secret key for

user: "xiyou <sjx@localhost>"

2048-bit RSA key, ID DB58BFCE, created 2011-08-15 (main key ID CA83F5AF)

can't connect to `/home/bkjia/.gnupg/S.gpg-agent': No such file or directory

gpg: encrypted with 2048-bit RSA key, ID DB58BFCE, created 2011-08-15

"xiyou<sjx@server1.example.com>"

xi'an university of posts and telecommunications

openSUSE下玩轉GPG圖文詳解 

Linux下gpg的簡單應用 

GPG作者差點破產,危機已過 

本文永久更新連結地址:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.