Skip to content
RedBlueStrategy
Go back

Alice Doesn't Know Algebra — Codeby.games

Alice Doesn't Know Algebra — Codeby.games

Initial Data

Task link Alice doesn’t know algebra

Description Alice took a long time to decide which exponent to choose when encrypting the key. In the end, she decided to use both…

Difficulty Medium

Server 62.173.***.***:11***

Files Download Alice_doesnt_know_algebra.zip

Solution

Input Analysis

Unpack the task.zip archive. It contains a single file, cipher.py. Let’s look at its contents:

#!/usr/bin/ python3

from binascii import unhexlify
from Crypto.Util.number import bytes_to_long, getPrime , long_to_bytes

intro = ("""   

          !GPPPPPPPP?                                                                   
          P@@@@@@@@@#                                                                   
       @@@7      ^!~J@@@@@@P                                                            
   .!!7PGG~      :^:!###GPGY!!!                                                         
   J@@@   .^:::::::::^^^   ~@@@.                                                        
G##J^^^::::^:!#&&7:^^~~~:..:^^^G###########BB#########BBB##########BB###!               
@@@J   ^^^~7!?&&&Y!7!~~~^:^.   #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@&&&&&&@Y...            
&@@J   :::G@@&   G@@&~~~^::::::^~~^::^^~^~~~~~~~^:^^^^^^~~^::::^^.      ?@@&            
&@@5 ..:::G@@#   G@@&~~~^:::^^^^^^^^^^^~~^^^^^^^^^^:::::^:!P55^:::......5@@@            
&@@P::::::G@@#   G@@&~~~^::^^^^^~^~!!!!!!^::::::^~~^::::::?@@@!::^~^~!!~G@@@            
@@@G:^^:::^!~7&&&J~!~^^^:::^~~~&@@@@@@@@@@&@@@@@&&@#777#&&G...B&@P:::777B@@@            
G##P???^:::::~&&&7::::::^^^~YJJB################@@@&7?7&@@B   &@@B^^^7?7B@@@            
   J@@@~^^::::::::::::::^~~5@@@.                G@@&777&@@B   &@@#77777!B@@@            
   .!!7B##?^^^^^^^^^7BBB###P!!!                 :!!?#&&J!!^   ^!!Y&&&&&&5!!~            
       &@@P!7777777!Y@@@@@@5                       .@@@:         !@@@@@@?               
          P@@@@@@@@@#                                                                   
          !GPPPPPPPP?                                                                   
          
          """)

print(intro)

FLAG = open("SuperSecretflag.txt" , "rb").read()
KEY = open("SuperSecretkey.txt" , "rb").read()

class Cipher():
    def __init__(self):
        self.p = getPrime(256)
        self.q = getPrime(256)
        self.e_1 = getPrime(256)
        self.e_2 = getPrime(256)
        self.N = self.p * self.q
        print(f"N: {self.N}")
        print()

    def encrypt(self, pt):
        if len(pt) == 0:
            return 0
        ct_1 = b""
        ct_2 = b""
        pt_num = bytes_to_long(pt)
        
        ct_1 = long_to_bytes(pow(pt_num , self.e_1 , self.N))
        ct_2 = long_to_bytes(pow(pt_num , self.e_2 , self.N))

        return ct_1, ct_2
 
    def decrypt(self,ct):
        return self.encrypt(ct)
    
def check_input(phrase):
    while True:
        try:
            your_input = int(input(phrase))
            break
        except:
            print("Invalid int format")
    return your_input

def check_input_hex(phrase):
    while True:
        try:
            your_input = unhexlify(input(phrase))
            break
        except:
            print("Invalid hex format")
    return your_input.hex()

cipher = Cipher()

while True:

    print()
    print("[1] - Encrypt text")
    print("[2] - Decrypt text")
    print("[3] - Get the flag")
    print("[4] - Get the encrypted key")
    print("[0] - Exit")
    print()

    your_input = check_input("Select an action: ", )
    print()

    if your_input == 1:

        pt = check_input_hex("Enter the plaintext in hex format: ")

        ct_1, ct_2 = cipher.encrypt(bytes.fromhex(pt))
        print(f"First ciphertext: {ct_1.hex()}") 
        print(f"Second ciphertext: {ct_2.hex()}")  

    elif your_input == 2:

        ct = check_input_hex("Enter the ciphertext in hex format: ")
        pt_1, pt_2 = cipher.decrypt(bytes.fromhex(ct))
        print(f"Plaintext: {pt_1.hex()}")

    elif your_input == 3:
        input_password = input("Enter the key: ")

        if input_password.encode() == KEY:
            print(FLAG)
        else:
            print("Wrong key!")    

    elif your_input == 4:
        print("Key:")
        enc_key_1, enc_key_2 = cipher.encrypt(KEY)
        print(enc_key_1.hex())
        print("Or...")
        print(enc_key_2.hex())

    elif your_input == 0:
        print("Game over . . .")
        exit()cipher.py

