【问题标题】:Encrypt/Decrypt message using PGPy使用 PGPy 加密/解密消息
【发布时间】:2020-11-01 15:16:03
【问题描述】:

我正在尝试 PGPy,但每当我加密或解密消息时都会出错。

这些是我使用的代码。文档和示例可以在here找到。

使用公钥加密消息的代码:

import pgpy

key_pub = '''BEGIN PUBLIC KEY BLOCK...END PUBLIC KEY BLOCK'''.lstrip()
message = "It worked!"

# import ASCII formatted public key
pub_key = pgpy.PGPKey()
pub_key.parse(key_pub)

# create new message
text_message = pgpy.PGPMessage.new(message)

# encrypt a message using pub key
encrypted_message = pub_key.encrypt(text_message)

print(encrypted_message)

这个给出了输出但带有一条消息

UserWarning: Selected compression algorithm not in key preferences
  encrypted_message = pub_key.encrypt(text_message)

我不明白为什么。
使用私钥解密消息的代码:

import pgpy

key_priv ='''*BEGIN PRIV KEY BLOCK...END PRIV KEY BLOCK'''.lstrip()
cipher_text = '''BEGIN PGP MESSAGE...END PGP MESSAGE'''.lstrip()

# import ASCII formatted private key
priv_key = pgpy.PGPKey()
priv_key.parse(key_priv)

message_from_blob = pgpy.PGPMessage.from_blob(cipher_text)

# decrypts a message using priv key
decrypted_message = priv_key.decrypt(cipher_text)

print(decrypted_message)

而这个根本不起作用。这是我得到的错误。

    Traceback (most recent call last):
  File "C:/Users..practice.py", line 13, in <module>
    decrypted_message = priv_key.decrypt(cipher_text)
  File "C:\Users...venv\lib\site-packages\pgpy\decorators.py", line 126, in _action
    self.check_attributes(key)
  File "C:\Users...venv\lib\site-packages\pgpy\decorators.py", line 111, in check_attributes
    raise PGPError("Expected: {attr:s} == {eval:s}. Got: {got:s}"
pgpy.errors.PGPError: Expected: is_unlocked == True. Got: False

【问题讨论】:

  • 你使用什么加密,RSA?如果不支持,这将解释您的第一个错误。

标签: python encryption pgp


【解决方案1】:
  1. 假设您有一个 RSA 公钥和私钥,加密字符串或文件可以很简单:
    PUBLIC_KEY_FILE = 'path/to/keyfile/my_pub_key.asc'    
    pub_key, _ = pgpy.PGPKey.from_file(str(PUBLIC_KEY_FILE))
    
    # Encrypt string
    txt_msg = pgpy.PGPMessage.new("Hello PGPy World")
    print('txt_msg.is_encrypted')
    print(txt_msg.is_encrypted)
    print('txt_msg.message')
    print(txt_msg.message)
    encrypted_txt_msg = pub_key.encrypt(txt_msg)
    print('encrypted_txt_msg.is_encrypted')
    print(encrypted_txt_msg.is_encrypted)
    print('encrypted_txt_msg.message')
    print(encrypted_txt_msg.message)
    
    # Encrypt file
    f_t_e = pgpy.PGPMessage.new(str(FILE_TO_ENCRYPT),file=True)
    print('f_t_e.is_encrypted')
    print(f_t_e.is_encrypted)
    encrypted_f_t_e = pub_key.encrypt(f_t_e)
    print('encrypted_f_t_e.is_encoded')
    print(encrypted_f_t_e.is_encrypted)
  1. 假设您拥有与上述公钥相关联的 RSA 私钥(在收件人列表中)和一个包含密码短语的文本文件,那么解密字符串或文件非常简单:
    # Load passphrase from file
    with open(PASSPHRASE_FILE,'r') as ppfp:
        PASSPHRASE = ppfp.readline().replace('\n','')
    print(PASSPHRASE)
    
    # Load private key
    PRIVATE_KEY_FILE='path/to/keyfile/my_prv_key.gpg'
    prv_key, _ = pgpy.PGPKey.from_file(str(PRIVATE_KEY_FILE))

    # Unlock private key
    print(prv_key.is_protected)  # Should be True
    with prv_key.unlock(PASSPHRASE):
        print(prv_key.is_unlocked)  #Should be True

        # Decrypt string
        decrypted_txt_msg = prv_key.decrypt(encrypted_txt_msg)
        print('decrypted_txt_msg.is_encrypted')
        print(decrypted_txt_msg.is_encrypted)
        print('decrypted_txt_msg.message')
        print(decrypted_txt_msg.message)

        # Decrypt file
        decrypted_f_t_e = prv_key.decrypt(encrypted_f_t_e)
        print('decrypted_f_t_e.is_encrypted')
        print(decrypted_f_t_e.is_encrypted)

【讨论】:

    猜你喜欢
    • 2021-07-04
    • 1970-01-01
    • 1970-01-01
    • 2019-01-14
    • 1970-01-01
    • 2012-01-17
    • 2017-11-08
    • 2022-01-28
    • 1970-01-01
    相关资源
    最近更新 更多