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("{} relayed message: {}".format(self.name, message)) recipient_message, recipient_iv = self.recipient.echo_message( encrypted_message, iv) recipient_cipher = AES.new(self.aes_key, AES.MODE_CBC, recipient_iv) decrypted_recipient_message = unpadder( recipient_cipher.decrypt(recipient_message)) print("{} relayed message: {}".format(self.name, decrypted_recipient_message)) return recipient_message, recipient_iv
def decrypt(ciphertext_bytearr, key): iv = key cipher = AES.new(key, AES.MODE_CBC, iv) ciphertext_bytearr = bytes(ciphertext_bytearr) padded_plaintext_bytearr = cipher.decrypt(ciphertext_bytearr) plaintext_bytearr = unpadder(padded_plaintext_bytearr) for b in plaintext_bytearr: if b > 127: return False, plaintext_bytearr return True, plaintext_bytearr
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 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)
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"))) key = "YELLOW SUBMARINE" iv = bytearray(len(key)) plaintext = decrypt(ciphertext_bytearr, iv, key) print(plaintext) """
def decrypt_profile(ciphertext, key): cipher = AES.new(bytes(key), AES.MODE_ECB) decrypted_cookie = unpadder(cipher.decrypt(ciphertext)) decrypted_profile = parse_cookie(decrypted_cookie.decode("utf-8")) return decrypted_profile