def forward_and_decrypt(self, key: bytes) -> bytes: key = SHA1(key).digest()[:16] iv = self.received_cyphertext[-16:] message = de_pkcs7(decrypt_aes_cbc(key, iv, self.received_cyphertext[:-16])) self.peer.receive_message(self.received_cyphertext) return message
def main() -> None: with open("data/10.txt", "r") as data: cyphertext = b64decode(data.read()) key = b"YELLOW SUBMARINE" iv = bytes(len(key)) plaintext = de_pkcs7(decrypt_aes_cbc(key, iv, cyphertext)) print(plaintext.decode())
def break_ecb(oracle: Callable[[bytes], bytes]) -> bytes: bs = blocksize(oracle) l = len(oracle(b"")) string_length = len_string(oracle) plaintext = b"" prefix = (l + bs - 1) * b"A" while len(plaintext) <= string_length: oracle_prefix = oracle(prefix) for i in range(127): test = prefix + plaintext + bytes([i]) if oracle(test)[l:l + bs] == oracle_prefix[l:l + bs]: prefix = prefix[1:] plaintext += bytes([i]) break return de_pkcs7(plaintext)
def break_ecb(oracle: Callable[[bytes], bytes]) -> bytes: bs = blocksize(oracle) l = len(oracle(b"")) prefix_length = len_prefix(oracle) string_length = len_string(oracle) - prefix_length plaintext = b"" uc = (l + bs - prefix_length - 1) * b"A" while len(plaintext) <= string_length: oracle_input = oracle(uc) for i in range(127): test = uc + plaintext + bytes([i]) if oracle(test)[l:l + bs] == oracle_input[l:l + bs]: uc = uc[1:] plaintext += bytes([i]) break return de_pkcs7(plaintext)
def break_ecb(oracle): bs = blocksize(oracle) l = len(oracle()) string_length = len_string(oracle) prefix_length = len_prefix(oracle) plaintext = b'' uc = (l + bs - len_prefix(oracle) - 1) * b'A' while len(plaintext) <= string_length: oracle_input = oracle(uc) for i in range(127): test = uc + plaintext + bytes([i]) if oracle(test)[l:l + bs] == oracle_input[l:l + bs]: uc = uc[1:] plaintext += bytes([i]) #print(chr(i), end = "", flush = True) break return de_pkcs7(plaintext)
def break_ecb(oracle): bs = blocksize(oracle) l = len(oracle()) string_length = len_string(oracle) plaintext = b'' prefix = (l + bs - 1) * b'A' while len(plaintext) <= string_length: oracle_prefix = oracle(prefix) for i in range(127): test = prefix + plaintext + bytes([i]) if oracle(test)[l:l + bs] == oracle_prefix[l:l + bs]: #if i < 10: # hack to break if non- # return plaintext # printable padding found prefix = prefix[1:] plaintext += bytes([i]) #print(chr(i), end = "", flush = True) break return de_pkcs7(plaintext)
def decrypt(cyphertext): cypher = AES.new(RANDOM_KEY, AES.MODE_ECB) return de_pkcs7(cypher.decrypt(cyphertext))
def receive_message(self, cyphertext: bytes) -> None: self.received_cyphertext = cyphertext iv = cyphertext[-16:] message = de_pkcs7(decrypt_aes_cbc(self._aes_key(), iv, cyphertext[:-16])) self.received_message = message
def decrypt_oracle(profile: bytes) -> dict[str, str]: cypher = AES.new(RANDOM_KEY, AES.MODE_ECB) profile = de_pkcs7(cypher.decrypt(profile)) return parse(profile.decode())
def decrypt_oracle(profile): cypher = AES.new(RANDOM_KEY, AES.MODE_ECB) profile = de_pkcs7(cypher.decrypt(profile)) return parse(profile.decode())
def decrypt(cyphertext: bytes) -> bytes: cypher = AES.new(RANDOM_KEY, AES.MODE_ECB) return de_pkcs7(cypher.decrypt(cyphertext))
for block in blocks: block = fixed_xor(block, vector) block = cypher.encrypt(block) cyphertext += block vector = block return cyphertext def decrypt_aes_cbc(cyphertext, key, iv): cypher = AES.new(key, AES.MODE_ECB) blocks = [cyphertext[i:i + len(key)] for i in range(0, len(cyphertext), len(key))] vector = iv plaintext = b'' for aesblock in blocks: block = cypher.decrypt(aesblock) plaintext += fixed_xor(block, vector) vector = aesblock return plaintext if __name__ == "__main__": cyphertext = b64decode(open("data/10.txt", "r").read()) key = bytes("YELLOW SUBMARINE", "utf8") iv = bytes(len(key)) plaintext = de_pkcs7(decrypt_aes_cbc(cyphertext, key, iv)) print(plaintext.decode())
def decrypt(self, cyphertext: bytes = None) -> bytes: cyphertext = cyphertext or self._message_buffer.pop() iv = cyphertext[-16:] message = de_pkcs7( decrypt_aes_cbc(self._aes_key(), iv, cyphertext[:-16])) return message