[Python] I am decrypting files in gnupg but they are being written as empty

Discussão em 'Python' iniciado por Stack, Abril 29, 2025.

  1. Stack

    Stack Membro Participativo

    This script is supposed to download gpg files off an sftp server, copy them to the local windows dir, back up the encrypted files, and decrypt the files to their own directory. Everything is working except that the decrypted files that are created are always empty.

    import pysftp
    import gnupg #dycription
    import os

    ############## Decryption variables
    KeyPath=(r"C:\winuser\test\key"+slash)
    KeyFile=(r"C:\winuser\test\sbp.asc")
    DecryptedPath=(r"C:\winuser\test\Deycrpt"+slash)
    SourcPath=(r"C:\winuser\test"+slash)
    ArchivePath=(r"C:\winuser\test"+slash)

    def ConnectSFTP(server, port, username, password, cnopts):

    def DownloadSFTP(RemotePath, end, LocalPath,newsftp):

    def decrypt(KeyPath, KeyFile, DecryptedPath, SourcePath, ArchivePath, ls):
    gpg = gnupg.GPG(gnupghome=KeyPath)
    gpg.import_keys(open(KeyFile).read())
    for f in ls:
    with open(SourcePath+f, 'rb') as fh:
    status = gpg.decrypt_file(fh, output=f[:-4])
    outfile = DecryptedPath+f[:-4]
    with open(outfile,"wb") as fo:
    fo.write(status.data)
    #os.rename(DecryptedPath+f[:-4], DecryptedPath+f[:-4]+status.extension)
    os.rename(SourcePath+f, ArchivePath+f)
    return True

    with ConnectSFTP(host, port, username, password, cnopts) as newsftp:
    ls = DownloadSFTP(RemotePath, end,LocalPath,newsftp)

    decrypt(KeyPath, KeyFile, DecryptedPath, SourcPath, ArchivePath, ls)

    print(ls)


    the only thing I can think of is that I am not writing to the files. Being new to python I am a little befuddled as to how I am ducking it up.

    Continue reading...

Compartilhe esta Página