Challenge: Hyper Normal
Author: Shotokhan
Description: GCD in finite fields
CTF: CryptoCTF 2021
Category: Crypto

Task

Being normal is hard these days because of Corona virus pandemic!

We have source code and a file called output.txt.
The source code is the following:

import random
from flag import FLAG

p = 8443

def transpose(x):
	result = [[x[j][i] for j in range(len(x))] for i in range(len(x[0]))]
	return result

def vsum(u, v):
	assert len(u) == len(v)
	l, w = len(u), []
	for i in range(l):
		w += [(u[i] + v[i]) % p]
	return w

def sprod(a, u):
	w = []
	for i in range(len(u)):
		w += [a*u[i] % p]
	return w

def encrypt(msg):
	l = len(msg)
	hyper = [ord(m)*(i+1) for (m, i) in zip(list(msg), range(l))]
	V, W = [], []
	for i in range(l):
		v = [0]*i + [hyper[i]] + [0]*(l - i - 1)
		V.append(v)
	random.shuffle(V)
	for _ in range(l):
		R, v = [random.randint(0, 126) for _ in range(l)], [0]*l
		for j in range(l):
            v = vsum(v, sprod(R[j], V[j]))	
		W.append(v)
	
	random.shuffle(transpose(W))
	return W

enc = encrypt(FLAG)
print(enc)

output.txt contains the “encrypted” flag.

Analysis

There are some computations made on the plaintext.
The first one: multiply the ASCII value of each character with its index in the plaintext increased by one, to obtain the list called “hyper”.
Then, a diagonal matrix “V” is built from hyper: the values on the diagonal are the values from hyper, in the same order.
The rows of the matrix are then shuffled.
At this point, it starts the building of the matrix “W”.
To summarize, each row of W is the list “hyper”, with each element modified in the following way:

W[i][j] = (hyper[i] * random.randint(0, 126)) % p;
where p = 8443, "i" and "j" in range(l), where "l" is the length of the message to encrypt.

The previous shuffle was thwarted.
Finally, there is a misleading statement: random.shuffle(transpose(W)); the random shuffle could make the challenge much harder… if only it were happened.
In fact, “transpose” function doesn’t operate in place, returning the same object, but creates a new object.
Therefore, the statement has no effects.
The matrix “W” is then returned as “encrypted” output.

Solution

The generic column of “W”, let’s denote it as W[:][j] is of the following form:

W[:][j] = [(ord(m) * (j + 1) * r[0][j]) % p, (ord(m) * (j + 1) * r[1][j]) % p, ... , (ord(m) * (j + 1) * r[l-1][j]) % p]

If it wasn’t for the modulo operations, we could easily get ord(m) * (j + 1) by computing the GCD over column j, then obtain the character m in position j for each j, and finally join all characters to obtain the plaintext.

We can try to find a way to compute GCD in finite fields, but since the range of random values is not very large, we can compute a full mapping.
I mean: for each index i in the range of the length of the plaintext, for each possible character m in the plaintext’s alphabet, for each possible random value r, compute the out value:
((i + 1) * ord(m) * r) % p, and remember it in a dict which maps out values to corresponding pairs (index, ASCII value of character); note that we don’t need to memorize the random value.

Now, we know that for each column, we are going to find the same pair (i + 1, ord(m)).
So, starting from the first element of a column and looking at its out value, we have a set of possible (i + 1, ord(m)) pairs which could have generated that value.
If we look at the second element, it will have, in general, a different value and so a different set; but at least one pair must be in common with the first set, because one pair will be the good one for all the elements in the column.
Therefore we compute the intersection between the first set and the second set, and remember the result in “s”.
As you may expect, now the game is simple: we loop over the column, obtain for each element the set of possible pairs, compute the intersection between this set and the set “s” and assign the result to “s”.
We do this for all columns, obtaining an ordered list of sets of possible (i + 1, ord(m)) pairs.

At this point, we have to choose the pairs, and now the statement “the random shuffle could make the challenge much harder” written before makes sense.
It’s easy to choose the pairs if we suppose that for each set the indexes are unique, and this is easy to have because of the intersections between columns’ sets.
In fact, from the set in position “i” in the ordered list, we just choose the pair which has index equal to “i + 1”, and discard all the other pairs.
Now we have a list of pairs, that easily become a list of ASCII values, and we obtain the flag from this last list.

Here is the script:

import string


