def encryption_oracle(plaintext, key, randomdata): buff = "Um9sbGluJyBpbiBteSA1LjAKV2l0aCBteSByYWctdG9wIGRvd24gc28gbXkg\ aGFpciBjYW4gYmxvdwpUaGUgZ2lybGllcyBvbiBzdGFuZGJ5IHdhdmluZyBq\ dXN0IHRvIHNheSBoaQpEaWQgeW91IHN0b3A/IE5vLCBJIGp1c3QgZHJvdmUg\ YnkK" return aes_ecb_encrypt( paddpkcs7(randomdata + plaintext + base64.b64decode(buff), 16), key)
def aes_ctr(text, key): resulttext = b'' for i in range(0, len(text), 16): count = (constants.nonce.to_bytes(8, 'little') + (i // 16).to_bytes(8, 'little')) keystream = set2.aes_ecb_encrypt(count, key) block = text[i:i + 16] resulttext += set1.xor(block, keystream[:len(block)]) return resulttext
def aes_ctr(binary_blob, aes_key, nonce=0): ''' Implements stream cipher mode AES ''' nonce = nonce.to_bytes(8, byteorder='little') keystream = b'' counter = 0 while len(keystream) < len(binary_blob): keystream += aes_ecb_encrypt(nonce + counter.to_bytes(8, byteorder='little'), aes_key) counter += 1 return bytes([a ^ b for a, b in zip(binary_blob, keystream)])
def read_ctr_cipher(ciphertext, key, offset, length, nonce=0): ''' seeks into the cipherteext using the offset and returns the decrypted info with length specified ''' nonce = nonce.to_bytes(8, byteorder='little') keystream = b'' counter = 0 while len(keystream) < offset + length: keystream += aes_ecb_encrypt( nonce + counter.to_bytes(8, byteorder='little'), key) counter += 1 return bytes([ a ^ b for a, b in zip(cipher[offset:offset + length], keystream[offset:]) ])
def edit_ctr_cipher(ciphertext, key, offset, newtext, nonce=0): ''' seeks into the ciphertext using the offset and replaces part of the ciphertext with the encrypted newtext ''' nonce = nonce.to_bytes(8, byteorder='little') keystream = b'' counter = 0 while len(keystream) < offset + len(newtext): keystream += aes_ecb_encrypt( nonce + counter.to_bytes(8, byteorder='little'), key) counter += 1 keystream = keystream[offset:] newcipher = bytes([a ^ b for a, b in zip(newtext, keystream)]) ciphertext = ciphertext[:offset] + newcipher + ciphertext[offset + len(newcipher):] return ciphertext
def aes_ctr_encrypt_block(block, key): return aes_ecb_encrypt(block, key)
def encryption_oracle(cleartext, key): return aes_ecb_encrypt(paddpkcs7(profile_for(cleartext), 16), key)