0

at my views.py i have the following line

encrypted_token_message = encryption_key.encrypt(PGPMessage.new(token_message), cipher=SymmetricKeyAlgorithm.AES256)

which creates a PGP message with a version information like this

-----BEGIN PGP MESSAGE-----

Version: XYZ

How can i remove/replace this version line?

if i try:

encrypted_token_message_pretty = (encrypted_token_message.replace('Version: XYZ', 'Version: XXX'))

i get back:

'PGPMessage' object has no attribute 'replace'

Thanks and regards

4

2 回答 2

1

它和对象本身不是字符串。您可以调用它的特定属性以替换版本号,如下所示 -

encrypted_token_message_pretty._attribute_name.replace('Version: XYZ', 'Version: XXX')

您还可以使用以下方法找到可能的属性列表encrypted_token_message_pretty.__dict__

于 2019-06-08T09:41:26.067 回答
0

PGPy 文档中所述,该encrypt方法返回PGPMessage. 您可以将该对象转换为 astr的原因是因为它覆盖了特殊__str__方法。

无论如何,replace是一种方法str,而不是PGPMessage。因此,如果要替换Version:,请将您的消息转换为字符串,然后替换版本。

encrypted_token = str(encryption_key.encrypt(PGPMessage.new(token_message), cipher=SymmetricKeyAlgorithm.AES256))  # Gets the string representing the newly created message
encrypted_token_message_pretty = encrypted_token.replace('Version: XYZ', 'Version: XXX')  # encrypted_token is now a string, you can replace whatever you want

于 2019-06-08T09:42:37.943 回答