Skip to content

Commit a35c166

Browse files
authored
Merge pull request #52 from koitococo/main
add crypto writeups
2 parents c33523c + cf43362 commit a35c166

File tree

8 files changed

+804
-179
lines changed

8 files changed

+804
-179
lines changed

Challenges/Crypto/bad_random/writeup.ipynb

+38-116
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,42 @@
3232
" return self.x"
3333
]
3434
},
35+
{
36+
"cell_type": "code",
37+
"execution_count": 2,
38+
"metadata": {},
39+
"outputs": [],
40+
"source": [
41+
"instance = LCG()"
42+
]
43+
},
44+
{
45+
"cell_type": "code",
46+
"execution_count": 5,
47+
"metadata": {},
48+
"outputs": [
49+
{
50+
"data": {
51+
"text/plain": [
52+
"18855"
53+
]
54+
},
55+
"execution_count": 5,
56+
"metadata": {},
57+
"output_type": "execute_result"
58+
}
59+
],
60+
"source": [
61+
"instance.m"
62+
]
63+
},
64+
{
65+
"cell_type": "code",
66+
"execution_count": null,
67+
"metadata": {},
68+
"outputs": [],
69+
"source": []
70+
},
3571
{
3672
"cell_type": "code",
3773
"execution_count": 1,
@@ -51,18 +87,7 @@
5187
"def solve_a(x1,x2,x3,m):\n",
5288
" return ((x3-x2) * pow(x2-x1,-1,m))%m\n",
5389
"def solve_c(x1,x2,a,m):\n",
54-
" return (x2-a*x1)%m\n",
55-
"\n",
56-
"from Crypto.Util.number import sieve_base\n",
57-
"def fast_factor(num):\n",
58-
" factors = []\n",
59-
" for p in sieve_base:\n",
60-
" while num % p == 0:\n",
61-
" factors.append(p)\n",
62-
" num //= p\n",
63-
" if num == 1:\n",
64-
" break\n",
65-
" return factors,num"
90+
" return (x2-a*x1)%m"
6691
]
6792
},
6893
{
@@ -91,109 +116,6 @@
91116
" print(s[:-4])\n",
92117
" break\n"
93118
]
94-
},
95-
{
96-
"cell_type": "code",
97-
"execution_count": 45,
98-
"metadata": {},
99-
"outputs": [
100-
{
101-
"name": "stdout",
102-
"output_type": "stream",
103-
"text": [
104-
"17373\n"
105-
]
106-
}
107-
],
108-
"source": [
109-
"xs = [5124,9694,12564,5965,2391]\n",
110-
"m_ = solve_m(xs)\n",
111-
"print(m_)\n",
112-
"\n",
113-
"m=m_"
114-
]
115-
},
116-
{
117-
"cell_type": "code",
118-
"execution_count": 46,
119-
"metadata": {},
120-
"outputs": [
121-
{
122-
"name": "stdout",
123-
"output_type": "stream",
124-
"text": [
125-
"17373\n"
126-
]
127-
},
128-
{
129-
"data": {
130-
"text/plain": [
131-
"([3, 5791], 1)"
132-
]
133-
},
134-
"execution_count": 46,
135-
"metadata": {},
136-
"output_type": "execute_result"
137-
}
138-
],
139-
"source": [
140-
"print(m)\n",
141-
"fast_factor(m)"
142-
]
143-
},
144-
{
145-
"cell_type": "code",
146-
"execution_count": 47,
147-
"metadata": {},
148-
"outputs": [
149-
{
150-
"name": "stdout",
151-
"output_type": "stream",
152-
"text": [
153-
"15815\n",
154-
"1306\n"
155-
]
156-
}
157-
],
158-
"source": [
159-
"a = solve_a(*xs[:3],m)\n",
160-
"print(a)\n",
161-
"c = solve_c(*xs[:2],a,m)\n",
162-
"print(c)"
163-
]
164-
},
165-
{
166-
"cell_type": "code",
167-
"execution_count": 21,
168-
"metadata": {},
169-
"outputs": [],
170-
"source": [
171-
"instance = LCG()\n",
172-
"instance.m = m\n",
173-
"instance.a = a\n",
174-
"instance.c = c\n",
175-
"instance.x = xs[-1]"
176-
]
177-
},
178-
{
179-
"cell_type": "code",
180-
"execution_count": 24,
181-
"metadata": {},
182-
"outputs": [
183-
{
184-
"data": {
185-
"text/plain": [
186-
"634"
187-
]
188-
},
189-
"execution_count": 24,
190-
"metadata": {},
191-
"output_type": "execute_result"
192-
}
193-
],
194-
"source": [
195-
"instance()"
196-
]
197119
}
198120
],
199121
"metadata": {
@@ -212,7 +134,7 @@
212134
"name": "python",
213135
"nbconvert_exporter": "python",
214136
"pygments_lexer": "ipython3",
215-
"version": "3.11.4"
137+
"version": "3.11.6"
216138
},
217139
"orig_nbformat": 4
218140
},

