def random_aes_mode_encryption(plaintext_bytes): key = random.getrandbits(MyAES.block_size * 8).to_bytes(MyAES.block_size, byteorder="big") saltedtext_bytes = randomword(random.randint(5, 10)).encode("utf-8") + plaintext_bytes + randomword(random.randint(5, 10)).encode("utf-8") coin = random.randint(0, 1) if coin == 0: # encrypt with ECB print("Real: ECB encrypted") return MyAES.new(key, MyAES.MODE_ECB, None).raw_encrypt(MyAES.pkcs7_pad(saltedtext_bytes)) else: # encrypt with CBC print("Real: CBC encrypted") iv = random.getrandbits(MyAES.block_size * 8).to_bytes(MyAES.block_size, byteorder="big") return MyAES.new(key, MyAES.MODE_CBC, iv).raw_encrypt(MyAES.pkcs7_pad(saltedtext_bytes))
def select_and_encrypt(): choice = random.randint(0, len(b64_strings) - 1) b64_string = b64_strings[choice] string_bytes = base64.b64decode(b64_string) iv_bytes = random.getrandbits(MyAES.block_size * 8).to_bytes( MyAES.block_size, byteorder="big") cipher = MyAES.new(key_bytes, MyAES.MODE_CBC, iv_bytes) ciphertext = cipher.raw_encrypt(MyAES.pkcs7_pad(string_bytes)) return (iv_bytes, ciphertext)
def aes_ecb_random_prefix_appended_secret(plaintext_bytes): # create a random prefix random_length = random.randint(min_random_length, max_random_length) tmp = bytearray() tmp.extend(random_bytes(random_length)) # add plaintext tmp.extend(plaintext_bytes) # append secret tmp.extend(base64.b64decode(secret)) cipher = MyAES.new(key, MyAES.MODE_ECB, None) return cipher.raw_encrypt(MyAES.pkcs7_pad(tmp))
def aes_cbc_fixed_prefix_fixed_suffix(input_bytes): input_type = type(input_bytes) assert input_type is bytes, "[convert_offending_char]: expecting input as bytes, found " + input_type.__name__ plaintext_bytes = bytearray() prefix_bytes = "comment1=cooking%20MCs;userdata=".encode("utf-8") suffix_bytes = ";comment2=%20like%20a%20pound%20of%20bacon".encode("utf-8") plaintext_bytes.extend(prefix_bytes) plaintext_bytes.extend(input_bytes) plaintext_bytes.extend(suffix_bytes) plaintext_bytes = MyAES.pkcs7_pad(plaintext_bytes) print(plaintext_bytes) # IV all zeroes cipher = MyAES.new(key_bytes, MyAES.MODE_CBC, bytes(MyAES.block_size)) return cipher.raw_encrypt(plaintext_bytes)
def aes_ecb_with_appended_secret(plaintext_bytes): temp = bytearray(plaintext_bytes) temp.extend(base64.b64decode(secret)) cipher = MyAES.new(key, MyAES.MODE_ECB, None) return cipher.raw_encrypt(MyAES.pkcs7_pad(temp))
def encrypt_profile(email_string): plaintext_bytes = MyAES.pkcs7_pad(bytes(email_string, encoding="utf-8")) cipher = MyAES.new(key_bytes, MyAES.MODE_ECB, None) return binascii.hexlify(cipher.raw_encrypt(plaintext_bytes))