Namta
Published:
Category: Crypto
Problem: description:
One of our developers has created a new way to encrypt any text via Namta (Multiplication). But we think the encryption is kinda weak.
Can you break it and decode the encrypted text.
Input: CTF_BD{#####################################}
Input: 14
Output:
798, 1596, 3612, 5656, 8190, 4452, 8330, 11200, 13734, 7980, 7546, 16632, 21840, 14504, 10080, 19712, 16898, 20664, 13034, 22680, 19404, 25872, 31234, 16128, 28700, 44408, 33642, 43904, 48720, 29820, 42966, 50176, 46200, 32368, 33810, 50904, 24864, 26068, 26754, 54880, 67158, 51744, 73444, 48048, 73710, 52164, 32242, 37632,
The following code is also given as attachment.
# namta.py
import base64
encryption_string = input("Enter Text to Encrypt: ")
multi = int(input("Enter Number to Multiply: "))
encryption_string_bytes = encryption_string.encode("ascii")
base64_bytes = base64.b64encode(encryption_string_bytes)
crypt_data = base64_bytes.decode("ascii")
lol = ""
lol2 = ""
for i in range(multi):
lol += crypt_data[multi-1]
l = list(crypt_data)
del(l[multi-1])
crypt_data = "".join(l)
new_crypt = lol + crypt_data
for i in range(multi):
lol2 += new_crypt[-multi]
l = list(new_crypt)
del(l[-multi])
new_crypt = "".join(l)
new_crypt = lol2 + new_crypt
x = new_crypt
chunks, chunk_size = len(x), len(x)//2
y = [x[i:i+chunk_size] for i in range(0, chunks, chunk_size)]
new_crypt = y[1][::-1] + y[0][::-1]
full_crypt = ""
for i in range(len(new_crypt)):
print(f"{multi} * {i+1} = {multi*(i+1)}")
full_crypt += f"{(multi*(i+1))*ord(new_crypt[i])}, "
with open("output.txt", "w") as enc_out:
enc_out.write(full_crypt)
# print(full_crypt)
Solution:
After inspection of the code, we can observe the following:
- Flag is first base64 encoded
- 14 characters from index 14 are brought to the front
- 14 characters from index 14 from the last is brought to the front in reverse order
- The string is reversed
- Each character of the string becomes the multiplication of 14, its index+1 and it’s ascii code
So, the following code reverses the logic and prints out the flag:
import base64
multi = 14
input_file = open("input.txt", "r")
str = input_file.readline()
str = str.split(", ")
str[len(str)-1] = str[len(str)-1].replace(",", "")
str = [int(i) for i in str]
crypt_data = ""
for i in range(0, len(str)):
char = str[i]/(multi * (i+1))
char = int(char)
crypt_data = crypt_data + chr(char)
crypt_data = crypt_data[::-1]
data_lenght = len(crypt_data)
lol2 = crypt_data[0:14]
lol2 = lol2[::-1]
crypt_data = crypt_data[14:35] + \
lol2 + crypt_data[35:data_lenght]
crypt_data = crypt_data[14:27] + crypt_data[0:14] + crypt_data[27:data_lenght]
print(crypt_data)
result = base64.b64decode(crypt_data)
print(result.decode("ascii"))
Flag : CTFBD{Mul7iplic4ti0n_3nC_1s_fuNny}