From 27c8a3f81225b447c1a3ac148bd3fd00b748f10b Mon Sep 17 00:00:00 2001 From: Roman Hergenreder Date: Tue, 26 Dec 2023 15:14:57 +0100 Subject: [PATCH] Day 23 --- .gitignore | 2 +- Day 23/decrypt.sage | 59 ++++++++++++++++++++++++++++++++++++++++++++ Day 23/enc.py | 41 ++++++++++++++++++++++++++++++ Day 23/output.txt | 5 ++++ Day 23/roll-rsa.zip | Bin 0 -> 1708 bytes 5 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 Day 23/decrypt.sage create mode 100644 Day 23/enc.py create mode 100644 Day 23/output.txt create mode 100644 Day 23/roll-rsa.zip diff --git a/.gitignore b/.gitignore index 3901713..09a9e72 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ __pycache__ *.pyc - +*.sage.py diff --git a/Day 23/decrypt.sage b/Day 23/decrypt.sage new file mode 100644 index 0000000..50a56b2 --- /dev/null +++ b/Day 23/decrypt.sage @@ -0,0 +1,59 @@ +import re +import random +from Crypto.Util.number import long_to_bytes + +values = {} +with open("output.txt", "r") as f: + lines = f.read().split("\n") + values["sum"] = int(lines[0]) + for line in lines[1:]: + match = re.match(r"(\w+)=(-?[0-9]+)", line) + values[match[1]] = int(match[2]) + +for y in range(0, 1000+1): + random.seed(y) + grade = random.choice([2,3]) + a = random.randint(9999, 999999) + b = random.randint(8888, 888888) + c = random.randint(7777, 777777) + if a + b + c == values["sum"]: + print("[+] Found y:", y) + break + +print("[+] a:", a) +print("[+] b:", b) +print("[+] c:", c) +print("[+] grade:", grade) + +x = var("x", domain="integer") +if grade == 2: + y_x = a*x**2+b*x+c +if grade == 3: + d = random.randint(6666, 666666) + y_x = a*x**3+b*x**2+c*x+d + print("[+] d:", d) +print("[+] polynome:", y_x) + +N = values["N"] +hint = values["hint"] + +p = None +q = var("q", domain="integer") +for x in range(0,1000+1): + eq = (N/q)^3 - q^8 + y_x(x=x) == hint + solution = solve([eq], q, solution_dict=True) + if solution and q in solution[0]: + print("[+] Found x:", x) + q = int(solution[0][q]) + p = N//q + break + +if p is None: + print("[-] Could not find p and q") + exit() + +phi_n = (p - 1) * (q - 1) +d = pow(values["e"], -1, phi_n) +pt = pow(values["encrypted"], d, values["N"]) +flag = long_to_bytes(pt).decode() +print("[+] Flag:", flag) \ No newline at end of file diff --git a/Day 23/enc.py b/Day 23/enc.py new file mode 100644 index 0000000..a3e2946 --- /dev/null +++ b/Day 23/enc.py @@ -0,0 +1,41 @@ +from Crypto.Util.number import * +from sage.all import * +from secret import FLAG, x, y +import random + +# D = {x∈ℕ | 0 ≤ x ≤ 1000} +# D = {y∈ℕ | 0 ≤ y ≤ 1000} + +def enc(flag, polynomial_function): + p = getStrongPrime(512) + q = getStrongPrime(512) + N = p * q + e = 65537 + hint = p**3 - q**8 + polynomial_function(x=x) + encrypted = pow(bytes_to_long(flag), e, N) + print(f"{N=}") + print(f"{e=}") + print(f"{hint=}") + print(f"{encrypted=}") + + +def generate_polynomial_function(seed): + x = SR.var("x") + random.seed(seed) + grade = random.choice([2,3]) + a = random.randint(9999, 999999) + b = random.randint(8888, 888888) + c = random.randint(7777, 777777) + + if grade == 2: + y_x = a*x**2+b*x+c + if grade == 3: + d = random.randint(6666, 666666) + y_x = a*x**3+b*x**2+c*x+d + + print(a+b+c) + return y_x + + +y_x = generate_polynomial_function(y) +enc(FLAG.encode(), y_x) \ No newline at end of file diff --git a/Day 23/output.txt b/Day 23/output.txt new file mode 100644 index 0000000..290d277 --- /dev/null +++ b/Day 23/output.txt @@ -0,0 +1,5 @@ +1709262 +N=143306145185651132108707685748692789834391223254420921250753369412889732941905250889012412570851623535344424483564532684976892052830348014035303355261052741504390590825455129003262581199117432362073303998908141781601553213103109295898711066542593102305069363965164592663322089299134520383469241654273153506653 +e=65537 +hint=-367367861727692900288480576510727681065028599304486950529865504611346573250755811691725216308460956865709134086848666413510519469962840879406666853346027105744846872125225171429488388383598931153062856414870036460329519241754646669265989077569377130467115317299086371406081342249967666782962173513369856861858058676451390037278311316937161756731165929187543148639994660265783994439168583858109082136915810219786390452412584110468513829455001689531028969430907046738225668834761412112885772525079903072777443223873041260072918891696459905352737195384116938142788776947705026132197185926344278041831047013477983297898344933372775972141179163010102537733004410775357501267841845321271140399200044741656474378808452920297777911527159803159582800816951547394087190043792625664885536154225227819735800442814065528155407746556297892931242208688533313054308779657788077807340045465701247210553988059519291363634253248268722975827616752514688291723712069675405995149499947239454505797412122124933836396842943540518521648803348207619354854290787969076059265170474203200482079680136404766877617679652611682327535174212016390608658107555103054183393719700027186913354158961245998591486268846852581402900857595817303811471853325463202817521164757 +encrypted=72792762778232160989381071629769766489971170790967414271032682193723004039685063639675377805724567838635943988752706743932748347933013530918279285456553768626331874756049006544553546268049053833014940495217179504587162478219970564159885619559723778613379425375733026859684952880028997538045791748027936366062 \ No newline at end of file diff --git a/Day 23/roll-rsa.zip b/Day 23/roll-rsa.zip new file mode 100644 index 0000000000000000000000000000000000000000..8fb3f66cca459cae448eaf893dd1db0469ebff1f GIT binary patch literal 1708 zcmZ`)c{JOJ7XHzRXqpm)QbJpymXRt^s)|&!$taRg6FQ}IFSI-~a$9 z1CZ~EzH~CmB915kz|$Q7ptL=`6n+5}8GkLGpEYI)>zC%PsnkW|^)zC2*ZBw5lbj8^l6g&}3Y(ta%Ex7wT5=}eChE!n745NvWiRhBMjQOz9dIi`QKiMp+37ygN zhJ>@4ic<%~Q+n1Gr;R@7Tu~E6ZgWetw`0SkpZ26r>E3pZgRY3nT^?@q9Gz7rDO=P+ z8%+#kLmEBW{Iel}WZ%^M3XK_o%HVnum&}<~NoSeFU3#a309hCE5CW}3&qz&fqsJ`y zs7ZsaYRot8H(xms{x5e%chQ&bR|qyE+foOC0RXZscSI~DGM0j(P$@=*ZdqPLC{}{| z95h7VQe}aTDBvtH%7+8x<&c@{NV%iv%zc(3@VKT#eqW z6o>}x5-X`@vi;*#tj2G8UI)vK^&KlTB{Z8vyQUTk>A{R=NXwWyd-KX!2eWCH-Za!*Ta0w6K_tzwkl<8My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