- The GNU Privacy Guard (gnupg.org)
- python-gnupg · PyPI
- python-gnupg – A Python wrapper for GnuPG
- Python gnupg (GPG) example – SaltyCrane Blog
- How To Verify Code and Encrypt Data with Python-GnuPG and Python 3 | DigitalOcean
- python-gnupg – A Python wrapper for GnuPG — Python Wrapper for GnuPG 0.4.0 documentation (pythonhosted.org)
ทดสอบบน Ubuntu 20.04.5 LTS ใน WSL 2
- Generate a key
- Export keys
- Import keys
- List keys
- Encrypt a string
- Decrypt a string
- Encrypt a file
- Decrypt a file
Installation
ติดตั้ง gnupg
(น่าจะติดตั้งอยู่แล้ว)
sudo apt-get install gnupg
$ gpg --version gpg (GnuPG) 2.2.19 libgcrypt 1.8.5 Copyright (C) 2019 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Home: /home/jack/.gnupg Supported algorithms: Pubkey: RSA, ELG, DSA, ECDH, ECDSA, EDDSA Cipher: IDEA, 3DES, CAST5, BLOWFISH, AES, AES192, AES256, TWOFISH, CAMELLIA128, CAMELLIA192, CAMELLIA256 Hash: SHA1, RIPEMD160, SHA256, SHA384, SHA512, SHA224 Compression: Uncompressed, ZIP, ZLIB, BZIP2
ติดตั้ง python-gnupg
pip install python-gnupg
1. Generate a key
import os import gnupg os.system('rm -rf /home/jack/gpghome') os.system('mkdir /home/jack/gpghome') gpg = gnupg.GPG(gnupghome='/home/jack/gpghome') input_data = gpg.gen_key_input( key_length=4096, name_email='mr.phaisarn@gmail.com', passphrase='my passphrase') key = gpg.gen_key(input_data) print(key)
84511C0301E2D51E59D79C91B5432E86F4B8925A
2. Export keys
export ค่า public key และ private key ไว้ในไฟล์ mykeyfile.asc
import gnupg key='84511C0301E2D51E59D79C91B5432E86F4B8925A' gpg = gnupg.GPG(gnupghome='/home/jack/gpghome') ascii_armored_public_keys = gpg.export_keys(key,passphrase='my passphrase') ascii_armored_private_keys = gpg.export_keys(key, True, passphrase='my passphrase') with open('mykeyfile.asc', 'w') as f: f.write(ascii_armored_public_keys) f.write(ascii_armored_private_keys)
-----BEGIN PGP PUBLIC KEY BLOCK----- mQINBGNqDu4BEADY6n5U0Fs4YBhAejiPZu5hI5i3oYbscK5xQSSXvgkUwoQkmHKQ JVv+Chq4+jCguBSlmVXXMNnk45F7OhjuxpfrXDSxQPivwchXTh0117UEtTi5jE2s eZTP5fMSdHomdD4NgU9PfSUiSPNH/PGnSQ8AFstFdFff846ry+dPu3XJdbkkVtEP Cbv6idGCa2u/ZMukj2o2ZbvOXNVj1M5RFOpqJ6m52UQXA1W8S/Qr3g7gJ41jTWE3 oOP1oSfw5IbuO6yTrhOjwELTM2BexXwUAEtW59A8e+1JVh1xtbiTRxmnC+WHZ1T1 RQ9a7rFbFyqmboZ5RE8wUX7SsklzPyXtZhCnsFfBt3OVJNyD5hPoZ4qq7ihGeIIa OD2g2VDu+uHeJg4Vqd8YcafFruLs0WR0BAdliYHClhhGSTQ2mFK7jQDfAVZYuFhZ kKqd7WV7++e/oyLViWxDvhBN1XQ7Wbw5o1hkgeH6cyFg9nX/psVecXCriOJJUyVh AKeFypLgfUiXkNoNuiWIcyJer55wIzjV0oTcLmHLtedh4Bh0+qvI+EI2UnKW2Ih0 Xas9jE3nx97T5365MD7hDjjm7sqmbg3xbih2VXUC+WjPuaisqbGgM/3vO06VA2A4 jg3j1SUm0N4IN8LSFzmAtvoSc5kvHDzTgySCYwlC3YowjKPciMu24jfjXwARAQAB tClBdXRvZ2VuZXJhdGVkIEtleSA8bXIucGhhaXNhcm5AZ21haWwuY29tPokCTgQT AQoAOBYhBIRRHAMB4tUeWdeckbVDLob0uJJaBQJjag7uAhsvBQsJCAcCBhUKCQgL AgQWAgMBAh4BAheAAAoJELVDLob0uJJaL1AP+QEa1Uc1etOnSlz8V6IV5vcpPn0i 6T1cAAb0yfHELKScL4zC6oPq2AyWTTJUT9T/qiHtWi38bM90fkNfefpMdMHeh0TQ GsTBeAO5Yd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i1R5Z15yRtUMuhvS4kloFAmNqDu4CGy8FCwkIBwIGFQoJCAsCBBYCAwEC HgECF4AACgkQtUMuhvS4klovUA/5ARrVRzV606dKXPxXohXm9yk+fSLpPVwABvTJ 8cQspJwvjMLqg+rYDJZNMlRP1P+qIe1aLfxsz3R+Q195+kx0wd6HRNAaxMF4A7lh 0X4jkYRGJvabW8omNo9U5BZpbQaEaj1+EjxMF0rRvjXfwm8el1Wyq5v8puyhFeuJ dDGtgTLtueXtGyvatiZGUU3kwaTFtWOYdiDUUmV7p7qQJEPew2zaO4NrUcOpiTr8 wXoPkuVD+ju0jnfN5GtICg69j7ISean1ro9vZfy/DcgYTM/Xa5p3zhewPYGFcue2 J7juy8OpJvM508PQ439bozkO4K1Yz2rAytgcjxDJmosbaxjsybP/aRD1H36dTY7k jmxhamRxbiiUwSl7uNqJQ2Wry0Ssig7EOidz21Su6agc5C+7FWh2dKt/XMnnKlsT 6/YLPwwfDmGgHBBaiTFnyqtOjXM/de8N720zkvKQU6jw7VEnViYInnypoekLmMhn 9v7b9goKUbddTGoVUEcuXli918RecnM6pEcuZqR3sfI6BRswFXQW1h8T6Kl2N9E9 ewuHlxsX4ehWRJytxj6qZNJ03WbhzV98s7jX1IC+EqB0LnVUB3xR2hBliPHe8PTq CEc7yw3K+5Qv3PRea4nKQ3ELV5VWfGBXyB/Xo5XKbAzFy7lE8wgO0ZLQfuQeB+Xc CY8IutY= =6b0l -----END PGP PRIVATE KEY BLOCK-----
3. Import keys
import gnupg from pprint import pprint gpg = gnupg.GPG(gnupghome='/home/jack/gpghome') key_data = open('mykeyfile.asc').read() import_result = gpg.import_keys(key_data) pprint(import_result.results)
[{'fingerprint': '84511C0301E2D51E59D79C91B5432E86F4B8925A', 'ok': '0', 'text': 'Not actually changed\n'}, {'fingerprint': '84511C0301E2D51E59D79C91B5432E86F4B8925A', 'ok': '0', 'text': 'Not actually changed\n'}, {'fingerprint': '84511C0301E2D51E59D79C91B5432E86F4B8925A', 'ok': '16', 'text': 'Not actually changed\nContains private key\n'}]
4. List keys
import gnupg from pprint import pprint gpg = gnupg.GPG(gnupghome='/home/jack/gpghome') public_keys = gpg.list_keys() private_keys = gpg.list_keys(True) print('public keys:') pprint(public_keys) print('private keys:') pprint(private_keys)
public keys: [{'algo': '1', 'cap': 'escaESCA', 'compliance': '23', 'curve': '', 'date': '1667895022', 'dummy': '', 'expires': '', 'fingerprint': '84511C0301E2D51E59D79C91B5432E86F4B8925A', 'flag': '', 'hash': '', 'issuer': '', 'keygrip': 'AADC0ABF3CA1285034094D0381E0142B513D5514', 'keyid': 'B5432E86F4B8925A', 'length': '4096', 'origin': '0', 'ownertrust': 'u', 'sig': '', 'sigs': [], 'subkeys': [], 'token': '', 'trust': 'u', 'type': 'pub', 'uids': ['Autogenerated Key <mr.phaisarn@gmail.com>'], 'updated': ''}] private keys: [{'algo': '1', 'cap': 'escaESCA', 'compliance': '23', 'curve': '', 'date': '1667895022', 'dummy': '', 'expires': '', 'fingerprint': '84511C0301E2D51E59D79C91B5432E86F4B8925A', 'flag': '', 'hash': '', 'issuer': '', 'keygrip': 'AADC0ABF3CA1285034094D0381E0142B513D5514', 'keyid': 'B5432E86F4B8925A', 'length': '4096', 'origin': '0', 'ownertrust': 'u', 'sig': '', 'sigs': [], 'subkeys': [], 'token': '+', 'trust': 'u', 'type': 'sec', 'uids': ['Autogenerated Key <mr.phaisarn@gmail.com>'], 'updated': ''}]
5. Encrypt a string
import gnupg gpg = gnupg.GPG(gnupghome='/home/jack/gpghome') unencrypted_string = 'Who are you? How did you get in my house?' encrypted_data = gpg.encrypt(unencrypted_string, 'mr.phaisarn@gmail.com') encrypted_string = str(encrypted_data) print('ok: ', encrypted_data.ok) print('status: ', encrypted_data.status) print('stderr: ', encrypted_data.stderr) print('unencrypted_string: ', unencrypted_string) print('encrypted_string: ', encrypted_string)
ok: True status: encryption ok stderr: gpg: WARNING: unsafe permissions on homedir '/home/jack/gpghome' [GNUPG:] KEY_CONSIDERED 84511C0301E2D51E59D79C91B5432E86F4B8925A 0 [GNUPG:] KEY_CONSIDERED 84511C0301E2D51E59D79C91B5432E86F4B8925A 2 [GNUPG:] ENCRYPTION_COMPLIANCE_MODE 23 [GNUPG:] BEGIN_ENCRYPTION 2 9 [GNUPG:] END_ENCRYPTION unencrypted_string: Who are you? How did you get in my house? encrypted_string: -----BEGIN PGP MESSAGE----- hQIMA7VDLob0uJJaAQ//czHG42eeN1rtFTo6qdvoHSa/B3uJNeFSobUi4gUCxceh yVbZ0La8pnLMy2JmA2agYaOvC6OhAXfNOhPXV5jinMTvwGB+dKC1qaAYXMknQjyF 5whvmQeBbuQX8AoKxs7hAYmlQO8mrHDBoP2xSdY/AgLeqdlnbZNcDR+NIpoPJS8a zr+KHYAhEyZT0Bm6FAw7DvhriB4nJRLeMLoIQKIJTN3ZeEdlcZAt5xYL24dZfz8/ 4Qpn++ItqsCQfXJ1leJIIE/oB4E83/J4FZuS1xh7n9OomBT2fRG8hmGlSzFYepX/ jDzjApmA/R4HGd6BuJZdNy6SV2DERs2NlTRko98IDFdCIg2Jp7jsSGSZowsKD7z5 rIU110iwR6hRropsuX18jlRk6QM1Um+dSGbHXTUk1OzUDE5uC3hBZsB0s9M2q5DS HN+EbyatT9hB9X5Obf7w9Vj+Jta9q5voL/riZ2d+GFr8oR0d594An7Dup12zBff/ kdV7DgibAqorCD1MvVePOpvYw2NdhukRluOsVtXySkcAGy8VssBLoW3kqilOvrhq pCJUG157TnhKnYW0l4BJ7KVoNFwBGsJKzS0Drh5Iq6LmiqHsKv7neK9LkApDeehD EhdNoGN79fPvEYmync2bbGbVq6F7czHADwVKqNv0aDxaCrFHASsSXkGNbqMQ+tnS YgFfzEOmYoDR7UjEog/OrBJAnPSgDcwn3I0xuU3RtDaEvlHCt6aX+NHEyIUvsIe+ Don8uUXi4VkjO2uoCOyNgwndoigDuouh8NDv5DIWcKt+KMGPkgKEmfbOxJNA0Rl5 TK5E =Jyon -----END PGP MESSAGE-----
6. Decrypt a string
import gnupg gpg = gnupg.GPG(gnupghome='/home/jack/gpghome') unencrypted_string = 'Who are you? How did you get in my house?' encrypted_data = gpg.encrypt(unencrypted_string, 'mr.phaisarn@gmail.com') encrypted_string = str(encrypted_data) decrypted_data = gpg.decrypt(encrypted_string, passphrase='my passphrase') print('ok: ', decrypted_data.ok) print('status: ', decrypted_data.status) print('stderr: ', decrypted_data.stderr) print('decrypted string: ', decrypted_data.data)
ok: True status: decryption ok stderr: gpg: WARNING: unsafe permissions on homedir '/home/jack/gpghome' [GNUPG:] ENC_TO B5432E86F4B8925A 1 0 [GNUPG:] KEY_CONSIDERED 84511C0301E2D51E59D79C91B5432E86F4B8925A 0 [GNUPG:] KEY_CONSIDERED 84511C0301E2D51E59D79C91B5432E86F4B8925A 0 [GNUPG:] DECRYPTION_KEY 84511C0301E2D51E59D79C91B5432E86F4B8925A 84511C0301E2D51E59D79C91B5432E86F4B8925A u [GNUPG:] KEY_CONSIDERED 84511C0301E2D51E59D79C91B5432E86F4B8925A 0 gpg: encrypted with 4096-bit RSA key, ID B5432E86F4B8925A, created 2022-11-08 "Autogenerated Key <mr.phaisarn@gmail.com>" [GNUPG:] BEGIN_DECRYPTION [GNUPG:] DECRYPTION_COMPLIANCE_MODE 23 [GNUPG:] DECRYPTION_INFO 2 9 [GNUPG:] PLAINTEXT 62 1667895570 [GNUPG:] PLAINTEXT_LENGTH 41 [GNUPG:] DECRYPTION_OKAY [GNUPG:] GOODMDC [GNUPG:] END_DECRYPTION decrypted string: b'Who are you? How did you get in my house?'
7. Encrypt a file
import gnupg gpg = gnupg.GPG(gnupghome='/home/jack/gpghome') open('my-unencrypted.txt', 'w').write('You need to Google Venn diagram.') with open('my-unencrypted.txt', 'rb') as f: status = gpg.encrypt_file( f, recipients=['mr.phaisarn@gmail.com'], output='my-encrypted.txt.gpg') print('ok: ', status.ok) print('status: ', status.status) print('stderr: ', status.stderr)
ok: True status: encryption ok stderr: gpg: WARNING: unsafe permissions on homedir '/home/jack/gpghome' [GNUPG:] KEY_CONSIDERED 84511C0301E2D51E59D79C91B5432E86F4B8925A 0 [GNUPG:] KEY_CONSIDERED 84511C0301E2D51E59D79C91B5432E86F4B8925A 2 [GNUPG:] ENCRYPTION_COMPLIANCE_MODE 23 [GNUPG:] BEGIN_ENCRYPTION 2 9 [GNUPG:] END_ENCRYPTION
-----BEGIN PGP MESSAGE----- hQIMA7VDLob0uJJaARAAhwNuaLm5/eSCV81Uw+H2E536AzzXm/yPxM0CIjLjYxnE +GwgvBxJsYtuuwCElUfYkIKYNzAWOGGRje961Be3+g5kjFfphvl4I1Xzwb5RKdWa i9raKNQ1/PLQ6Hr8Fc3yh08QcgLkihyRgVKZ3suTX+KRzWd3/SS8rjS27nigQk8C IoIuwJIsBYpB8xbBDM+Sfo2XJm938Z7n9ZfdDiuO0hEWBo5qF85Vh0jY91BVyPqD yGYcsivK/OqRG/VrPTrp01+2+jBn5HPZoPCnjEZdyJ3p+k/bnvAymKpKmPfEHrt3 4FwJ0EIfFROx6u2xBWhoRAhl5vLjIgRqccwEHD9+HrT3aFyeQhOKcQoV0TXy5Y0w rYLRBny2oolCGW1NqZi43pjamQXXVpJLDjOsn8pzhdbFiSxUrFZj2WXET7ItQaVd fQQnPxpOpj7LOOncLdZ0qfGMYjNNXGoKlaPfdtJy1yQlz8mUUdZZAjAdCpKpnfca YxuFmzMy2gGBKsYYrLB2FH6HasRbjb5uQ1qO72vybuPWCBNkX5jgWHGLddNagxR7 9EwMnvu958Oy3TiFfrr7YKWPIOQXaLx8WsgXCCl2rFJKG4Hy81ZM5k7DDQzzQFGd w+a5FWo/cthXm6qx2LSE2ByFyAerqwe/T/wkAYmrQLIJdxZIWxp5xsuPBS6W6oLS WwEiSHXqJPjqKHKnZwS0W9kWq/68voNf03QlEkN0+Hm3PRmEFIJnpw19BtfQEoY3 Rv/XCQXE2G6NVMajvFj72fvFzTUUyVRa8m//nu2wg9vG5JMjJbThA8L8+5E= =WZm8 -----END PGP MESSAGE-----
ถ้า import public key แล้วนำมาใช้ encrypt จะได้ error invalid recipient
ok: False status: invalid recipient stderr: gpg: WARNING: unsafe permissions on homedir `/home/jack/gpghome' gpg: EAECD251: There is no assurance this key belongs to the named user [GNUPG:] INV_RECP 10 84511C0301E2D51E59D79C91B5432E86F4B8925A gpg: [stdin]: encryption failed: Unusable public key
ให้เพิ่ม gpg.trust_keys()
import gnupg gpg = gnupg.GPG(gnupghome='/home/jack/gpghome') gpg.trust_keys('84511C0301E2D51E59D79C91B5432E86F4B8925A', 'TRUST_ULTIMATE') open('my-unencrypted.txt', 'w').write('You need to Google Venn diagram.') with open('my-unencrypted.txt', 'rb') as f: status = gpg.encrypt_file( f, recipients='84511C0301E2D51E59D79C91B5432E86F4B8925A', output='encrypted.txt.gpg') print('ok: ', status.ok) print('status: ', status.status) print('stderr: ', status.stderr)
8. Decrypt a file
import gnupg gpg = gnupg.GPG(gnupghome='/home/jack/gpghome') with open('my-encrypted.txt.gpg', 'rb') as f: status = gpg.decrypt_file(f, passphrase='my passphrase', output='my-decrypted.txt') print('ok: ', status.ok) print('status: ', status.status) print('stderr: ', status.stderr)
ok: True status: decryption ok stderr: gpg: WARNING: unsafe permissions on homedir '/home/jack/gpghome' [GNUPG:] ENC_TO B5432E86F4B8925A 1 0 [GNUPG:] KEY_CONSIDERED 84511C0301E2D51E59D79C91B5432E86F4B8925A 0 [GNUPG:] KEY_CONSIDERED 84511C0301E2D51E59D79C91B5432E86F4B8925A 0 [GNUPG:] DECRYPTION_KEY 84511C0301E2D51E59D79C91B5432E86F4B8925A 84511C0301E2D51E59D79C91B5432E86F4B8925A u [GNUPG:] KEY_CONSIDERED 84511C0301E2D51E59D79C91B5432E86F4B8925A 0 gpg: encrypted with 4096-bit RSA key, ID B5432E86F4B8925A, created 2022-11-08 "Autogenerated Key <mr.phaisarn@gmail.com>" [GNUPG:] BEGIN_DECRYPTION [GNUPG:] DECRYPTION_COMPLIANCE_MODE 23 [GNUPG:] DECRYPTION_INFO 2 9 [GNUPG:] PLAINTEXT 62 1667895671 [GNUPG:] PLAINTEXT_LENGTH 32 [GNUPG:] DECRYPTION_OKAY [GNUPG:] GOODMDC [GNUPG:] END_DECRYPTION