Author Topic: ทำ อย่างไร transfer file ด้วย gpg [GNU Privacy guard] private key , public key  (Read 7950 times)

golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2140
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
ปกติแล้ว การถ่ายโอนไฟล์ในระบบ อินเตอร์เนตเพื่อให้มีความปลอดภัย ของไฟล์ว่าไม่ถูกเปิดอ่านโดย บุคคลอื่น หรือถูกแก้ไข ระหว่างทางนั้น
การเข้ารหัสไฟล์ด้วยวิธีที่เรียกว่า gpg [GNU Privacy guard] ก็เป็นวิธีหนึ่งที่ได้รับความนิยม เหมือนกัน

โดยอาศัยหลักการของ
- Private key(เก็บไว้ที่เครื่องต้นทาง ห้ามเปิดเผยกับใคร) และ
- public key (ไฟล์ที่สามารถแชร์ไปยังบุคคลอื่นได้ )

โดนโอนไปให้ผู้ซึ่งมองง่ายๆ คือเวลาจะส่งไฟล์ ไปหาอีกคนหนึ่ง ซึ่งในที่นี่ยกตัวอย่างได้เช่น golf จะส่งไฟล์ text.txt หา mook
golf และ mook จะต้องทำการสร้าง private key ของตัวเองขึ้นมา และสร้าง public key จากที่เครื่องที่มีตัวโปรแกรม gpg

mook#gpg --gen-key

gpg (GnuPG) 1.4.5; Copyright (C) 2006 Free Software Foundation, Inc.
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions. See the file COPYING for details.

Please select what kind of key you want:
   (1) DSA and Elgamal (default)
   (2) DSA (sign only)
   (5) RSA (sign only)
Your selection?
1 (แล้วกด Enter เพื่อใช้ DSA+Elgamal )

DSA keypair will have 1024 bits.
ELG-E keys may be between 1024 and 4096 bits long.
What keysize do you want? (2048)
(กด Enter เพื่อใช้ keysize = 2048 bit)

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)
(ต้องการให้ key มีอายุการใช้งานหรือเปล่า ถ้าใส่ 0 ก็ไม่มี expire )

Key does not expire at all
Is this correct? (y/N)
(กด y ถ้าข้อมูลถูกต้อง)


หลังจากนั้นก็จะมีการ generate public key ให้ครับผม

ขั้นตอนต่อมาก็ทำการ export public_key ของ mook ออกไปให้เครื่องของ golf
mook#gpg --export -armor mook > mook.pub
mook#scp mook.pub  root@192.168.0.1:/root/.


เสร็จแล้วเริ่มทำการ import public key ของเครื่อง mook ลงไปที่เครื่อง golf
golf#gpg --import mook.pub

ทำการ list ดู key ที่ทำการ import ได้ดังนี้
golf#gpg --list-key
pub   1024D/0zzzzzz3 2014-06-19
uid                  mook (main) <mook@example.com>
sub   2048g/0xxxxxs7 2014-06-19

เข้ารหัสไฟล์ เพื่อส่งไปให้เครื่อง mook
golf#gpg -e -r mook  text.txt  
gpg: xxxxxxx: There is no assurance this key belongs to the named user

pub  2048g/0zzzzzz3 2014-06-19 mook <mook@example.com>
 Primary key fingerprint: 0222 4444 0222 DDDD 7777 2222 0000 EEEE 5555 9111
      Subkey fingerprint: 2222 6666 7777 8888 3333  EEEE 2222 5555 3333 0000

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 เพื่อทำการใช้ key)

หลังจากเสร็จแล้วจะได้ไฟล์  "text.txt.gpg"
ทำการโยนไฟล์ไปให้กับ เครื่อง mook

แล้วทำการถอดรหัสไฟล์ โดยใช้ primary key บนเครื่อง mook เพื่อถอดรหัสไฟล์ได้ดังนี้
mook#gpg -o text.txt -d text.txt.gpg

ก็จะเห็นไฟล์ ข้อความภายในได้ครับ
mook#less text.txt  

เพิ่มเติมสามารถดูได้จาก #man gpg
« Last Edit: มิถุนายน 19, 2014, 01:36:45 PM by golfreeze »

golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2140
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
Re: ทำ อย่างไร transfer file ด้วย gpg [GNU Privacy guard] private key , public key
« Reply #1 on: กุมภาพันธ์ 21, 2023, 10:15:31 AM »
== ถ้าต้องการ import public key แบบ custom path และระบุ passphrase เลย
gpg2 --import '/app/test/PKL.asc'
gpg2 --batch passparse=123xxPKL --symmetric filename