Challenges/Crypto/factor_signin/writeup.ipynb

+69-2
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,85 @@
22
"cells": [
33
{
44
"cell_type": "code",
5-
"execution_count": null,
5+
"execution_count": 2,
66
"metadata": {},
77
"outputs": [],
88
"source": [
9+
"e = 65537\n",
10+
"c1 = 10004937130983861141937782436252502991050957330184611684406783226971057978666503675149401388381995491152372622456604317681236160071166819028679754762162125904637599991943368450200313304999566592294442696755822585022667008378021280392976010576970877334159755332946926433635584313137140987588847077645814987268595739733550220882135750267567373532603503399428451548677091911410732474324157868011686641243202218731844256789044721309478991918322850448456919991540932206923861653518190974620161055008847475600980152660468279765607319838003177639654115075183493029803981527882155542925959658123816315099271123470754815045214896642428657264709805029840253303446203030294879166242867850331945166255924821406218090304893024711068773287842075208409312312188560675094244318565148284432361706108491327014254387317744284876018328591380705408407853404828189643214087638328376675071962141118973835178054884474523241911240926274907256651801384433652425740230755811160476356172444327762497910600719286629420662696949923799255603628210458906831175806791599965316549386396788014703044837917283461862338269599464440202019922379625071512100821922879623930069349084917919100015782270736808388388006084027673781004085620817521378823838335749279055639005125\n",
11+
"n1 = 343504538870081878757729748260620800783581983635281373321527119223374418103340873199654926888439040391545101913132680017655039577253974802351999985470115474655124168592386965001556620077117966153475518658881140827499124290142523464795351995478153288872749817655925271395693435582010998996210909883510311066017237567799370371513462802547313382594409676803895262837061350017911885033133654781876923251129406855067993830824618637981136966134029212516871210627954762147349788788999116702635535406398258621926040887099782494271000823401788337120154104692934583729065189687995570122890809807661370008740283447636580308161498808092269041815719148127168137018600113465985504975054319601741498799761500526467431533990903047624407330243357514588557352746347337683868781554819821575385685459666842162355673947984514687068626166144076257334426612302554448774082488600083569900006274897032242821388126274957846236552373226099112200392102883351088570736254707966329366625911183721875374731791052229266503696334310835323523568132399330263642353927504971311717117370721838701629885670598853025212521537158141447625623337563164790788106598854822686494249848796441153496412236527242235888308435573209980270776407776277489669763803746640746378181948641\n",
12+
"c2 = 4948422459907576438725352912593232312182623872749480015295307088166392790756090961680588458629287353136729331282506869598853654959933189916541367579979613191505226006688017103736659670745715837820780269669982614187726024837483992949073998289744910800139692315475427811724840888983757813069849711652177078415791290894737059610056340691753379065563574279210755232749774749757141836708161854072798697882671844015773796030086898649043727563289757423417931359190238689436180953442515869613672008678717039516723747808793079592658069533269662834322438864456440701995249381880745586708718334052938634931936240736457181295\n",
13+
"n2 = 8582505375542551134698364096640878629785534004976071646505285128223700755811329156276289439920192196962008222418309136528180402357612976316670896973298407081310073283979903409463559102445223030866575563539261326076167685019121804961393115251287057504682389257841337573435085535013992761172452417731887700665115563173984357419855481847035192853387338980937451843809282267888616833734087813693242841580644645315837196205981207827105545437201799441352173638172133698491126291396194764373021523547130703629001683366722885529834956411976212381935354905525700646776572036418453784898084635925476199878640087165680193737\n",
914
"p,q = 19024691283015651666032297670418553586155390575928421823630922553034857624430114628839720683172187406577114034710093054198921843669645736474448836706112221787749688565566635453151716934583685087745112614898780150391513798368931496744574075511968933800467288441832780919514199410584786925010518564670786685241724643282580795568609339268652910564215887176803735675069372979560024792322029911970574914829712553975379661212645059271137916107885326625543090473004683836665262304916304580076748336858662108554591235698235221618061328251985929904075811056422186525179189846420226944944513865790999242309352900287977666792901,18055722101348711626577381571859114850735298658417345663254295930584841136416234624852520581982069555948490061840244710773146585295336094872892685938420880462305333393436098181186277450475949236132458958671804132443554885896037342335902958516394876382378829317303693655605215373555988755516058130500801822723195474873517960624159417903134580987202400855946137101429970119186394052011747475879598126195607938106163892658285305921071673588966184054026228745012993740035399652049777986535759039077634555909031397541116025395236871778797949216479130412500655359057128438928721459688727543057760739527720641179290282309741\n",
1015
"primes = [10864078180916418691, 10049235158029375571, 12448177342966243757, 17093292308638969889, 16123604149048919099, 15175734709842430433, 18106525049998616747, 11092420583960163379, 15751974537676958401, 18345408081492711641, 14745811312384518031, 9949603102225364603, 12034779627328165471, 13062839684118954553, 14813953870710226847, 15332916111580607077, 17265001711647542137, 16870346804576162551, 15211380502610462057, 14678737767649343977, 13645878578452317313, 18390046459144888243, 14397830993057803133, 17543713628803023199, 16408421615173973083, 17673334943789572513, 12404642343676224637, 10547615587767500213, 11853704782834170959, 17289161209347211817, 10596280721192026229, 14619040595108594017]\n"
1116
]
17+
},
18+
{
19+
"cell_type": "code",
20+
"execution_count": 9,
21+
"metadata": {},
22+
"outputs": [
23+
{
24+
"data": {
25+
"text/plain": [
26+
"b'moectf{fACtord6_And_YAfu_Are_6oth_good_utils_to_fACtorize_num6ers_ff90S}'"
27+
]
28+
},
29+
"execution_count": 9,
30+
"metadata": {},
31+
"output_type": "execute_result"
32+
}
33+
],
34+
"source": [
35+
"from math import prod\n",
36+
"m1 = pow(c1,pow(e,-1,(p-1)*(q-1)),n1)\n",
37+
"m2 = pow(c2,pow(e,-1,prod(x-1 for x in primes)),n2)\n",
38+
"int.to_bytes(m1,byteorder='big',length=m1.bit_length()//8+1)+int.to_bytes(m2,byteorder='big',length=m2.bit_length()//8+1)"
39+
]
40+
},
41+
{
42+
"cell_type": "code",
43+
"execution_count": 7,
44+
"metadata": {},
45+
"outputs": [
46+
{
47+
"data": {
48+
"text/plain": [
49+
"b'od_utils_to_fACtorize_num6ers_ff90S}'"
50+
]
51+
},
52+
"execution_count": 7,
53+
"metadata": {},
54+
"output_type": "execute_result"
55+
}
56+
],
57+
"source": []
58+
},
59+
{
60+
"cell_type": "code",
61+
"execution_count": null,
62+
"metadata": {},
63+
"outputs": [],
64+
"source": []
1265
}
1366
],
1467
"metadata": {
68+
"kernelspec": {
69+
"display_name": "venv",
70+
"language": "python",
71+
"name": "python3"
72+
},
1573
"language_info": {
16-
"name": "python"
74+
"codemirror_mode": {
75+
"name": "ipython",
76+
"version": 3
77+
},
78+
"file_extension": ".py",
79+
"mimetype": "text/x-python",
80+
"name": "python",
81+
"nbconvert_exporter": "python",
82+
"pygments_lexer": "ipython3",
83+
"version": "3.11.6"
1784
},
1885
"orig_nbformat": 4
1986
},

