[Python] Transparent filesystem in pyfuse with encryption and compression

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

  1. Stack

    Stack Membro Participativo

    I'm trying to make a simple transparent filesystem in fuse. I'm using this guide as a basis. This code works perfectly. I'm trying to modify it so that it compresses and then encrypts as well.

    So far I have:

    from Crypto.Cipher import AES
    from Crypto.Util.Padding import pad, unpad

    AES_KEY = b"thisisaverysecretkey1234"
    AES_IV = b"thisisaverysecre"

    def encrypt_data(self, data):
    cipher = AES.new(AES_KEY, AES.MODE_CBC, AES_IV)
    return cipher.encrypt(pad(data, AES.block_size))

    def decrypt_data(self, data):
    cipher = AES.new(AES_KEY, AES.MODE_CBC, AES_IV)
    return unpad(cipher.decrypt(data), AES.block_size)


    And I modified the read and write methods like so:

    def read(self, path, size, offset, fh):
    full_path = self._get_file_path(path)
    os.lseek(fh, offset, os.SEEK_SET)
    encrypted_data = os.read(fh, size)

    # Decrypt the data before returning it
    return decrypt_data(encrypted_data)

    def write(self, path, buf, offset, fh):
    full_path = self._get_file_path(path)
    os.lseek(fh, offset, os.SEEK_SET)

    # Encrypt data before writing it
    encrypted_data = self._encrypt_data(buf)

    return os.write(encrypted_data)


    However, I keep getting the error:

    fuse: wrote too many bytes


    I can't seem to write encrypted files properly (I haven't even compressed/decompressed before applying encryption).

    This is just a side project to test out my own compression/encryption scheme, and I'm using aes and zlib as placeholders.

    Continue reading...

Compartilhe esta Página