page counter next up previous contents
Next: 搜尋公鑰並匯入 Up: GnuPG Previous: GnuPG 密鑰上傳   Contents   DYWANG_HOME

GnuPG 加密與解密

  1. 建立一個測試文件 demo.txt。
    [deyu1@kvm8 ~]$ echo 'abc123' > demo.txt
    [deyu1@kvm8 ~]$ cat demo.txt
    abc123
    
  2. 確認簽章
    [deyu1@kvm8 ~]$ gpg --check-sigs
    /home/deyu1/.gnupg/pubring.gpg
    ------------------------------
    pub   2048R/A98B198E 2014-05-26
    uid                  abc123 (ABC) <abc123@csie.cyut.edu.tw>
    sig!3        A98B198E 2014-05-26  abc123 (ABC) <abc123@csie.cyut.edu.tw>
    sub   2048R/C039E2B6 2014-05-26
    sig!         A98B198E 2014-05-26  abc123 (ABC) <abc123@csie.cyut.edu.tw>
    
  3. 使用 A98B198E 公鑰加密,也就是只有該員可以解密。原始文件 demo.txt,加密後文件 demo.en.txt。
    [deyu1@kvm8 ~]$ gpg --recipient A98B198E --output demo.en.txt --encrypt demo.txt
    
  4. 加密後文件 demo.en.txt 已不可讀。
    [deyu1@kvm8 ~]$ cat demo.en.txt
    ...........................................
    
  5. 解密使 demo.en.txt,存成 demo.de.txt,必須輸入 passpheasw。
    [deyu1@kvm8 ~]$ gpg --output demo.de.txt --decrypt demo.en.txt
    
    You need a passphrase to unlock the secret key for
    user: "abc123 (ABC) <abc123@csie.cyut.edu.tw>"
    2048-bit RSA key, ID C039E2B6, created 2014-05-26 (main key ID A98B198E)
    
    gpg: encrypted with 2048-bit RSA key, ID C039E2B6, created 2014-05-26
          "abc123 (ABC) <abc123@csie.cyut.edu.tw>"
    
  6. 列出解密結果,與原始檔案相同。
    [deyu1@kvm8 ~]$ cat demo.de.txt 
    abc123
    
  7. 直接解密 demo.en.txt,會在螢幕上列出解密的結果。
    [deyu1@kvm8 ~]$ gpg --decrypt demo.en.txt
    
    You need a passphrase to unlock the secret key for
    user: "abc123 (ABC) <abc123@csie.cyut.edu.tw>"
    2048-bit RSA key, ID C039E2B6, created 2014-05-26 (main key ID A98B198E)
    
    gpg: encrypted with 2048-bit RSA key, ID C039E2B6, created 2014-05-26
          "abc123 (ABC) <abc123@csie.cyut.edu.tw>"
    abc123
    



De-Yu Wang 2020-05-19