def generate_encrypted_profile(email, key): cipher = AES.new(bytes(key), AES.MODE_ECB) profile = profile_for(email) profile_bytearr = profile.encode("utf-8") padded_profile_bytearr = padder(profile_bytearr, len(key)) ciphertext = cipher.encrypt(bytes(padded_profile_bytearr)) return ciphertext
def echo_message(self, encrypted_message, iv): cipher = AES.new(self.aes_key, AES.MODE_CBC, iv) padded_message = cipher.decrypt(encrypted_message) message = unpadder(padded_message) print("{} received message: {}".format(self.name, message)) return_iv = bytes( [random.randint(0, 255) for _ in range(len(self.aes_key))]) return_cipher = AES.new(self.aes_key, AES.MODE_CBC, return_iv) return return_cipher.encrypt(padder(message, len(self.aes_key))), return_iv
def encrypt_ecb_cbc(target_bytearr, is_ecb): key = gen_random_key(16) padded_target_bytearr = padder(target_bytearr, len(key)) if is_ecb: ciphertext_bytearr = encrypt_ecb(bytes(padded_target_bytearr), bytes(key)) else: iv = bytearray([random.randint(0, 255) for k in range(len(key))]) ciphertext_bytearr = encrypt_cbc(bytes(padded_target_bytearr), iv, bytes(key)) return ciphertext_bytearr
def send_message(self, message, partner): print("{} sending message: {}".format(self.name, message)) padded_message = padder(message, len(self.aes_key)) iv = bytes([random.randint(0, 255) for _ in range(len(self.aes_key))]) cipher = AES.new(self.aes_key, AES.MODE_CBC, iv) encrypted_message = cipher.encrypt(padded_message) echoed_message, partner_iv = partner.echo_message( encrypted_message, iv) partner_cipher = AES.new(self.aes_key, AES.MODE_CBC, partner_iv) decrypted_echoed_message = unpadder( partner_cipher.decrypt(echoed_message)) assert decrypted_echoed_message == message, "sent and echoed message differ do not match! sent: {} echoed: {}".format( message, decrypted_echoed_message)
return b"".join(encrypted_block_list) def decrypt(target_bytearray, iv, key): block_list = split_into_blocks(target_bytearray, len(key)) decrypted_block_list = decrypt_blocks(block_list, iv, key) return b"".join(decrypted_block_list) if __name__ == "__main__": plaintext = "The quick brown fox jumps over the lazy dog" plaintext_bytearray = plaintext.encode("utf-8") key = "YELLOW SUBMARINE" iv = bytearray(len(key)) padded_plaintext_bytearray = padder(plaintext_bytearray, len(key)) print("Plaintext: {}".format(plaintext)) ciphertext = encrypt(padded_plaintext_bytearray, iv, key) print("Ciphertext: {}".format(ciphertext)) recreated_plaintext = decrypt(ciphertext, iv, key) unpadded_recreated_plaintext = unpadder(recreated_plaintext) print("Recreated plaintext: {}".format(unpadded_recreated_plaintext)) assert plaintext_bytearray == unpadded_recreated_plaintext, "plaintext and recreated_plaintext do not match!" """ ## DECRYPT GIVEN TEXT FILE with open("set2/data_ch_2.txt", "r") as f: ciphertext_bytearr = b64decode("".join(f.read().strip().split("\n")))
def encrypt(prefix_bytearr, my_bytearr, append_bytearr, key): target_bytearr = prefix_bytearr + my_bytearr + append_bytearr padded_target_bytearr = padder(target_bytearr, len(key)) cipher = AES.new(bytes(key), AES.MODE_ECB) return cipher.encrypt(bytes(padded_target_bytearr))
def encrypt_cbc(plaintext_bytearr, key): padded_plaintext_bytearr = padder(plaintext_bytearr, len(key)) iv = key cipher = AES.new(key, AES.MODE_CBC, iv) return cipher.encrypt(padded_plaintext_bytearr)
def encrypt(plaintext_bytearr, key): iv = generate_random_bytes(len(key)) padded_plaintext_bytearr = padder(plaintext_bytearr, len(key)) cipher = AES.new(key, AES.MODE_CBC, iv) ciphertext_bytearr = cipher.encrypt(padded_plaintext_bytearr) return ciphertext_bytearr, iv