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 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())
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 _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 _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 _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)