Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to decrypt a PGP file using Public Key #288

Closed
felixcheruiyot opened this issue Sep 5, 2019 · 5 comments
Closed

How to decrypt a PGP file using Public Key #288

felixcheruiyot opened this issue Sep 5, 2019 · 5 comments

Comments

@felixcheruiyot
Copy link

felixcheruiyot commented Sep 5, 2019

I have an issue, also highlighted here > https://stackoverflow.com/questions/57807242/how-to-decrypt-a-pgp-file-using-public-key-in-pgpy

I have a Public shared to me and I'm loading it as shown below:

key_path = os.environ.get('ESB_FILES_PUBLIC_KEY')
key, _ = pgpy.PGPKey.from_file(key_path)

I try to decrypt the file using this key

message_from_file = pgpy.PGPMessage.from_file(filepath)
raw_message = key.decrypt(message_from_file).message
print(raw_message)

It doesn't work and I get this error instead pgpy.errors.PGPError: Expected: is_public == False. Got: True

Any idea on how to decrypt an PGP file in Python with only the Public Key.

Thanks

@Commod0re
Copy link
Contributor

You can't; public keys can only be used for encryption and signature verification. Decryption and signing require the private key.

@felixcheruiyot
Copy link
Author

Thanks @Commod0re for the quick reply.

Is this how PGP generally works or basically based on the scope of the PGPy module?

Please advise.

Thanks.

@Commod0re
Copy link
Contributor

Yes, this is generally how public-key cryptography works

@felixcheruiyot
Copy link
Author

Thanks @Commod0re

@sydneywu
Copy link

Sorry to bring out an old issue. From my understanding of PGP, it requires 2 sets of key for encrypting and 2 sets of key for decrypting. So for example, when a sender encrypt a file for me:

Sender will need to use his private key and my public key to encrypt.
I will need to use my private key and sender's public key to decrypt.

You can see in this guide: https://www.howtogeek.com/427982/how-to-encrypt-and-decrypt-files-with-gpg-on-linux/
"To send a file securely, you encrypt it with your private key and the recipient’s public key. To decrypt the file, they need their private key and your public key."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants