def encrypt_fp(secret_key, plaintext_fp, cipher_fp, init_vec=''): encrypt('\x00' * 16, secret_key) while True: block = plaintext_fp.read(block_size) block_len = len(block) if 0 < block_len < block_size: block += padding(block_len) cipher_fp.write(''.join([encrypt(block[start:start + 16]) for start in rng[:block_len / 16 + 1]])) break cipher_fp.write(''.join([encrypt(block[start:start + 16]) for start in rng])) return cipher_fp
def protocrypt_fp(func, secret_key, in_fp, out_fp, init_vector='\x00' * 16): encrypt('\x00' * 16, secret_key) in_fp.seek(0, os.SEEK_END) rest_size = in_fp.tell() in_fp.seek(0, os.SEEK_SET) while True: if not rest_size: break elif rest_size > block_size_and_a_byte: size = block_size else: size = rest_size out_text, init_vector = func(in_fp.read(size), init_vector) out_fp.write(out_text) rest_size -= size return out_fp
def decrypt_fp(secret_key, cipher_fp, plaintext_fp, init_vec=''): encrypt('\x00' * 16, secret_key) length = len_fp(cipher_fp) while True: block = cipher_fp.read(block_size) block_len = len(block) if block_len < block_size: plaintext_block = ''.join([decrypt(block[start:start + 16]) for start in rng[:block_len / 16]]) else: plaintext_block = ''.join([decrypt(block[start:start + 16]) for start in rng]) if cipher_fp.tell() == length: plaintext_block = plaintext_block[:-ord(plaintext_block[-1])] plaintext_fp.write(plaintext_block) break plaintext_fp.write(plaintext_block) return plaintext_fp