def prepare(self, input_string): comment1 = b'comment1=cooking%20MCs;userdata=' comment2 = b';comment2=%20like%20a%20pound%20of%20bacon' table = str.maketrans(dict.fromkeys('=;')) clean_input_string = input_string.decode().translate(table) plain_text_pad = pad_pkcs(comment1 + clean_input_string.encode() + comment2, 16) return aes128_cbc_encrypt(plain_text_pad, self.iv, self.key)
def encrypt(self, plain_text): plain_text_pad = pad_pkcs(plain_text + b64decode(self.data), 16) cipher_text = aes128_ecb_encrypt(plain_text_pad, self.key) return cipher_text
def encrypt(self, plain_text): # the difference between this challenge and challenge12 are the random bytes plain_text_pad = pad_pkcs( self.random_bytes + plain_text + b64decode(self.data), 16) cipher_text = aes128_ecb_encrypt(plain_text_pad, self.key) return cipher_text
ctr_substitution[i]['cipher'], key_stream) print(i, ctr_substitution[i]['plain'], len(ctr_substitution[i]['plain']), "refined:", ctr_substitution[i]['refined']) print("\n", count_not_refined(ctr_substitution), "texts to be refined") # At this point, the beginning of sentences can be read, but the end is definitely not readable. # We can conclude that the first block is correctly, and the followings are deviated due the padding chars. # Let's try to refine our results, starting by block number two: ctr_substitution = refine_block(ctr_substitution, 1) # 1 is the block number two key_stream_refined = b'' for i in range(len(ctr_substitution)): if ctr_substitution[i]['refined']: pad_plain = pad_pkcs(ctr_substitution[i]['plain'], 16) # pad any refined text correctly # plain xor cipher = keystream key_stream_refined = xor_bytes( pad_plain, ctr_substitution[i]['cipher']) # Calculate keystream again break # The keystream calculated from refined only sizes two blocks. # For the last block we use the keystream calculated before key_stream = key_stream_refined[:32] + key_stream[32:] # decrypt using the new keystream print("\nSecond attempt using the new keystream:") print("Keystream 2:", key_stream) for i in range(len(ctr_substitution)): if not ctr_substitution[i]['refined']:
def encrypt_profile(self, email): '''Create profile from email and encrypt it using random key''' profile = profile_factory.profile_for(email) formatted_profile_pad = pad_pkcs(profile, 16) cipher_text = aes128_ecb_encrypt(formatted_profile_pad, self.key) return cipher_text
# Implement PKCS#7 padding from utils import pad_pkcs if __name__ == '__main__': x = b"YELLOW SUBMARINE" y = pad_pkcs(x, 20) print(y)
def encrypt(self, str): str_pad = pad_pkcs(str, 16) cipher_text = aes128_cbc_encrypt(str_pad, self.iv, self.key) return cipher_text, self.iv