1

I am trying to encrypt a file using pgpy. I am able to encrypt the content of files but unable to save it. I am trying to get output as .csv.pgp

Getting this error: encrypted_file.write(encrypted_f_t_e) TypeError: a bytes-like object is required, not 'PGPMessage'

import pgpy
from pgpy import PGPKey, PGPMessage
PUBLIC_KEY_FILE = 'myPublicKey.asc'    
pub_key, _ = pgpy.PGPKey.from_file(str(PUBLIC_KEY_FILE))
FILE_TO_ENCRYPT = 'data.csv'
f_t_e = pgpy.PGPMessage.new(str(FILE_TO_ENCRYPT),file=True)
print(f_t_e.is_encrypted)
encrypted_f_t_e = pub_key.encrypt(f_t_e)
print(encrypted_f_t_e)
with open('data.csv.pgp', 'wb') as encrypted_file:
    encrypted_file.write(encrypted_f_t_e)

Aman shaw
  • 51
  • 7

1 Answers1

0

You need to either write bytes or use w file mode (not wb):

  1. Bytes option
with open('data.csv.pgp', 'wb') as encrypted_file:
    encrypted_file.write(bytes(encrypted_f_t_e))
  1. Text option
with open('data.csv.pgp', 'w') as encrypted_file:
    encrypted_file.write(str(encrypted_f_t_e))
Klim
  • 142
  • 2
  • 9