From efebb67f3a41603e805825ab3677ce0e8221247a Mon Sep 17 00:00:00 2001 From: Maxime Vorwerk Date: Mon, 1 Jul 2024 17:51:53 +0200 Subject: [PATCH] Custom Encryption --- custom_encryption/custom_encryption.py | 65 ++++++++++++++++++++++++++ custom_encryption/enc_flag | 3 ++ custom_encryption/sol.py | 24 ++++++++++ 3 files changed, 92 insertions(+) create mode 100755 custom_encryption/custom_encryption.py create mode 100755 custom_encryption/enc_flag create mode 100755 custom_encryption/sol.py diff --git a/custom_encryption/custom_encryption.py b/custom_encryption/custom_encryption.py new file mode 100755 index 0000000..a205fd5 --- /dev/null +++ b/custom_encryption/custom_encryption.py @@ -0,0 +1,65 @@ +#!/home/maxime/.pyvenv/bin/python3 +from random import randint +import sys + + +def generator(g, x, p): + return pow(g, x) % p + + +def encrypt(plaintext, key): + cipher = [] + for char in plaintext: + cipher.append(((ord(char) * key*311))) + return cipher + + +def is_prime(p): + v = 0 + for i in range(2, p + 1): + if p % i == 0: + v = v + 1 + if v > 1: + return False + else: + return True + + +def dynamic_xor_encrypt(plaintext, text_key): + cipher_text = "" + key_length = len(text_key) + for i, char in enumerate(plaintext[::-1]): + key_char = text_key[i % key_length] + encrypted_char = chr(ord(char) ^ ord(key_char)) + cipher_text += encrypted_char + return cipher_text + + +def test(plain_text, text_key): + p = 97 + g = 31 + if not is_prime(p) and not is_prime(g): + print("Enter prime numbers") + return + a = randint(p-10, p) + b = randint(g-10, g) + print(f"a = {a}") + print(f"b = {b}") + u = generator(g, a, p) + v = generator(g, b, p) + key = generator(v, a, p) + b_key = generator(u, b, p) + shared_key = None + if key == b_key: + shared_key = key + else: + print("Invalid key") + return + semi_cipher = dynamic_xor_encrypt(plain_text, text_key) + cipher = encrypt(semi_cipher, shared_key) + print(f'cipher is: {cipher}') + + +if __name__ == "__main__": + message = sys.argv[1] + test(message, "trudeau") diff --git a/custom_encryption/enc_flag b/custom_encryption/enc_flag new file mode 100755 index 0000000..6b97a63 --- /dev/null +++ b/custom_encryption/enc_flag @@ -0,0 +1,3 @@ +a = 88 +b = 26 +cipher is: [97965, 185045, 740180, 946995, 1012305, 21770, 827260, 751065, 718410, 457170, 0, 903455, 228585, 54425, 740180, 0, 239470, 936110, 10885, 674870, 261240, 293895, 65310, 65310, 185045, 65310, 283010, 555135, 348320, 533365, 283010, 76195, 130620, 185045] diff --git a/custom_encryption/sol.py b/custom_encryption/sol.py new file mode 100755 index 0000000..c28c3e3 --- /dev/null +++ b/custom_encryption/sol.py @@ -0,0 +1,24 @@ +#!/home/maxime/.pyvenv/bin/python3 +from pwn import * + +with open("enc_flag", 'r') as f: + text = f.readlines() + a = int(text[0][4:].strip()) + b = int(text[1][4:].strip()) + cipher = text[2][11:].replace('[', '').replace(']', '').replace(' ', '').strip().split(',') + + p = 97 + g = 31 + text_key = "trudeau" + + key = pow(g, a*b, p) + semi_cipher = "" + for i in cipher: + i = int(i.strip()) + semi_cipher += chr(i // (311*key)) + plaintext = "" + for i, char in enumerate(semi_cipher): + plaintext += chr(ord(char) ^ ord(text_key[i%len(text_key)])) + plaintext = plaintext[::-1] + print(plaintext) +