output = [[2010, 5761, 5040, 1357, 8215, 0, 164, 6385, 2245, 7824, 5933, 2580, 5326, 3291, 7217, 7278, 2642, 856, 5029, 211, 7163, 1856, 477, 3153, 394, 4104, 5940, 5675, 2140, 4739, 3987, 5198, 4291, 5274, 5259, 2523, 2651, 218, 5707, 3098, 2719, 729, 2961, 4210, 620, 1331, 503, 5302, 7707, 309, 4881, 4901, 5815, 124, 6633], [3685, 3081, 1385, 797, 721, 3577, 1172, 750, 5559, 1987, 6468, 7412, 4627, 7803, 5734, 3270, 6069, 6307, 1886, 2561, 3971, 4014, 503, 2703, 535, 5584, 467, 4546, 1842, 1891, 5776, 6547, 6518, 858, 1378, 2732, 6680, 6464, 2577, 4395, 8313, 729, 3180, 8058, 8289, 1597, 7191, 1756, 6380, 5054, 2722, 2637, 428, 1488, 6391], [3283, 535, 5544, 4674, 3295, 788, 6634, 625, 2672, 8331, 5929, 6495, 6500, 6473, 1601, 622, 681, 7484, 6826, 2758, 5823, 1260, 2617, 3020, 6415, 1895, 3531, 5915, 7251, 2280, 7854, 5330, 2051, 858, 1885, 5293, 3305, 8434, 1706, 5502, 6304, 3039, 1316, 4097, 5501, 3651, 5832, 8122, 2851, 7334, 0, 1252, 3049, 2356, 6875], [1474, 7771, 5415, 1120, 1784, 6010, 2981, 2154, 856, 8219, 3600, 3649, 783, 227, 5022, 5426, 681, 6842, 1796, 6874, 1133, 98, 7296, 3787, 8195, 2938, 8238, 1928, 2727, 7875, 1729, 5113, 4165, 7482, 681, 8006, 5617, 2188, 3842, 537, 7499, 4714, 1975, 2437, 8289, 6693, 7084, 7396, 1043, 8320, 6452, 3250, 3767, 5413, 4583], [5695, 3484, 7937, 8271, 4305, 7071, 5247, 7789, 215, 6462, 7007, 7298, 42, 5429, 2254, 499, 1816, 2138, 1077, 7472, 4434, 3599, 8018, 2669, 0, 303, 3531, 4336, 4849, 3315, 1309, 4830, 1875, 3638, 380, 2656, 2476, 5774, 1059, 4764, 1169, 3985, 2303, 1441, 3874, 2928, 6624, 5471, 2173, 4561, 8051, 2105, 5960, 496, 3617], [7169, 4422, 123, 3080, 1784, 6677, 836, 4808, 3849, 2382, 7731, 5426, 2013, 3527, 5397, 557, 6564, 5774, 5568, 2554, 0, 6119, 6342, 2369, 1746, 6324, 4670, 2288, 8181, 4669, 1969, 4415, 6518, 6071, 6637, 8139, 7614, 5892, 7019, 3668, 2312, 7808, 5812, 7198, 1756, 76, 6624, 5167, 423, 6410, 2974, 4022, 6006, 496, 1688], [3618, 1340, 4788, 4437, 2179, 7973, 3860, 1702, 535, 1875, 8274, 2675, 3257, 3872, 7908, 8197, 8112, 2, 4401, 4531, 1754, 3433, 4963, 2352, 7339, 5483, 3736, 1039, 2429, 1393, 2029, 5830, 1334, 1022, 3659, 5255, 4169, 4848, 900, 1074, 3152, 1121, 3838, 3516, 4623, 5248, 289, 2060, 5476, 7827, 6648, 7139, 6770, 4119, 8442], [5896, 8307, 1635, 4114, 2627, 3342, 5247, 4760, 5344, 3119, 370, 6400, 1398, 7517, 5279, 3516, 7059, 5344, 3412, 591, 1705, 4195, 6600, 4438, 1070, 5169, 4297, 7014, 632, 2070, 2918, 19, 8330, 4088, 3374, 2314, 5267, 4612, 7066, 1823, 7603, 648, 7896, 2105, 6171, 7492, 1830, 3529, 3952, 617, 2806, 3250, 1620, 5014, 4100], [3082, 3350, 6804, 5751, 5922, 6169, 1594, 798, 963, 4593, 7196, 5445, 2069, 808, 7651, 2163, 4562, 6307, 987, 7866, 4995, 3433, 2127, 7158, 5131, 1794, 1868, 3387, 3341, 6630, 5656, 434, 667, 6296, 3168, 4932, 6925, 8325, 1912, 5312, 6304, 6619, 5483, 3878, 5397, 7378, 8304, 7666, 4178, 3267, 1067, 7298, 3003, 4394, 3376], [4489, 402, 2643, 1357, 6317, 3145, 8064, 6664, 1283, 3119, 7731, 5616, 4585, 2020, 7276, 5795, 3323, 2887, 1347, 5903, 2473, 2520, 3996, 8259, 1819, 8331, 8238, 3507, 8172, 6879, 0, 2849, 1573, 5907, 6748, 8253, 0, 345, 6672, 2382, 8287, 1837, 4715, 4542, 6275, 1293, 5693, 7970, 2428, 8135, 1067, 6260, 3958, 4022, 1809], [1608, 268, 6048, 668, 3796, 1493, 1973, 7337, 2032, 3178, 2703, 4566, 2642, 3813, 7137, 4196, 7472, 107, 1526, 4918, 3557, 8277, 3764, 8409, 2281, 5057, 6669, 2228, 1517, 3813, 7974, 3764, 4165, 7318, 3865, 7797, 4064, 6464, 7778, 3825, 6278, 1202, 988, 6783, 7798, 7834, 5725, 4880, 5476, 8197, 6732, 1385, 6342, 8017, 2170], [1407, 536, 4282, 4200, 8048, 4714, 3989, 6712, 4596, 3290, 6649, 8386, 6626, 5202, 4171, 7893, 6069, 2033, 2245, 1984, 6798, 6368, 2591, 2386, 1926, 5382, 5735, 3267, 6348, 6342, 6785, 217, 2655, 1594, 6542, 8101, 4099, 2651, 7937, 5133, 3204, 8375, 3729, 4097, 7307, 3993, 5832, 7379, 5986, 8012, 4937, 3649, 8153, 3774, 5064], [5025, 4288, 5667, 3080, 8382, 356, 5204, 125, 3528, 5837, 3234, 1625, 2656, 8325, 830, 5426, 763, 2352, 5119, 7, 3094, 3599, 3300, 5723, 5987, 4530, 1028, 4726, 2438, 6342, 780, 2217, 4706, 1880, 2265, 2238, 2581, 4966, 806, 3098, 7499, 2080, 2523, 6036, 466, 2738, 3007, 3546, 2880, 1849, 1095, 613, 6105, 4270, 7718], [4958, 5226, 2139, 1034, 2794, 8170, 7642, 1827, 6948, 5218, 4316, 3535, 6584, 6877, 7533, 4319, 5429, 7161, 7005, 6290, 6591, 7281, 4267, 2202, 6415, 4530, 6202, 4486, 1815, 8194, 0, 85, 5071, 7257, 8237, 8386, 549, 5774, 553, 7325, 2719, 7821, 3620, 5953, 0, 6351, 8122, 4137, 8356, 5609, 1095, 2984, 1574, 6777, 3015], [5829, 3885, 6298, 2520, 5588, 1849, 1215, 5635, 6307, 3402, 2341, 7241, 6626, 3754, 3737, 1975, 3736, 7163, 988, 3159, 5104, 2437, 3764, 3337, 6663, 6010, 5436, 1129, 1237, 7128, 6245, 3066, 1938, 3802, 887, 2219, 7474, 2769, 5154, 716, 277, 4782, 1427, 5206, 4390, 3004, 1038, 287, 2851, 5855, 6144, 6633, 8061, 7300, 2652], [5695, 1474, 125, 2951, 6150, 159, 2559, 8164, 2673, 5501, 901, 727, 3271, 4739, 8165, 4319, 4789, 6628, 448, 6107, 6128, 8375, 5891, 7775, 7232, 4844, 4633, 2078, 3937, 39, 5296, 19, 302, 1941, 1964, 2048, 4029, 7735, 6260, 3277, 6685, 8294, 220, 362, 7282, 5324, 4120, 2938, 2851, 185, 1683, 2770, 5341, 4890, 964], [1273, 7370, 3778, 3920, 3242, 6836, 7263, 2452, 1924, 2777, 6114, 7241, 7213, 631, 7592, 5303, 5202, 4597, 6916, 3933, 5458, 4180, 6136, 3504, 7626, 830, 467, 2018, 3052, 4171, 4407, 6613, 491, 7379, 2582, 2276, 5302, 6119, 8078, 705, 1576, 1202, 4168, 6783, 6895, 6161, 6656, 1756, 7058, 1295, 6284, 4875, 5815, 6777, 6392], [5293, 6431, 8189, 797, 1283, 1023, 6970, 1904, 3849, 6804, 6834, 3668, 1412, 3527, 5220, 8385, 7390, 4597, 4939, 7662, 1498, 5523, 7515, 6974, 4703, 3476, 7603, 4516, 605, 5276, 1020, 1434, 3322, 2105, 79, 4951, 6096, 5429, 2624, 1096, 3074, 6457, 2852, 7115, 6920, 3042, 5190, 4137, 6606, 4499, 8135, 4129, 2239, 4739, 8081], [670, 4154, 5919, 108, 1336, 3653, 1680, 2077, 2032, 4876, 0, 6362, 4613, 3468, 3796, 7278, 3055, 7910, 628, 4334, 414, 6285, 490, 133, 7553, 5057, 7808, 3447, 8181, 7556, 3158, 151, 2781, 3638, 5164, 5426, 5337, 7054, 2671, 716, 5923, 7173, 3071, 3878, 5784, 1597, 2900, 6349, 5957, 4561, 1515, 8177, 5005, 496, 6030], [2211, 8040, 3274, 4200, 7600, 1925, 6591, 2856, 5451, 4251, 1979, 8348, 84, 4739, 118, 8262, 7163, 2031, 3322, 7268, 3606, 4527, 1676, 1418, 7519, 527, 3998, 2378, 4244, 3245, 5536, 3066, 6153, 0, 4150, 8044, 444, 2660, 3130, 4216, 8365, 6146, 6142, 2603, 2609, 5210, 7769, 3546, 8217, 247, 7939, 6393, 6388, 5165, 1929], [5494, 804, 7056, 5277, 4806, 7503, 1258, 4760, 1710, 454, 6472, 1644, 1398, 5429, 4685, 2590, 8339, 7268, 4580, 3159, 7370, 4195, 3055, 3153, 1853, 6324, 8238, 8023, 4235, 5844, 6965, 5547, 5851, 8279, 3453, 5350, 1203, 345, 6260, 4395, 1143, 4147, 3620, 3682, 724, 7910, 3403, 557, 7284, 3020, 1207, 613, 520, 4518, 4341], [3149, 6029, 3526, 3791, 4920, 1887, 207, 7914, 3527, 8160, 539, 1777, 2670, 7281, 1740, 2221, 4190, 5772, 4849, 1787, 2473, 1856, 1650, 300, 2602, 1996, 8107, 3087, 8154, 3494, 5116, 7877, 4895, 2616, 3453, 8120, 7719, 7054, 7431, 6788, 6027, 4066, 1098, 3629, 5035, 7568, 824, 1891, 1947, 3575, 4601, 5034, 3668, 2728, 7477], [2546, 2679, 1887, 6720, 3295, 2357, 2016, 2856, 2565, 1138, 4135, 3744, 769, 1498, 632, 4442, 3096, 7803, 5928, 3933, 3094, 0, 4022, 6524, 2247, 6223, 5399, 7993, 7865, 4101, 6965, 868, 4832, 2841, 7445, 8386, 5442, 4848, 6213, 7135, 5516, 7254, 6141, 4708, 2222, 7910, 3831, 6045, 1524, 4992, 1039, 5274, 8298, 372, 723], [3685, 6833, 1383, 6074, 7934, 985, 2016, 3356, 4275, 2665, 6291, 917, 7800, 3173, 1403, 7213, 1548, 537, 359, 2371, 1291, 513, 4963, 4605, 822, 202, 6333, 6834, 3937, 3634, 960, 3283, 1749, 6071, 3960, 5559, 1787, 6936, 4648, 4563, 8339, 797, 5593, 4459, 5759, 3841, 8336, 8291, 1918, 6410, 6284, 6286, 7534, 5262, 6271], [2077, 6431, 2268, 3877, 5588, 6404, 3188, 4760, 7909, 3178, 362, 917, 713, 7921, 375, 7032, 8298, 3101, 1616, 6881, 109, 6187, 2127, 4755, 2281, 8006, 4502, 8143, 2420, 6879, 3927, 302, 6581, 8054, 4467, 8158, 1962, 454, 2071, 7683, 1905, 7173, 2194, 7862, 7282, 2624, 3221, 5623, 1101, 2527, 8163, 5274, 2575, 7796, 2773], [1407, 3082, 4788, 2800, 6036, 2122, 4618, 8116, 7696, 2040, 5213, 8348, 3355, 6355, 6505, 2040, 7431, 858, 1886, 5713, 5665, 3863, 1418, 951, 7660, 830, 897, 3177, 4253, 5774, 300, 7245, 1699, 1247, 1679, 5464, 7719, 6346, 7825, 6218, 4321, 2080, 3948, 6617, 4777, 6617, 7191, 557, 1495, 4807, 2890, 7272, 3148, 4022, 4100], [335, 2009, 756, 8120, 2513, 8405, 5247, 1375, 4169, 855, 1802, 6305, 7227, 7163, 771, 7336, 2642, 6628, 4131, 2174, 3399, 6700, 4692, 3971, 3064, 3902, 2634, 7963, 3639, 5167, 300, 3915, 7248, 4149, 1980, 2656, 3994, 5075, 3177, 6766, 5542, 648, 6908, 611, 8418, 190, 6624, 709, 4601, 4992, 6368, 1972, 5960, 8017, 8201], [6231, 4689, 6300, 517, 1503, 7344, 3231, 1904, 2672, 796, 8, 8291, 2069, 6995, 7988, 8074, 3736, 6628, 538, 6487, 5774, 3765, 4718, 7458, 8302, 5898, 7435, 7164, 632, 7517, 2558, 6962, 5499, 3188, 8427, 7892, 1098, 1961, 1059, 4753, 5109, 3593, 7019, 279, 6533, 5058, 8304, 4137, 3500, 7642, 8163, 3863, 1857, 5413, 7718], [4824, 4824, 2014, 2477, 5641, 432, 2266, 5760, 3314, 2270, 189, 3725, 42, 6355, 118, 5180, 6832, 5879, 449, 6684, 3862, 1426, 4473, 6340, 6235, 2108, 7472, 7104, 4235, 2957, 7254, 3415, 3083, 6071, 1679, 2390, 6680, 7272, 1153, 347, 5975, 1999, 2962, 4044, 2376, 4868, 5372, 1030, 7962, 7088, 4993, 2637, 7297, 868, 5789], [2948, 267, 8316, 5514, 5026, 6442, 1172, 2779, 7161, 8278, 5213, 2618, 1370, 7803, 6762, 2901, 6151, 7161, 7365, 1590, 7931, 3101, 709, 6974, 6770, 4956, 3867, 5795, 2754, 7626, 2149, 8160, 2781, 1880, 190, 2238, 6890, 5202, 8284, 3657, 4373, 1594, 3290, 7115, 491, 6351, 6870, 1604, 3077, 5732, 4601, 7778, 5578, 6254, 6392], [7571, 268, 3653, 1077, 2566, 6480, 1637, 2779, 429, 7258, 1802, 8329, 7926, 513, 4171, 3147, 309, 749, 3053, 6691, 6128, 5357, 1212, 3504, 3813, 6212, 2466, 6954, 2727, 4241, 2678, 7311, 4832, 8401, 5544, 7683, 4788, 345, 5454, 2930, 1524, 4552, 8005, 5538, 6637, 1407, 7084, 2634, 6664, 5978, 1263, 3276, 5150, 2728, 6874], [6700, 6968, 2141, 3231, 6704, 4752, 3274, 4885, 7161, 1474, 8, 5483, 1370, 1843, 8047, 6598, 1176, 4171, 6466, 1386, 2936, 7779, 3094, 3504, 2816, 6223, 3064, 6924, 3323, 5097, 7025, 3198, 3750, 4027, 4562, 2371, 5442, 2415, 600, 6039, 7499, 2080, 7677, 7530, 1240, 1255, 6624, 2499, 4827, 6595, 1347, 480, 4814, 4243, 4220], [67, 1742, 4911, 1874, 4305, 3577, 3024, 7337, 1177, 2211, 7192, 1796, 42, 5547, 59, 8016, 1589, 5560, 5658, 401, 7833, 8360, 7773, 3821, 1391, 3891, 1065, 5705, 7269, 6023, 1789, 7462, 6216, 2166, 2376, 2789, 4578, 7980, 3477, 5133, 6763, 8051, 3509, 1939, 4157, 7834, 8122, 2482, 7991, 5609, 8191, 960, 8061, 2259, 7597], [2144, 1741, 1510, 5234, 1283, 4085, 3317, 5183, 5451, 4027, 4855, 3535, 1999, 8089, 257, 5918, 8071, 2780, 3861, 992, 7212, 5606, 735, 3804, 4562, 5898, 7603, 2408, 605, 2319, 5716, 2349, 3385, 347, 4467, 4970, 5547, 2306, 5801, 4943, 3940, 3431, 6908, 3961, 7024, 2890, 3403, 2634, 2173, 5177, 3310, 4369, 7198, 345, 5064], [6231, 4824, 6552, 6677, 5755, 2554, 3946, 1500, 4383, 1138, 189, 746, 4599, 1961, 3400, 6352, 4231, 2566, 4670, 7282, 316, 4610, 5878, 2686, 6984, 2209, 6202, 2378, 5734, 965, 4936, 7811, 3624, 3352, 871, 8196, 3305, 6927, 6166, 7705, 2338, 6619, 7567, 777, 7515, 4982, 6517, 3107, 3303, 8320, 6480, 1732, 5387, 7769, 3739], [402, 938, 2643, 4157, 5087, 1652, 1887, 3529, 1, 1704, 3773, 8272, 2642, 6650, 6880, 1975, 1403, 858, 2514, 1189, 3862, 3765, 3545, 1418, 2174, 8432, 934, 5855, 7540, 6879, 2209, 7311, 856, 4477, 6051, 2333, 2067, 681, 6260, 168, 251, 5268, 1317, 2022, 620, 3955, 5725, 6653, 2173, 1664, 1599, 4635, 6724, 7893, 6150], [5360, 2948, 1008, 6914, 7714, 394, 3610, 4760, 8337, 4310, 3781, 6476, 657, 2020, 2827, 1852, 7245, 6200, 5299, 5896, 6128, 3267, 1444, 4605, 4703, 4429, 4932, 6744, 8172, 1533, 1729, 1283, 1699, 8340, 6732, 7987, 4788, 8198, 2212, 3277, 3585, 7578, 2194, 8058, 3254, 3765, 6335, 7244, 3471, 4807, 1179, 81, 5960, 3747, 2170], [5896, 1340, 3905, 474, 782, 3577, 121, 5760, 5665, 1250, 5213, 8329, 699, 5429, 6109, 7459, 2869, 2568, 3861, 0, 7833, 7364, 3081, 4455, 3137, 6436, 8107, 8263, 7847, 7875, 2029, 6047, 3989, 4149, 8031, 8329, 8163, 6355, 5548, 6598, 1195, 2161, 1316, 777, 4777, 7568, 610, 1604, 6664, 925, 4769, 4635, 5387, 3375, 2171], [6030, 4287, 2139, 8120, 7380, 864, 2602, 3481, 962, 3290, 3600, 7488, 1356, 5547, 1917, 1114, 309, 7163, 3143, 4925, 3971, 6202, 5878, 4588, 7412, 1682, 1831, 8233, 6962, 1105, 4407, 3349, 8393, 7257, 7033, 2751, 8233, 8316, 2718, 6039, 2286, 5498, 2633, 3101, 4777, 2700, 1434, 7396, 2202, 4438, 4545, 960, 1238, 992, 723], [3752, 1741, 5667, 1680, 1731, 2516, 4997, 4885, 1925, 4027, 7546, 898, 98, 345, 6880, 4131, 3137, 5453, 4670, 3342, 6700, 6451, 4937, 7308, 2850, 3050, 6538, 8293, 1842, 8303, 5836, 5962, 6040, 6010, 5655, 2029, 2651, 4158, 5754, 3478, 3940, 5349, 3948, 5206, 4132, 3841, 2076, 422, 7284, 5362, 5049, 5008, 2621, 7769, 722], [4422, 5226, 3778, 2477, 3910, 3888, 7056, 1077, 4704, 6521, 5394, 2770, 5913, 4503, 5654, 8385, 7059, 1817, 5838, 7662, 2729, 8277, 735, 5256, 3565, 1895, 4371, 4816, 7576, 3494, 5896, 5547, 2051, 4252, 6336, 8405, 7614, 2542, 6813, 1465, 3940, 2323, 3729, 943, 2996, 2510, 6838, 3816, 3726, 2835, 4545, 4022, 237, 7424, 7357], [3551, 2009, 5796, 1594, 3857, 2046, 2223, 7616, 1283, 1875, 3600, 2789, 783, 5025, 573, 2286, 2188, 3529, 6107, 8260, 3764, 264, 1650, 1735, 3351, 4317, 6333, 3147, 2447, 2319, 6185, 5113, 1938, 2616, 3057, 8405, 3340, 8198, 3177, 5133, 8036, 2553, 6908, 8224, 491, 684, 1862, 6214, 1495, 5362, 2890, 214, 4623, 841, 7718], [3283, 2411, 2772, 1314, 1898, 6207, 1215, 1327, 6627, 4027, 2876, 5540, 2698, 1557, 0, 5918, 7844, 0, 3412, 5122, 365, 1011, 3558, 5856, 6556, 7479, 7771, 709, 5761, 4848, 5416, 217, 5008, 3638, 4467, 7949, 3515, 6582, 4742, 3668, 658, 324, 6360, 8028, 7773, 3727, 1434, 7396, 846, 1726, 7743, 5141, 2575, 5386, 6754], [3752, 7503, 5544, 4997, 1283, 2986, 3903, 375, 1604, 1421, 5036, 4433, 4599, 3064, 6248, 8385, 7803, 1926, 3861, 8260, 4897, 1758, 954, 3003, 6556, 3476, 1831, 5825, 1535, 4669, 4647, 2217, 3385, 4599, 586, 0, 3959, 8207, 1359, 2382, 7984, 3998, 329, 3878, 1860, 7378, 7052, 1148, 5928, 678, 2918, 6260, 2285, 1116, 3014], [1675, 401, 4788, 6677, 1230, 3342, 5669, 5058, 4703, 1191, 2518, 1625, 3257, 6591, 7592, 3147, 2828, 5772, 3053, 5699, 6237, 2935, 5904, 5873, 5238, 2108, 5305, 7903, 7874, 1463, 3458, 3981, 7361, 4435, 1378, 2732, 409, 2197, 2577, 5312, 1117, 878, 3180, 8224, 2351, 5172, 5618, 574, 7087, 6841, 1487, 1865, 6770, 6405, 4583], [3752, 3752, 1510, 840, 5140, 2789, 8021, 2529, 7161, 1138, 1086, 8234, 7842, 4857, 7849, 622, 7390, 5130, 3413, 8056, 5202, 8375, 7051, 2052, 2388, 4216, 4334, 4786, 1833, 4848, 7494, 1283, 6216, 1819, 7635, 7778, 6960, 3341, 3430, 5860, 1931, 1594, 4935, 6368, 1473, 4944, 2472, 1604, 4178, 6718, 6088, 8018, 3576, 3100, 7719], [67, 5761, 2141, 108, 3242, 6601, 6341, 6260, 6307, 5218, 5040, 3592, 2083, 6355, 3539, 7828, 2683, 7375, 5837, 3349, 6335, 6187, 6845, 150, 4241, 5696, 5866, 2108, 2420, 1712, 4647, 4047, 7550, 919, 4055, 7702, 2032, 4621, 806, 7146, 2693, 3201, 7348, 1856, 7024, 7910, 8197, 287, 4149, 4931, 1039, 3516, 711, 5014, 2049], [1876, 6565, 627, 4071, 2513, 1023, 5333, 7337, 428, 5442, 2160, 6305, 98, 1212, 4428, 8385, 8030, 7161, 2784, 7479, 5872, 7779, 6110, 3504, 7519, 3476, 4035, 5885, 2420, 607, 5776, 4047, 7726, 1716, 7429, 5160, 5267, 4848, 94, 1834, 5542, 6214, 7676, 7809, 104, 3651, 717, 591, 3048, 2774, 2806, 4528, 6487, 3871, 2411], [3350, 4287, 4155, 7237, 3690, 5540, 7013, 2529, 3527, 2040, 2703, 3668, 4655, 1498, 2629, 4934, 4562, 6840, 1257, 5319, 2680, 2007, 3790, 7792, 2033, 5483, 7771, 3237, 605, 8233, 8214, 7028, 2416, 3352, 1172, 2504, 6925, 6936, 4095, 2740, 251, 1364, 3400, 6202, 5372, 2966, 3724, 6501, 1976, 5547, 6732, 7298, 7343, 8292, 844], [5829, 7905, 2266, 6397, 8215, 5813, 5876, 4760, 5985, 4988, 728, 3687, 6556, 3872, 2431, 4377, 4458, 2675, 5837, 1386, 2424, 3599, 477, 450, 4596, 527, 2970, 3327, 7540, 3385, 4587, 4896, 5612, 183, 5053, 5350, 7439, 218, 4695, 1085, 658, 6065, 6908, 7447, 3358, 7644, 4762, 2634, 2173, 1603, 4517, 853, 1047, 6157, 4462], [4422, 5092, 7685, 4717, 2460, 356, 2852, 375, 321, 1138, 4674, 7260, 2712, 4503, 3084, 5918, 2828, 4385, 3862, 6297, 1912, 4527, 26, 3320, 4348, 1895, 3904, 3177, 632, 4420, 7914, 4132, 3926, 1758, 2677, 5331, 2581, 7272, 7066, 0, 1576, 567, 1098, 8224, 1885, 2966, 4366, 1891, 5702, 6225, 1459, 3623, 2384, 841, 2412], [1943, 2546, 7433, 2714, 6317, 4047, 965, 7414, 3207, 2441, 1086, 8348, 1426, 7222, 3855, 5057, 7658, 3529, 5928, 5910, 2631, 2686, 6110, 2703, 5987, 1144, 299, 7104, 3052, 4490, 2798, 3198, 2177, 1880, 3358, 7911, 5991, 8316, 3889, 3478, 7525, 2553, 659, 2354, 1007, 228, 5372, 2516, 7255, 617, 8135, 6526, 283, 345, 6632], [6432, 8174, 5290, 1228, 5026, 7700, 3903, 1625, 7268, 4310, 7727, 3611, 3285, 7222, 4883, 1729, 1961, 1924, 2963, 1386, 5567, 7281, 1921, 1401, 6128, 2411, 7603, 2108, 7260, 4241, 3278, 1066, 969, 7604, 5449, 5027, 1857, 2996, 8190, 7325, 1065, 3039, 3948, 6534, 8314, 7378, 4655, 422, 5476, 3328, 0, 8151, 2239, 5661, 4823], [7705, 6699, 123, 1077, 1898, 3221, 6212, 7866, 1283, 3461, 0, 879, 6528, 1675, 3282, 6106, 5284, 7270, 1796, 8260, 1961, 7198, 3300, 1101, 3064, 101, 5567, 829, 641, 5027, 4227, 3915, 4769, 5724, 3564, 5084, 3445, 3459, 7825, 8242, 3152, 7092, 5483, 2188, 208, 2434, 6517, 878, 4630, 7642, 7967, 7298, 3431, 2728, 7115], [5963, 6834, 4659, 3274, 2232, 8208, 4997, 7741, 6092, 5784, 7373, 4490, 6570, 867, 573, 2163, 495, 4385, 6825, 5903, 365, 264, 4718, 3787, 3458, 7793, 5567, 1159, 2140, 4350, 6605, 7594, 1095, 3741, 5544, 8177, 619, 6936, 1312, 8231, 3888, 1445, 6032, 3380, 4752, 7454, 7266, 7109, 4375, 7704, 6732, 6260, 4050, 2604, 4582]]


