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

2
  • Because it's an object not a string itself you can call specific attribute of encrypted_token_message_pretty and then perform replace operation on it. Commented Jun 8, 2019 at 9:22
  • Well cool, thanks for this hint. can you provide an example or an answere here? Commented Jun 8, 2019 at 9:27

2 Answers 2

1

It's and object not a string itself. You can call a specific attribute on it in order to replace the version number like this -

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

You can also find the list of possible attributes using encrypted_token_message_pretty.__dict__

Sign up to request clarification or add additional context in comments.

Comments

0

As stated in the PGPy documentation, the encrypt method returns an instance of PGPMessage. The reason why you can convert that object into a str is because it overrides the special __str__ method.

Anyway, replace is a method of str, not of PGPMessage. So if you want to replace the Version:, convert your message to a string, and then replace the 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

1 Comment

Thanks for your example, i was a bit confused about the object handling here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.