class CBCPaddingOracle(object): def __init__(self, key, iv): self.cipher = AES(key) self.iv = iv def has_valid_padding(self, ciphertext): try: self.cipher.decrypt(ciphertext, mode=CBC(self.iv)) has_valid_padding = True except InvalidPaddingException: has_valid_padding = False return has_valid_padding
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 _init_cipher_from(self, secret, iv=None): secret_bytes = IntToBytes(secret).value() key = SHA1().hash(secret_bytes)[:self.BLOCK_SIZE] self.iv = RandomByteGenerator().value(self.BLOCK_SIZE) if iv is None\ else iv self.cipher = AES(key) self.cipher_mode = CBC(iv=self.iv)
class UserProfileGenerator(object): def __init__(self, key): self.cipher = AES(key) self.profile_template = 'email=%s&uid=%d&role=user' def _rand_uid(self): return random.randint(10, 99) def profile_for(self, email): if '&' in email or '=' in email: raise RuntimeError('email address has invalid characters!') uid = self._rand_uid() profile = self.profile_template % (email, uid) return self.cipher.encrypt(profile, mode=ECB())
class UserProfileGenerator(object): def __init__(self, key): self.cipher = AES(key) self.profile_template = 'email=%s&uid=%d&role=user' def _rand_uid(self): return random.randint(10,99) def profile_for(self, email): if '&' in email or '=' in email: raise RuntimeError('email address has invalid characters!') uid = self._rand_uid() profile = self.profile_template % (email, uid) return self.cipher.encrypt(profile, mode=ECB())
class UserProfileParser(object): def __init__(self, key, decryption_mode): self.cipher = AES(key) self.mode = decryption_mode def _unquote(self, text): text = text.replace('%3B', ';') text = text.replace('%3D', '=') return text def parse(self, encrypted_profile): profile_string = self.cipher.decrypt(encrypted_profile, mode=self.mode) return self._unquote(profile_string.bytes())
class UserProfileGenerator(object): PREFIX = 'comment1=cooking%20MCs;userdata=' SUFFIX = ';comment2=%20like%20a%20pound%20of%20bacon' def __init__(self, key, encryption_mode): self.cipher = AES(key) self.mode = encryption_mode def _quote(self, text): text = text.replace(';', '%3B') text = text.replace('=', '%3D') return text def profile_for(self, user_data): plaintext = '%s%s%s' % (self.PREFIX, user_data, self.SUFFIX) plaintext = self._quote(plaintext) return self.cipher.encrypt(plaintext, mode=self.mode)
class ECBEncryptionOracle(object): BLOCK_SIZE = 16 def __init__(self): key = RandomByteGenerator().value(self.BLOCK_SIZE) self.cipher = AES(key) self.trailing_string = self._decode_trailing_string() def _trailing_string(self): return 'Um9sbGluJyBpbiBteSA1LjAKV2l0aCBteSByYWctdG9wIGRvd24gc28gbXk' +\ 'gaGFpciBjYW4gYmxvdwpUaGUgZ2lybGllcyBvbiBzdGFuZGJ5IHdhdmluZy' +\ 'BqdXN0IHRvIHNheSBoaQpEaWQgeW91IHN0b3A/IE5vLCBJIGp1c3QgZHJvd' +\ 'mUgYnkK' def _decode_trailing_string(self): string = self._trailing_string() return Base64Decoder().decode(string) def encrypt(self, plaintext): plaintext += self.trailing_string return self.cipher.encrypt(plaintext, mode=ECB())
class UserProfileParser(object): FIELDS = ['email', 'uid', 'role'] def __init__(self, key): self.cipher = AES(key) def _parse(self, profile): tuples = profile.split('&') return dict(map(lambda item: item.split('='), tuples)) def _profile_has_valid_fields(self, profile): return set(profile.keys()) == set(self.FIELDS) def _validate(self, profile): if not self._profile_has_valid_fields(profile): raise RuntimeError('invalid profile!') def parse(self, encrypted_profile): profile_string = self.cipher.decrypt(encrypted_profile, mode=ECB()) profile = self._parse(profile_string.bytes()) self._validate(profile) return profile
def __init__(self, key, iv): self.cipher = AES(key) self.iv = iv
def __init__(self, key): self.cipher = AES(key) self.profile_template = 'email=%s&uid=%d&role=user'
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 _encrypt(self, message): key = RandomByteGenerator().value(BlockCipherMode.DEFAULT_BLOCK_SIZE) cipher = AES(key) mode = self._get_encryption_mode() return cipher.encrypt(message, mode=mode)
def __init__(self, key): self.cipher = AES(key)
def __init__(self): MatasanoChallenge.__init__(self) ciphertext = Base64Decoder().decode_file(self.FILE) self.plaintext = AES(self.KEY).decrypt(ciphertext, mode=ECB()).bytes()
def __init__(self): key = RandomByteGenerator().value(self.BLOCK_SIZE) self.cipher = AES(key) self.trailing_string = self._decode_trailing_string()
def _process_chunk(self, chunk): key = self._build_key_from_register() result = AES(key).encrypt(chunk).bytes() return [self.endianness().to_int(result[:byte_size]).value()]
def _encrypt(self, plaintexts): key = RandomByteGenerator().value(self.BLOCK_SIZE) aes = AES(key) return map(lambda text: aes.encrypt(text, mode=CTR(nonce=0)).bytes(), plaintexts)
def value(self): key = 'YELLOW SUBMARINE' ciphertext = Base64Decoder().decode_file(self.FILE) return AES(key).decrypt(ciphertext, mode=ECB()).bytes()
def __init__(self, key, encryption_mode): self.cipher = AES(key) self.mode = encryption_mode
def value(self): ciphertext = Base64Decoder().decode_file(self.FILE) return AES(self.KEY).decrypt(ciphertext, mode=CBC(self.IV)).bytes()