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 main(): ciphertext_string = "L77na/nrFsKvynd6HzOoG7GHTLXsTVu9qvY/2syLXzhPweyyMTJULu/6/kXX0KSvoOLSFQ==" ciphertext = base64.b64decode(ciphertext_string) counter = struct.pack("<QQ", 0, 0) plaintext = MyAES.new(b"YELLOW SUBMARINE", MyAES.MODE_CTR, counter).raw_decrypt(ciphertext) print(plaintext) ciphertext = MyAES.new(b"YELLOW SUBMARINE", MyAES.MODE_CTR, counter).raw_encrypt(b"Another test, foo bar.") plaintext = MyAES.new(b"YELLOW SUBMARINE", MyAES.MODE_CTR, counter).raw_decrypt(ciphertext) print(plaintext)
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 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 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 main(): oneline = "" with open("data/7.txt", "r") as file: for line in file.readlines(): oneline += line.strip("\n") ciphertext = base64.b64decode(oneline) key = b"YELLOW SUBMARINE" plaintext = MyAES.new(key, MyAES.MODE_ECB, None).raw_decrypt(ciphertext) print(plaintext.decode("utf-8"))
def main(): key = b"YELLOW SUBMARINE" b64_encoded = "" with open("data/10.txt", "r") as file: for line in file.readlines(): b64_encoded += line.strip("\n") ciphertext = base64.b64decode(b64_encoded) plaintext = MyAES.new(key, MyAES.MODE_CBC, bytes(MyAES.block_size)).raw_decrypt(ciphertext) print(plaintext.decode("utf-8"))
def is_token_admin(input_bytes): cipher = MyAES.new(key_bytes, MyAES.MODE_CBC, bytes(MyAES.block_size)) plaintext_bytes = valid_pkcs7_padding(cipher.raw_decrypt(input_bytes), strip=True) print(plaintext_bytes) plaintext = plaintext_bytes.decode("utf-8", errors="ignore") parameter_list = plaintext.split(";") for parameter in parameter_list: values_list = parameter.split("=") if len(values_list) == 2: name = values_list[0] value = values_list[1] if name == "admin" and value == "true": return True else: continue return False
def decrypt(iv_bytes, ciphertext_bytes): cipher = MyAES.new(key_bytes, MyAES.MODE_CBC, iv_bytes) plaintext_bytes = cipher.raw_decrypt(ciphertext_bytes) return valid_pkcs7_padding(plaintext_bytes, strip=False)
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 decrypt_token(token_bytes): ciphertext_bytes = binascii.unhexlify(token_bytes) cipher = MyAES.new(key_bytes, MyAES.MODE_ECB, None) plaintext = cipher.raw_decrypt(ciphertext_bytes).decode("utf-8", errors="ignore") parsing_routine(plaintext)
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))