def main():
    p = 8443
    mapping = {}
    alphabet = string.digits + string.ascii_letters + '!-.?_{}'
    
    for i in range(len(output)):
        for m in alphabet:
            for r in range(127):
                _key = ((i + 1) * ord(m) * r) % p
                _value = (i + 1, ord(m))
                if mapping.get(_key):
                    mapping[_key].add(_value)
                else:
                    mapping[_key] = set([_value])
    
    values = []
    for i in range(len(output)):
        col = [output[j][i] for j in range(len(output))]
        s = mapping[col[0]]
        for j in range(1, len(col)):
            s = s.intersection(mapping[col[j]])
        values.append(s)
        
    for i in range(len(values)):
        val = values[i]
        inOrder = False
        delSet = set()
        for t in val:
            if t[0] == i + 1:
                inOrder = True
            else:
                delSet.add(t)
        if inOrder:
            values[i] = values[i] - delSet

    # sanity check :)
    assert all(map(lambda _set: len(_set) == 1, values))
    
    m = [0] * len(values)
    for i in range(len(values)):
        ind, flag_char = list(values[i])[0]
        m[ind - 1] = flag_char
    
    print("".join([chr(i) for i in m]))
    


if __name__ == "__main__":
    main()

CCTF{H0w_f1Nd_th3_4lL_3I9EnV4Lu35_iN_FiN173_Fi3lD5!???}