Connect to the remote server:

[amyasnikov@ubuntu:~]$ nc 62.173.***.*** 11***


          !GPPPPPPPP?
          P@@@@@@@@@#
       @@@7      ^!~J@@@@@@P
   .!!7PGG~      :^:!###GPGY!!!
   J@@@   .^:::::::::^^^   ~@@@.
G##J^^^::::^:!#&&7:^^~~~:..:^^^G###########BB#########BBB##########BB###!
@@@J   ^^^~7!?&&&Y!7!~~~^:^.   #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@&&&&&&@Y...
&@@J   :::G@@&   G@@&~~~^::::::^~~^::^^~^~~~~~~~^:^^^^^^~~^::::^^.      ?@@&
&@@5 ..:::G@@#   G@@&~~~^:::^^^^^^^^^^^~~^^^^^^^^^^:::::^:!P55^:::......5@@@
&@@P::::::G@@#   G@@&~~~^::^^^^^~^~!!!!!!^::::::^~~^::::::?@@@!::^~^~!!~G@@@
@@@G:^^:::^!~7&&&J~!~^^^:::^~~~&@@@@@@@@@@&@@@@@&&@#777#&&G...B&@P:::777B@@@
G##P???^:::::~&&&7::::::^^^~YJJB################@@@&7?7&@@B   &@@B^^^7?7B@@@
   J@@@~^^::::::::::::::^~~5@@@.                G@@&777&@@B   &@@#77777!B@@@
   .!!7B##?^^^^^^^^^7BBB###P!!!                 :!!?#&&J!!^   ^!!Y&&&&&&5!!~
       &@@P!7777777!Y@@@@@@5                       .@@@:         !@@@@@@?
          P@@@@@@@@@#
          !GPPPPPPPP?


N: 5528421003787152797780905532338533644132966573280985037688431655042011332476181014684652947059175828063517292266175422491347923201331594191535914968309911
e_1: 69711700699061079421281917335076487671413484781984251661406882503217129427749
e_2: 104216655613664737083708985163582773085116384037288226397753589821256726375927


[1] - Encrypt text
[2] - Decrypt text
[3] - Get the flag
[4] - Get the encrypted key
[0] - Exit

Select an action: 4

Key:
0b28b5908bcd409b69bd63e664149de5776428c6ac567b27f5b21f6dcb16c0d54338cb92688654687a67381720f7808dcbd68da00f0e97566e60b1c960b13f62
Or...
094a5fee3aec8f4b508238e311f6ae07e3a41a1111bd989673d38dec767bd0509e3f4d04bd2c645a2d4f266cd0b6bc8eaaa5a13fd5c33236d20c531476e32365

[1] - Encrypt text
[2] - Decrypt text
[3] - Get the flag
[4] - Get the encrypted key
[0] - Exit

Select an action: 0

Game over . . .server-output.log

Reasoning

Known:

  • N — modulus
  • e1 — first exponent
  • e2 — second exponent
  • c1 = F ^ e1 mod N — first encrypted message
  • c2 = F ^ e2 mod N — second encrypted message

Unknown:

  • F — the flag

Multiply c1 and c2 by the powers p and q: (c1 ^ p) * (c2 ^ q) = F ^ (p * e1 + q * e2) mod N

F == (c1 ^ p) * (c2 ^ q) if p * e1 + q * e2 == 1

Use the property: d ^ s = d ^ (s * (N ^ k)) mod N for any d, s, N, k

That is, p * e1 + q * e2 = N ^ k That is, gcd(p, q, e1, e2) = 1

Find p and q from the equation above, and then F: F = (c1 ^ p) * (c2 ^ q)

solve.py Script

Let’s write a script to find the key according to the reasoning above:

#!/usr/bin/ python3

from Crypto.Util.number import long_to_bytes

