def detect_ecb(oracle, keysize): """Detect ECB encryption.""" c = chunks(oracle(b"A" * 100), keysize) if count_repeats(c) > 0: return True else: return False
def decrypt(self, ciphertext): """Decrypt ciphertext with key using AES-128-CBC.""" c = chunks(ciphertext, self.keysize) plaintext = xor(super().decrypt(c[0]), self.iv) for i in range(0, len(c) - 1): plaintext += xor(c[i], super().decrypt(c[i + 1])) return plaintext
def encrypt(self, plaintext): """Encrypt ciphertext with key using AES-128-CBC.""" p = chunks(plaintext, self.keysize) c = super().encrypt(xor(p[0], self.iv)) ciphertext = c for chunk in p[1:]: c1 = super().encrypt(xor(c, chunk)) ciphertext += c1 c = c1 return ciphertext
def decrypt(self, ciphertext): """Decrypt ciphertext with key using AES-128-CTR.""" counter = 0 plaintext = b"" c = chunks(ciphertext, 16) for chunk in c: n = len(chunk) keystream = ECB(self.key).encrypt(self.nonce + pack("Q", counter)) plaintext += xor(keystream[0:n], chunk) counter += 1 return plaintext
def __init__(self, ciphertexts): """List of ciphertexts.""" self.ciphertexts = ciphertexts self.keysize = 16 self.scores = [] cnt = 1 for ciphertext in self.ciphertexts: c = chunks(line, self.keysize) self.scores.append((cnt, count_repeats(c))) cnt += 1 # most likely ciphertext encrypted with AES-128-ECB self.ciphertext = max(self.scores, key=lambda x: x[1])[0]