Challenges/Crypto/giant_e/solve.ipynb

+16-45
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,12 @@
22
"cells": [
33
{
44
"cell_type": "code",
5-
"execution_count": 3,
5+
"execution_count": 2,
66
"metadata": {},
77
"outputs": [],
88
"source": [
9-
"from Crypto.Util.number import getPrime\n",
109
"from gmpy2 import iroot\n",
11-
"from sympy import nextprime\n",
1210
"\n",
13-
"with open(\"flag.txt\",\"rb\") as fs:\n",
14-
" flag = fs.read().strip()\n",
15-
"\n",
16-
"p = getPrime(1024)\n",
17-
"q = getPrime(1024)\n",
18-
"n = p * q\n",
19-
"phi = (p-1)*(q-1)\n",
20-
"d = nextprime(int(iroot(n,4)[0] // 4))\n",
21-
"e = pow(d,-1,phi)\n",
22-
"m = int.from_bytes(flag,\"big\")\n",
23-
"c = pow(m,e,n)\n"
24-
]
25-
},
26-
{
27-
"cell_type": "code",
28-
"execution_count": 9,
29-
"metadata": {},
30-
"outputs": [],
31-
"source": [
3211
"def rational_to_contfrac(x,y):\n",
3312
" '''\n",
3413
" Converts a rational x/y fraction into\n",
@@ -42,13 +21,12 @@
4221
" pquotients.append(a)\n",
4322
" return pquotients\n",
4423
"\n",
45-
"#TODO: efficient method that calculates convergents on-the-go, without doing partial quotients first\n",
4624
"def convergents_from_contfrac(frac):\n",
4725
" '''\n",
4826
" computes the list of convergents\n",
4927
" using the list of partial quotients\n",
5028
" '''\n",
51-
" convs = [];\n",
29+
" convs = []\n",
5230
" for i in range(len(frac)):\n",
5331
" convs.append(contfrac_to_rational(frac[0:i]))\n",
5432
" return convs\n",
@@ -90,41 +68,34 @@
9068
},
9169
{
9270
"cell_type": "code",
93-
"execution_count": 14,
71+
"execution_count": 8,
9472
"metadata": {},
9573
"outputs": [
9674
{
9775
"name": "stdout",
9876
"output_type": "stream",
9977
"text": [
100-
"Hacked!\n"
78+
"Hacked!\n",
79+
"b'moectf{too_larGe_exponent_is_not_a_iDea_too!_Bung92WPIBung92WPIBung9?WP}'\n"
10180
]
10281
}
10382
],
10483
"source": [
105-
"assert hack_RSA(e,n) == d\n",
106-
"assert pow(c,d,n) == m"
84+
"n = 0xbaa70ba4c29eb1e6bb3458827540fce84d40e1c966db73c0a39e4f9f40e975c42e02971dab385be27bd2b0687e2476894845cc46e55d9747a5be5ca9d925931ca82b0489e39724ea814800eb3c0ea40d89ebe7fe377f8d3f431a68d209e7a149851c06a4e67db7c99fcfd9ec19496f29d59bb186feb44a36fe344f11d047b9435a1c47fa2f8ed72f59403ebb0e439738fd550a7684247ab7da64311690f461e6dce03bf2fcd55345948a3b537087f07cd680d7461d326690bf21e39dff30268cb33f86eeceff412cd63a38f7110805d337dcad25e6f7e3728b53ca722b695b0d9db37361b5b63213af50dd69ee8b3cf2085f845d7932c08b27bf638e98497239\n",
85+
"c = 0x45a9ce4297c8afee693d3cce2525d3399c5251061ddd2462513a57f0fd69bdc74b71b519d3a2c23209d74fcfbcb6b196b5943838c2441cb34496c96e0f9fc9f0f80a2f6d5b49f220cb3e78e36a4a66595aa2dbe3ff6e814d84f07cb5442e2d5d08d08aa9ccde0294b39bfde79a6c6dcd2329e9820744c4deb34a039da7933ddf00b0a0469afb89cba87490a39783a9b2f8f0274f646ca242e78a326dda886c213bc8d03ac1a9150de4ba08c5936c3fe924c8646652ef85aa7ac0103485f472413427a0e9d9a4d416b99e24861ca8499500c693d7a07360158ffffa543480758cafff2a09a9f6628f92767764fa026d48a9dd899838505ae16e38910697f9de14\n",
86+
"e = 0x609778981bfbb26bb93398cb6d96984616a6ab08ade090c1c0d4fedb00f44f0552a1555efec5cc66e7960b61e94e80e7483b9f906a6c8155a91cdc3e4917fa5347c58a2bc85bb160fcf7fe98e3645cfea8458ea209e565e4eb72ee7cbb232331a862d8a84d91a0ff6d74aa3c779b2b129c3d8148b090c4193234764f2e5d9b2170a9b4859501d07c0601cdd18616a0ab2cf713a7c785fd06f27d68dff24446d884644e08f31bd37ecf48750e4324f959a8d37c5bef25e1580851646d57b3d4f525bc04c7ddafdf146539a84703df2161a0da7a368675f473065d2cb661907d990ba4a8451b15e054bfc4dd73e134f3bf7d8fa4716125d8e21f946d16b7b0fc43\n",
87+
"\n",
88+
"d = hack_RSA(e,n)\n",
89+
"m = pow(c,d,n)\n",
90+
"print(int.to_bytes(m, 72, 'big'))"
10791
]
10892
},
10993
{
11094
"cell_type": "code",
111-
"execution_count": 13,
95+
"execution_count": null,
11296
"metadata": {},
113-
"outputs": [
114-
{
115-
"data": {
116-
"text/plain": [
117-
"'0xbaa70ba4c29eb1e6bb3458827540fce84d40e1c966db73c0a39e4f9f40e975c42e02971dab385be27bd2b0687e2476894845cc46e55d9747a5be5ca9d925931ca82b0489e39724ea814800eb3c0ea40d89ebe7fe377f8d3f431a68d209e7a149851c06a4e67db7c99fcfd9ec19496f29d59bb186feb44a36fe344f11d047b9435a1c47fa2f8ed72f59403ebb0e439738fd550a7684247ab7da64311690f461e6dce03bf2fcd55345948a3b537087f07cd680d7461d326690bf21e39dff30268cb33f86eeceff412cd63a38f7110805d337dcad25e6f7e3728b53ca722b695b0d9db37361b5b63213af50dd69ee8b3cf2085f845d7932c08b27bf638e98497239'"
118-
]
119-
},
120-
"execution_count": 13,
121-
"metadata": {},
122-
"output_type": "execute_result"
123-
}
124-
],
125-
"source": [
126-
"hex(n)"
127-
]
97+
"outputs": [],
98+
"source": []
12899
}
129100
],
130101
"metadata": {
@@ -143,7 +114,7 @@
143114
"name": "python",
144115
"nbconvert_exporter": "python",
145116
"pygments_lexer": "ipython3",
146-
"version": "3.11.4"
117+
"version": "3.11.6"
147118
},
148119
"orig_nbformat": 4
149120
},

0 commit comments

Comments
 (0)