1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
| from Crypto.Util.number import * from hashlib import *
def prime_to_matrix(p , n): binary_rep = bin(p)[2:].zfill(n^2) matrix = Matrix(GF(2), n, n, [int(b) for b in binary_rep]) return matrix
def matrix_to_prime(matrix, n):
binary_rep = ''.join(str(matrix[i, j]) for i in range(n) for j in range(n)) p = int(binary_rep, 2) return p
c = 3235165504936419327001182958857604252407894106191511594689199770239529482330201776634335054024549532136197669522057846845936856432300465191006002492337710 S = [[0,0,0,1,1,0,0,0,1,1,0,0,1,0,1,0], [1,1,0,1,1,1,0,0,1,0,1,0,0,0,0,0], [1,0,1,0,0,1,0,1,1,0,1,0,1,0,0,1], [0,0,1,0,0,0,1,0,0,1,0,0,0,1,0,1], [1,0,0,1,1,1,0,1,1,0,0,0,1,1,0,1], [0,1,0,1,0,0,1,0,0,1,0,1,0,0,1,1], [0,1,0,1,0,1,1,1,0,1,0,1,1,1,1,0], [0,1,1,0,1,0,0,0,1,1,1,1,0,1,0,0], [0,1,1,0,1,1,1,0,0,1,1,0,0,1,0,1], [0,1,1,0,0,1,0,1,0,0,0,1,0,0,0,1], [0,1,0,0,0,0,0,1,1,1,1,0,0,1,1,1], [0,0,0,0,0,0,0,0,0,0,1,0,1,0,1,1], [1,0,1,0,1,0,1,0,1,0,1,0,0,0,1,0], [0,1,0,0,1,1,0,0,0,1,0,1,1,0,1,0], [0,0,1,0,1,1,1,1,0,1,1,1,0,1,1,0], [1,1,0,0,1,1,1,0,0,0,0,0,0,0,0,0]] A = [[58759,40201,39541,50266,35606,44877,56746,51320,40748,58265,48198,36126,48722,38757,34645,49875,39713,39344,34596,44689,40300,55926,43000,56106,63327,47482,39178,49257,45418,48650,50427,41214], [37223,34740,55875,61596,60851,42409,41726,54223,37354,44483,35507,64442,65181,65016,52834,59838,45696,50951,64689,55931,44398,62595,54959,40901,53153,37642,49294,44606,54073,65437,35212,45412], [48231,47498,50751,54438,52483,45749,46482,53434,64253,62408,48700,34564,57730,54295,53081,42645,56470,49043,41516,35479,61962,56805,64471,48806,56148,64688,50255,50303,48066,63253,45081,37202], [39500,47944,47148,55510,41735,34408,42033,47874,52667,65230,38185,57224,57960,57242,43751,40229,50706,48842,56675,63792,38602,43856,47968,36029,46316,33861,43062,52545,36833,38576,33160,47188], [34763,52602,34003,38138,52214,37082,46267,41357,53209,46658,37405,38698,48689,58684,65468,53156,37151,42127,52088,60960,40078,54636,55496,63227,39694,39226,42924,46628,51678,47049,57251,39950], [47060,53229,53324,34857,53402,57558,41164,32890,40952,63714,59742,64510,45578,35856,53314,33442,55935,53065,47134,46908,34203,63156,40093,40012,42585,48353,42197,40052,55471,56463,59090,43410], [42707,46054,36404,47963,47992,38350,33551,63065,53172,62321,51122,42997,60500,47212,39884,38212,35359,64344,35149,36069,49665,34591,34888,46184,46025,43702,50458,43440,42957,55777,38349,62364], [45114,54936,52699,38175,56136,37236,42829,36618,60030,45321,63661,42951,62906,57387,39828,65151,46434,48641,48942,46877,64558,62541,54391,42591,63174,60575,57658,43790,61543,61629,43407,39256], [41315,61872,47514,42455,60909,54071,50796,40555,40903,48007,58870,48415,59960,52920,54333,49750,43868,36540,53277,55958,56116,55847,50861,39109,63323,51663,40245,61514,49405,38631,40884,41575], [58569,51882,33541,51174,56449,62583,39528,52284,61467,61079,35129,38211,46027,45383,51330,56124,62629,44368,42101,58060,50492,44668,56796,60349,52581,56199,61597,42695,38011,57044,53206,54786], [47321,62828,42621,47424,33743,62088,49711,61521,57670,43704,57504,64149,48406,61490,35062,47479,49000,53346,34652,64934,60709,42379,55886,38403,32818,49335,34353,38220,55492,53597,43511,65458], [36970,58823,43553,34488,45459,54107,36469,33268,48487,37195,50544,41167,64538,44132,47555,52965,33728,38671,60378,51219,42761,33794,35819,52469,48730,57793,59596,54642,34866,65462,60317,58309], [57083,59902,43498,58168,51020,48060,40540,40849,52773,34442,56495,51822,52167,36704,56534,38609,65188,36395,63399,58948,64253,61676,42001,40624,42533,61537,65194,50368,37166,55338,64318,60883], [38819,55867,35918,36263,55150,61800,40998,51487,60551,55103,64935,37621,44826,60725,37993,59843,54224,55716,36215,37215,41474,33618,63366,43856,50237,58623,37522,50735,44656,38173,50608,53558], [34900,37778,46423,39412,60092,51148,38323,53310,56808,51387,45360,51882,33763,52239,58031,38137,53084,57559,42893,47903,53781,44518,57306,50958,39625,64130,54299,36323,55685,39663,32956,40248], [52223,62894,35658,47955,53222,33292,40082,55850,35931,57385,61556,36711,55509,64333,55598,63410,54748,33026,44444,46688,34221,41402,37978,56794,51509,33728,46849,58278,51893,41375,47226,46681], [52976,34891,47925,43442,59515,34153,59957,55001,55731,51519,58333,49760,55771,35969,37262,34006,51319,33088,58729,43841,61794,46314,60137,36895,64435,37994,60705,48788,37855,61710,47819,42593], [61352,40709,36161,47983,45760,53141,39843,64766,43219,59102,41023,35212,34127,55560,59265,51853,36176,36365,64261,58669,55196,54380,54787,56392,51638,46506,33332,64068,47810,64586,55243,34259], [55770,57863,57900,49538,62388,49553,49700,65293,59573,34557,45202,38030,37606,63523,60307,60828,56235,34309,33666,54514,61250,36319,34338,64016,48939,64224,60796,39570,47126,57106,64591,52600], [44615,45732,55786,36582,42334,49303,60246,33709,48035,45869,51532,55821,50199,54772,44909,50088,38610,45149,39935,64626,41655,57944,53886,40673,36939,33676,40293,50839,61527,60699,51922,44106], [65377,62356,55220,59201,43963,64568,37991,40846,45998,51359,47140,54987,52330,44097,48724,40654,33688,44820,44244,50178,57279,55648,63665,41583,32832,57810,57402,59925,51002,58322,36333,51125], [41287,34166,56834,40393,59397,38911,49904,36849,58510,35840,57339,54476,40809,39457,50758,40572,46317,61096,62870,49606,38598,47450,60284,58987,51986,45080,63220,40176,36875,45807,43529,56068], [48193,48774,33289,42145,49312,47521,39914,51925,39917,48165,47224,63940,49652,57371,39924,48775,41194,48318,48655,46114,53818,42672,52893,63011,62802,44759,46134,47433,54340,36563,41569,60490], [44350,46084,52253,34130,42256,42059,57465,53689,55189,58968,57975,36629,59833,56538,41512,53160,65479,57214,42956,59310,44119,58409,56941,43799,63205,64438,62116,46552,64986,55088,63289,64992], [59981,65120,50830,49306,65152,33117,45446,59937,59628,50051,52405,58739,51865,50946,39868,43945,51093,43390,51371,56690,54484,59380,34086,55929,35559,57879,57400,62067,39189,35199,36107,60269], [53525,51814,63825,54602,63470,51388,41444,42796,64239,39348,32780,34184,40353,46874,46188,56655,49280,44606,53601,52204,51605,61067,38266,53926,50113,62374,55836,35390,58903,55867,62906,35522], [63603,53936,57720,64444,34320,61868,51306,64322,38351,42819,53198,49825,43151,39952,36588,43407,63775,44609,50383,55258,56905,32794,64407,42274,42412,32771,37915,52745,36204,61105,44689,57883], [57362,57294,63492,60398,45797,53784,56740,33939,44595,65455,37575,33954,53422,57271,52175,55068,49522,40490,53837,56155,46962,51752,60461,45680,32875,44901,33050,46546,41262,51455,35039,60243], [41721,44309,60714,49640,38811,50954,35039,61157,55683,37992,64013,38705,41688,61244,55284,39208,34102,44140,42076,44126,39098,37919,47494,38276,39471,57038,39324,45531,33300,32850,63827,61734], [50638,49699,44478,58948,41101,54366,56564,44044,51920,45510,56763,59125,61576,43849,48245,38731,34149,35902,64669,50273,51014,54000,59856,41923,53353,35259,60315,48177,43783,58570,39743,33108], [60559,36829,47021,64117,52389,38518,49663,63281,45882,53052,42438,58989,53339,34039,48095,43454,64678,64406,48996,40125,44353,45907,42903,62292,63644,55109,43597,49842,61754,49182,55595,34419], [43089,62593,42823,39712,41287,63496,48540,37423,33935,65267,40334,41665,42970,38119,36223,45388,41159,38337,48393,60078,57130,48281,32779,33470,63099,38404,58265,40245,59839,56526,63269,65521]] b = [[199010799], [211695410], [210217770], [187225056], [190479150], [194625008], [185661914], [211167858], [206237868], [212420633], [201482391], [194210580], [213090600], [195153487], [184935683], [200284604], [201920950], [207287467], [212872468], [198033410], [205187404], [192392168], [196682634], [221830743], [207500337], [208973913], [206595711], [203711457], [185840896], [199676057], [212282405], [197217773]]
def primal_attack1(A,b,m,n,esz): L = block_matrix( [ [(matrix(A).T).stack(-vector(b)).change_ring(ZZ),matrix.identity(n+1)], ] ) L = L.LLL() e = list(map(abs,L[0][:m])) s = list(map(abs,L[0][m:-1])) return (s,e)
A = Matrix(ZZ,A) b = Matrix(ZZ,b) m, n = A.dimensions() array_q, e = primal_attack1(A,b,m,n,128) q = sum([array_q[i]*256^i for i in range(len(array_q))])
Q = prime_to_matrix(q,16) S = Matrix(GF(2), S) E = Matrix(GF(2), identity_matrix(16)) P = (Q-E).solve_left(S) p = matrix_to_prime(P, 16)
nums = 16 c = c ^^ nums n = 2^512 L = block_matrix(ZZ, [ [1, (Matrix(ZZ,[pow(p,(nums-i),n) for i in range(nums)]).T).stack(vector(ZZ,[-c]))], [0,n] ]) ans = list(map(int, L.LLL()[0])) ans = ans[:-2] + [-ans[-1]] ans = ans[::-1] ans = [ans[-1]//abs(ans[-1]) * i for i in ans]
key = b"" for i in range(len(ans[:-1])): key += long_to_bytes((c - ans[i]) ^^ c) c = (c - ans[i]) % n c = c * inverse(p,n) % n key = key[::-1]
flag = "CBCTF{"+sha256(key).hexdigest()+"}" print(flag)
|