最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - Transparent filesystem in pyfuse with encryption and compression - Stack Overflow

programmeradmin0浏览0评论

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.

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.

Share Improve this question asked Feb 4 at 0:17 leelee 7722 gold badges12 silver badges27 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

The meaning of the message is that the number of bytes actually written is larger than the number of bytes the caller intended to write. And it makes sense because the buffer you actually write (encrypted_data) is different (in content and size, I guess) than the buffer given to fuse (buf).

You need to return the size of buf and not the return value of os.write.

This requires you as well, in the output of getattr to modify the size field of the file to the size as if the whole file is decoded.

Another issue is reading the file starting from some offset. You need to adjust the offset to be in "encrypted blocks" count.

发布评论

评论列表(0)

  1. 暂无评论