def test_challenge10(self): ciphertext = base64_to_bytes(read('10.txt')) key = 'YELLOW SUBMARINE' iv = '\x00' * 16 plaintext = cbc_decrypt(ciphertext, key, iv) ciphertext = cbc_encrypt(plaintext, key, iv) plaintext2 = cbc_decrypt(ciphertext, key, iv) self.assertEquals(plaintext, plaintext2)
def verify_decrypt(enc): print(enc) if enc == None or enc == "": return False try: plain = crypto.cbc_decrypt(enc.decode("hex"), master_key) return True except: return False
def echo(self, message): s = pow(self.A, self.b, self.p) key = Sha1Hash().update(to_bytes_le(s)).digest()[:16] ciphertext = message[:-16] iv = message[-16:] plaintext = crypto.cbc_decrypt(ciphertext, key, iv) self.messages.append(plaintext) iv = crypto.iv() return crypto.cbc_encrypt(plaintext, key, iv) + iv
def echo(self, message): response = self.receiver.echo(message) # Our malicious middleman has returned p to Alice. # Alice uses this value as B. # Alice generates her key with: (B ** a) % p # Because B == p, p ** a will always be an exact multiple of p so the # mod value will be 0 regardless of the value of a. key = Sha1Hash().digest()[:16] ciphertext = message[:-16] iv = message[-16:] plaintext = crypto.cbc_decrypt(ciphertext, key, iv) self.messages.append(plaintext) return response
def send(self, receiver, message): self.messages.append(message) B = receiver.handshake(self.p, self.g, self.A) s = pow(B, self.a, self.p) key = Sha1Hash().update(to_bytes_le(s)).digest()[:16] iv = crypto.iv() ciphertext = crypto.cbc_encrypt(message, key, iv) + iv returned_message = receiver.echo(ciphertext) returned_ciphertext = returned_message[:-16] returned_iv = returned_message[-16:] decrypted = crypto.cbc_decrypt( returned_ciphertext, key, returned_iv ) return decrypted
def is_admin(ciphertext, key, iv): decrypted = cbc_decrypt(ciphertext, key, iv) return ';admin=true;' in decrypted
def is_padding_valid(ciphertext, key, iv): try: cbc_decrypt(ciphertext, key, iv) return True except PaddingError: return False
def is_admin_cbc(ciphertext, key): decrypted = crypto.cbc_decrypt(ciphertext, key, key) if not all(ord(c) < 128 for c in decrypted): raise ValueError('Invalid message ' + decrypted) return ';admin=true;' in decrypted