def valid_padding(plaintext): unpad = set2_ch10.unpad(plaintext) padded = set2_ch9.padding(unpad, 16) if plaintext == padded: return unpad else: raise ValueError('Bad padding')
def encryption_oracle(message): key = random_16_bytes() message = random_append() + message + random_append() # random choose either cbc or ebc if randint(0,1): message = set2_ch9.padding(message, 16) return set1_ch7.encrypt(key, message) #ebc else: iv = random_16_bytes() return set2_ch10.cbc_encrypt(key, message, iv) # cbc
def cbc_encrypt(key, plaintext, iv): plaintext = set2_ch9.padding(plaintext, 16) blocks = list(set1_ch6.chunks(plaintext, 16)) prev_encrypt = iv ciphertext = [] for b in blocks: xored = set1_ch2.xor(b, prev_encrypt) prev_encrypt = set1_ch7.encrypt(key, xored) ciphertext.append(prev_encrypt) return b"".join(ciphertext)
def encrypt_profile(key, profile): prof_padded = set2_ch9.padding(profile, 16) encrypted = set1_ch7.encrypt(key, prof_padded) return encrypted
def byte_ecb_encrypt(message, key): unknown_string = b'Um9sbGluJyBpbiBteSA1LjAKV2l0aCBteSByYWctdG9wIGRvd24gc28gbXkgaGFpciBjYW4gYmxvdwpUaGUgZ2lybGllcyBvbiBzdGFuZGJ5IHdhdmluZyBqdXN0IHRvIHNheSBoaQpEaWQgeW91IHN0b3A/IE5vLCBJIGp1c3QgZHJvdmUgYnkK' message += codecs.decode(unknown_string, 'base64') message = set2_ch9.padding(message, 16) return set1_ch7.encrypt(key, message)