file_crypto.py 文件源码

python
阅读 21 收藏 0 点赞 0 评论 0

项目:storj-python-sdk 作者: Storj 项目源码 文件源码
def decrypt_file_aes(self, in_file, out_file, password, key_length=32):
        bs = AES.block_size
        salt = in_file.read(bs)[len('Salted__'):]
        key, iv = self.derive_key_and_iv(password, salt, key_length, bs)
        cipher = AES.new(key, AES.MODE_CBC, iv)
        next_chunk = ''
        finished = False
        while not finished:
            chunk, next_chunk = next_chunk, cipher.decrypt(in_file.read(1024 * bs))
            if len(next_chunk) == 0:
                padding_length = ord(chunk[-1])
                if padding_length < 1 or padding_length > bs:
                    raise ValueError('bad decrypt pad (%d)' % padding_length)
                # all the pad-bytes must be the same
                if chunk[-padding_length:] != (padding_length * chr(padding_length)):
                    # this is similar to the bad decrypt:evp_enc.c from openssl program
                    raise ValueError('bad decrypt')
                chunk = chunk[:-padding_length]
                finished = True
            out_file.write(chunk)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号