Roman Hergenreder 4 months ago
parent
commit
27c8a3f812
5 changed files with 106 additions and 1 deletions
  1. 1 1
      .gitignore
  2. 59 0
      Day 23/decrypt.sage
  3. 41 0
      Day 23/enc.py
  4. 5 0
      Day 23/output.txt
  5. BIN
      Day 23/roll-rsa.zip

+ 1 - 1
.gitignore

@@ -1,3 +1,3 @@
 __pycache__
 *.pyc
-
+*.sage.py

+ 59 - 0
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)

+ 41 - 0
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)

+ 5 - 0
Day 23/output.txt

@@ -0,0 +1,5 @@
+1709262
+N=143306145185651132108707685748692789834391223254420921250753369412889732941905250889012412570851623535344424483564532684976892052830348014035303355261052741504390590825455129003262581199117432362073303998908141781601553213103109295898711066542593102305069363965164592663322089299134520383469241654273153506653
+e=65537
+hint=-367367861727692900288480576510727681065028599304486950529865504611346573250755811691725216308460956865709134086848666413510519469962840879406666853346027105744846872125225171429488388383598931153062856414870036460329519241754646669265989077569377130467115317299086371406081342249967666782962173513369856861858058676451390037278311316937161756731165929187543148639994660265783994439168583858109082136915810219786390452412584110468513829455001689531028969430907046738225668834761412112885772525079903072777443223873041260072918891696459905352737195384116938142788776947705026132197185926344278041831047013477983297898344933372775972141179163010102537733004410775357501267841845321271140399200044741656474378808452920297777911527159803159582800816951547394087190043792625664885536154225227819735800442814065528155407746556297892931242208688533313054308779657788077807340045465701247210553988059519291363634253248268722975827616752514688291723712069675405995149499947239454505797412122124933836396842943540518521648803348207619354854290787969076059265170474203200482079680136404766877617679652611682327535174212016390608658107555103054183393719700027186913354158961245998591486268846852581402900857595817303811471853325463202817521164757
+encrypted=72792762778232160989381071629769766489971170790967414271032682193723004039685063639675377805724567838635943988752706743932748347933013530918279285456553768626331874756049006544553546268049053833014940495217179504587162478219970564159885619559723778613379425375733026859684952880028997538045791748027936366062

BIN
Day 23/roll-rsa.zip