infosecfamily
← Back to catalog
CyLab (picoCTF)  /  Medium

basic-mod2

picoCTF challenges

basic-mod2 ↗

Solution code:

import math

# Encrypted message: 432 331 192 108 180 50 231 188 105 51 364 168 344 195 297 342 292 198 448 62 236 342 63




# Euclidean algorithm for finding gcd and coefficients x, y (I had to remember 7th grade)
def extended_gcd(a, b):
    if a == 0:
        return b, 0, 1
    else:
        gcd, x1, y1 = extended_gcd(b % a, a)
        x = y1 - (b // a) * x1
        y = x1
        return gcd, x, y

# function for finding modular inversion
def mod_inverse(a, m):
    gcd, x, y = extended_gcd(a, m)
    if gcd != 1:
        return None
    else:
        return x % m

# Original message
message_str = "432 331 192 108 180 50 231 188 105 51 364 168 344 195 297 342 292 198 448 62 236 342 63"

# Converting a string to a list of numbers
message_list = message_str.split(" ")
message_list = [int(x) for x in message_list]

print("Исходный список чисел:", message_list)

# [432, 331, 192, 108, 180, 50, 231, 188, 105, 51, 364, 168, 344, 195, 297, 342, 292, 198, 448, 62, 236, 342, 63]


print("Длина списка:", len(message_list))

# 26


# Apply the %41 operation to each element of the list
for index in range(len(message_list)):
    message_list[index] = message_list[index] % 41
    print(f"Индекс {index}: {message_list[index]}")
'''
Индекс 0: 22
Индекс 1: 3
Индекс 2: 28
Индекс 3: 26
Индекс 4: 16
Индекс 5: 9
Индекс 6: 26
Индекс 7: 24
Индекс 8: 23
Индекс 9: 10
Индекс 10: 36
Индекс 11: 4
Индекс 12: 16
Индекс 13: 31
Индекс 14: 10
Индекс 15: 14
Индекс 16: 5
Индекс 17: 34
Индекс 18: 38
Индекс 19: 21
Индекс 20: 31
Индекс 21: 14
Индекс 22: 22
'''



# Apply modular inversion to each element of the list
for index in range(len(message_list)):
    inverse = mod_inverse(message_list[index], 41)
    if inverse is not None:
        message_list[index] = inverse
    else:
        print(f"Модулярная инверсия для числа {message_list[index]} по модулю 41 не существует.")
        message_list[index] = 0  # Replace with 0 or another default value

# Dictionary for converting numbers to symbols
char_map = {
    1: 'A', 2: 'B', 3: 'C', 4: 'D', 5: 'E', 6: 'F', 7: 'G', 8: 'H', 9: 'I', 10: 'J',
    11: 'K', 12: 'L', 13: 'M', 14: 'N', 15: 'O', 16: 'P', 17: 'Q', 18: 'R', 19: 'S',
    20: 'T', 21: 'U', 22: 'V', 23: 'W', 24: 'X', 25: 'Y', 26: 'Z',
    27: '0', 28: '1', 29: '2', 30: '3', 31: '4', 32: '5', 33: '6', 34: '7', 35: '8',
    36: '9', 37: '_'
}

# Convert numbers into characters and combine them into a string
result = ''.join([char_map[num] for num in message_list])

print("Результат преобразования чисел в строку:", result)


# 1NV3R53LY_H4RD_C680BDC1

Video walkthrough (not available):