[Python] Decrypting a file with Fernet in Python is returning an empty string

Discussão em 'Python' iniciado por Stack, Fevereiro 10, 2025.

  1. Stack

    Stack Membro Participativo

    I was testing creating a function to decrypt files within a directory using cryptography.fernet, but all I'm getting back is an empty string. I have looked elsewhere but could not find an answer.

    My current file is as such:

    import os
    from cryptography.fernet import Fernet

    fileDirectory = input('Please specfy directory to decrypt\n > ')

    os.chdir(fileDirectory)

    f = open('key.txt', 'rb')
    red = f.read()
    print(red)
    fernet = Fernet(red)
    f.close()

    dirContain = os.listdir()
    print(dirContain)
    dirContain.pop(dirContain.index('key.txt'))

    print(dirContain)

    for i in range(len(dirContain)):
    print(dirContain)
    f = open(dirContain, 'rb')
    rend = f.read()
    print(rend)
    data = fernet.decrypt(rend)
    print(data)
    f.close()


    And I get this as the output:

    Please specfy directory to decrypt
    > test
    uKPMID0YwQD0dneykrCkrUEnSwbFTRs-eDvXpF7KSi4=
    ['jibberish.txt', 'key.txt', 'otherJibberish.txt']
    ['jibberish.txt', 'otherJibberish.txt']
    jibberish.txt
    b'gAAAAABjwVrevzkZQoITAbDXYI2MlXgyBQIpTrYvwUnj0978KZxtrlZ46SUREyo3Zfg0S4c8kIDRQ2Qn59JSjVAlQ6RmYgg4yw=='
    b''
    otherJibberish.txt
    b'gAAAAABjwVre4cpnG6Zn1iAZv6VcaUQt5-8sDtjQzj5PBd4F2aIeze_ka_MRmHwnhJP5WwSyQUOskF3--X-19DD9dUIRw-jIFw=='
    b''



    Any ideas as to what is going on? Also, the only difference between me reading the files as binary or text is that, while reading as text, printing the f.read() doesn't have the b'' around the content.

    Continue reading...

Compartilhe esta Página