def egcd(p, q):
    if p == 0:
        return (q, 0, 1)
    else:
        (gcd, u, v) = egcd(q % p, p)
        return (gcd, v - (q // p) * u, u)

n = 5528421003787152797780905532338533644132966573280985037688431655042011332476181014684652947059175828063517292266175422491347923201331594191535914968309911
e_1 = 69711700699061079421281917335076487671413484781984251661406882503217129427749
e_2 = 104216655613664737083708985163582773085116384037288226397753589821256726375927
enc_key_1 = 0x0B28B5908BCD409B69BD63E664149DE5776428C6AC567B27F5B21F6DCB16C0D54338CB92688654687A67381720F7808DCBD68DA00F0E97566E60B1C960B13F62
enc_key_2 = 0x094A5FEE3AEC8F4B508238E311F6AE07E3A41A1111BD989673D38DEC767BD0509E3F4D04BD2C645A2D4F266CD0B6BC8EAAA5A13FD5C33236D20C531476E32365

g, p, q = egcd(e_1, e_2)
ct_1 = pow(enc_key_1, p, n)
ct_2 = pow(enc_key_2, q, n)

t = (ct_1 * ct_2) % n
t = long_to_bytes(t).decode()

print(f"g={g}")
print(f"p={p}")
print(f"q={q}")
print(f"t={t}")solve.py

Run solve.py:

[amyasnikov@ubuntu:~]$ python3 ./solve.py

g=1
p=50876718808717398630833481663924268106628133421247601105153259371288643274534
q=-34032013148563943495646408603594819163516768429468729511646574596529289866795
t=it'5_v3ry_*****************solve-output.log

Getting the Flag

Connect to the server and enter the recovered key:

[amyasnikov@ubuntu:~]$ nc 62.173.***.*** 11***


          !GPPPPPPPP?
          P@@@@@@@@@#
       @@@7      ^!~J@@@@@@P
   .!!7PGG~      :^:!###GPGY!!!
   J@@@   .^:::::::::^^^   ~@@@.
G##J^^^::::^:!#&&7:^^~~~:..:^^^G###########BB#########BBB##########BB###!
@@@J   ^^^~7!?&&&Y!7!~~~^:^.   #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@&&&&&&@Y...
&@@J   :::G@@&   G@@&~~~^::::::^~~^::^^~^~~~~~~~^:^^^^^^~~^::::^^.      ?@@&
&@@5 ..:::G@@#   G@@&~~~^:::^^^^^^^^^^^~~^^^^^^^^^^:::::^:!P55^:::......5@@@
&@@P::::::G@@#   G@@&~~~^::^^^^^~^~!!!!!!^::::::^~~^::::::?@@@!::^~^~!!~G@@@
@@@G:^^:::^!~7&&&J~!~^^^:::^~~~&@@@@@@@@@@&@@@@@&&@#777#&&G...B&@P:::777B@@@
G##P???^:::::~&&&7::::::^^^~YJJB################@@@&7?7&@@B   &@@B^^^7?7B@@@
   J@@@~^^::::::::::::::^~~5@@@.                G@@&777&@@B   &@@#77777!B@@@
   .!!7B##?^^^^^^^^^7BBB###P!!!                 :!!?#&&J!!^   ^!!Y&&&&&&5!!~
       &@@P!7777777!Y@@@@@@5                       .@@@:         !@@@@@@?
          P@@@@@@@@@#
          !GPPPPPPPP?


N: 5528421003787152797780905532338533644132966573280985037688431655042011332476181014684652947059175828063517292266175422491347923201331594191535914968309911
e_1: 69711700699061079421281917335076487671413484781984251661406882503217129427749
e_2: 104216655613664737083708985163582773085116384037288226397753589821256726375927


[1] - Encrypt text
[2] - Decrypt text
[3] - Get the flag
[4] - Get the encrypted key
[0] - Exit

Select an action: 4

Key:
0b28b5908bcd409b69bd63e664149de5776428c6ac567b27f5b21f6dcb16c0d54338cb92688654687a67381720f7808dcbd68da00f0e97566e60b1c960b13f62
Or...
094a5fee3aec8f4b508238e311f6ae07e3a41a1111bd989673d38dec767bd0509e3f4d04bd2c645a2d4f266cd0b6bc8eaaa5a13fd5c33236d20c531476e32365

[1] - Encrypt text
[2] - Decrypt text
[3] - Get the flag
[4] - Get the encrypted key
[0] - Exit

Select an action: 3

Enter the key: it'5_v3ry_*****************
b'CODEBY{RSA_*************}'

[1] - Encrypt text
[2] - Decrypt text
[3] - Get the flag
[4] - Get the encrypted key
[0] - Exit

Select an action: 0

Game over . . .solve-output-2.log

We see the flag CODEBY{RSA_*************}.

Conclusion

An easy task; just a bit of math to remember.


P.S. Some data is replaced with asterisks.



Next Post
No Dorchadas — osu!gaming CTF 2024