def value(self): random_generator = RandomByteGenerator() key = random_generator.value(self.BLOCK_SIZE) iv = random_generator.value(self.BLOCK_SIZE) ciphertext = AES(key).encrypt(self.plaintext, mode=CBC(iv)) oracle = CBCPaddingOracle(key, iv) return CBCPaddingOracleAttack(oracle).value(ciphertext, iv)
class RandomECB_CBCEncrypter(object): def __init__(self, block_size): self.block_size = block_size self.random_generator = RandomByteGenerator() def get_mode(self): # To use after encryption in order to confirm that the oracle works. return self.mode.name() def _prepare_plaintext(self, plaintext): header = self.random_generator.value(random.randint(5,10)) footer = self.random_generator.value(random.randint(5,10)) return '%s%s%s' % (header, plaintext, footer) def _get_random_mode(self): if random.random() >= 0.5: mode = ECB(block_size=self.block_size) else: random_iv = self.random_generator.value(self.block_size) mode = CBC(iv=random_iv, block_size=self.block_size) return mode def encrypt(self, plaintext): plaintext = self._prepare_plaintext(plaintext) key = self.random_generator.value(self.block_size) self.mode = self._get_random_mode() return AES(key).encrypt(plaintext, mode=self.mode)
def value(self): random_generator = RandomByteGenerator() key = random_generator.value(self.BLOCK_SIZE) iv = random_generator.value(self.BLOCK_SIZE) ciphertext = AES(key).encrypt(self.plaintext, mode=CBC(iv)) oracle = CBCPaddingOracle(key, iv) return CBCPaddingOracleAttack(oracle).value(ciphertext, iv)
def _build_padding(self, size): byte_generator = RandomByteGenerator() padding = byte_generator.value(size) return padding.replace('\x00', byte_generator.value(1))