def cbc_encrypt(text, key, iv): ''' CBC encrypt text with initialization vector iv and key ''' block_length = len(iv) text = pkcs_padding(text, block_length) blocks = util.blocks(text, block_length) blocks[0] = util.string_xor(blocks[0], iv) blocks[0] = util.ecb_encrypt(blocks[0], key) for i in xrange(1, len(blocks)): blocks[i] = util.string_xor(blocks[i], blocks[i - 1]) blocks[i] = util.ecb_encrypt(blocks[i], key) return ''.join(blocks)
def cbc_encrypt(text, key, iv): ''' CBC encrypt text with initialization vector iv and key ''' block_length = len(iv) text = pkcs_padding(text, block_length) blocks = util.blocks(text, block_length) blocks[0] = util.string_xor(blocks[0], iv) blocks[0] = util.ecb_encrypt(blocks[0], key) for i in xrange(1, len(blocks)): blocks[i] = util.string_xor(blocks[i], blocks[i-1]) blocks[i] = util.ecb_encrypt(blocks[i], key) return ''.join(blocks)
def random_aes(text): ''' Encrypt text with AES 128 ECB with a chosen random key Random key is chosen once per code invocation ''' padding = b64decode(unknown_string) #base64 if 'key' not in random_aes.func_dict: random_aes.key = random_string(16) key = random_aes.key return ecb_encrypt(text + padding, key) # 加上padding加密
def encryption_oracle(data): prepad = Random.get_random_bytes(random.choice(range(5, 11))) postpad = Random.get_random_bytes(random.choice(range(5, 11))) padded = pkcs7_pad(prepad + data + postpad) key = keygen() if random.choice(range(2)) == 0: iv = keygen() return cbc_encrypt(data, key, iv) else: return ecb_encrypt(data, key)
def random_aes(text): ''' Encrypt text with AES 128 ECB with a chosen random key Random key is chosen once per code invocation ''' padding = b64decode(unknown_string) if 'key' not in random_aes.func_dict: random_aes.key = random_string(16) random_aes.prefix = random_string(random.randint(0, 100)) key = random_aes.key prefix = random_aes.prefix return ecb_encrypt(prefix + text + padding, key)
def ctr_decrypt(text, key='YELLOW SUBMARINE', nonce=0): ''' CTR decrypt a text using key and nonce ''' block_size = len(key) blocks = util.get_blocks(text, block_size) c = Counter() decrypted = '' for block in blocks: keystring = util.ecb_encrypt(c.next(), key) decrypted += util.string_xor(keystring, block) return decrypted
def encryption_oracle(text): # Append some random bytes both ways text = random_string(random.randint(5, 10)) + text + random_string(random.randint(5, 10)) iv = random_string(16) key = random_string(16) if random.randint(0, 1): # ECB return util.ecb_encrypt(text, key) else: # CBC return cbc_encrypt(text, key, iv)
def encryption_oracle(text): # Append some random bytes both ways text = random_string(random.randint(5, 10)) + text + random_string( random.randint(5, 10)) iv = random_string(16) #16bits key = random_string(16) if random.randint(0, 1): # ECB return util.ecb_encrypt(text, key) else: # CBC return cbc_encrypt(text, key, iv)
def encryption_oracle(text): # 明文两侧添加随机密文 text = random_string(random.randint(5, 10)) + text + random_string( random.randint(5, 10)) iv = random_string(16) key = random_string(16) if random.randint(0, 1): # ECB return util.ecb_encrypt(text, key) else: # CBC return cbc_encrypt(text, key, iv)
def encrypt(self, email): ''' Encrypt user profile with AES 128 ECB with a chosen random key Random key is chosen once per code invocation ''' key = self.key return ecb_encrypt(self.profile_for(email), self.key)
def encrypt(self, email): ''' Encrypt user profile with AES 128 ECB with a chosen random key Random key is chosen once per code invocation ''' key = self.key return ecb_encrypt(self.profile_for(email), self.key)