Skip to content

Commit 19a8c92

Browse files
authored
Merge pull request #226 from hardword/master
Add Jupyter Notebook for Simple RSA
2 parents c874107 + 877f11b commit 19a8c92

File tree

1 file changed

+292
-0
lines changed

1 file changed

+292
-0
lines changed

Cryptography/src/RSA/RSA_simple.ipynb

+292
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,292 @@
1+
{
2+
"nbformat": 4,
3+
"nbformat_minor": 0,
4+
"metadata": {
5+
"colab": {
6+
"name": "RSA_simple.ipynb",
7+
"provenance": [],
8+
"collapsed_sections": []
9+
},
10+
"kernelspec": {
11+
"name": "python2",
12+
"display_name": "Python 2"
13+
}
14+
},
15+
"cells": [
16+
{
17+
"cell_type": "markdown",
18+
"metadata": {
19+
"id": "GlSiAIHM5UaN"
20+
},
21+
"source": [
22+
"## RSAaaay (From TAMUctf 2019)\n",
23+
"\n",
24+
"Hey, you're a hacker, right? I think I am too, look at what I made!\n",
25+
"\n",
26+
"---\n",
27+
"```\n",
28+
"(2531257, 43)\n",
29+
"\n",
30+
"My super secret message: \n",
31+
"906851 991083 1780304 2380434 438490 356019 921472 822283 817856 556932 2102538 2501908 2211404 991083 1562919 38268\n",
32+
"```\n",
33+
"---\n",
34+
"\n",
35+
"Problem is, I don't remember how to decrypt it... could you help me out?\n",
36+
"\n",
37+
"*Difficulty: easy*\n",
38+
"\n"
39+
]
40+
},
41+
{
42+
"cell_type": "markdown",
43+
"metadata": {
44+
"id": "imRBoEE408F-"
45+
},
46+
"source": [
47+
"Finding p,q: http://factordb.com/index.php?query=2531257 (Change query param with your N)\n",
48+
"\n",
49+
"Reference: https://github.com/p4-team/ctf/tree/master/2017-02-25-bkp/rsa_buffet"
50+
]
51+
},
52+
{
53+
"cell_type": "code",
54+
"metadata": {
55+
"id": "T3Zpi9mpJj8H"
56+
},
57+
"source": [
58+
"# From https://gist.github.com/JonCooperWorks/5314103\n",
59+
"\n",
60+
"def multiplicative_inverse(e, phi):\n",
61+
" d = 0\n",
62+
" x1 = 0\n",
63+
" x2 = 1\n",
64+
" y1 = 1\n",
65+
" temp_phi = phi\n",
66+
"\n",
67+
" while e > 0:\n",
68+
" temp1 = temp_phi/e\n",
69+
" temp2 = temp_phi - temp1 * e\n",
70+
" temp_phi = e\n",
71+
" e = temp2\n",
72+
"\n",
73+
" x = x2- temp1* x1\n",
74+
" y = d - temp1 * y1\n",
75+
" \n",
76+
" x2 = x1\n",
77+
" x1 = x\n",
78+
" d = y1\n",
79+
" y1 = y\n",
80+
" \n",
81+
" if temp_phi == 1:\n",
82+
" return d + phi\n",
83+
" "
84+
],
85+
"execution_count": null,
86+
"outputs": []
87+
},
88+
{
89+
"cell_type": "code",
90+
"metadata": {
91+
"id": "Ktq4TYOZJpwu",
92+
"outputId": "f07f47a3-7b40-4aa8-94f9-9b67dbc7f74c",
93+
"colab": {
94+
"base_uri": "https://localhost:8080/",
95+
"height": 34
96+
}
97+
},
98+
"source": [
99+
"p=509\n",
100+
"q=4973\n",
101+
"e=43\n",
102+
"phi=(p-1)*(q-1)\n",
103+
"n=p*q\n",
104+
"d=multiplicative_inverse(e, phi)\n",
105+
"\n",
106+
"print d"
107+
],
108+
"execution_count": null,
109+
"outputs": [
110+
{
111+
"output_type": "stream",
112+
"text": [
113+
"2584515\n"
114+
],
115+
"name": "stdout"
116+
}
117+
]
118+
},
119+
{
120+
"cell_type": "code",
121+
"metadata": {
122+
"id": "sfDZWjXTJtDz"
123+
},
124+
"source": [
125+
"enc='906851 991083 1780304 2380434 438490 356019 921472 822283 817856 556932 2102538 2501908 2211404 991083 1562919 38268'\n",
126+
"enc_list=enc.split()\n",
127+
"dec=[]"
128+
],
129+
"execution_count": null,
130+
"outputs": []
131+
},
132+
{
133+
"cell_type": "code",
134+
"metadata": {
135+
"id": "jTfOjnR-Jwrp"
136+
},
137+
"source": [
138+
"for _ in enc_list:\n",
139+
"\tmsg = int(_)**d % n\n",
140+
"\tdec.append(msg)\n",
141+
" "
142+
],
143+
"execution_count": null,
144+
"outputs": []
145+
},
146+
{
147+
"cell_type": "code",
148+
"metadata": {
149+
"id": "zW6gGGQMOInt",
150+
"outputId": "29fc14f3-fbe7-468b-9667-7b784b618724",
151+
"colab": {
152+
"base_uri": "https://localhost:8080/",
153+
"height": 34
154+
}
155+
},
156+
"source": [
157+
"print dec"
158+
],
159+
"execution_count": null,
160+
"outputs": [
161+
{
162+
"output_type": "stream",
163+
"text": [
164+
"[103L, 105103L, 101109L, 12383L, 97118L, 97103L, 10195L, 83105L, 12095L, 70108L, 121105L, 110103L, 9584L, 105103L, 101114L, 115125L]\n"
165+
],
166+
"name": "stdout"
167+
}
168+
]
169+
},
170+
{
171+
"cell_type": "code",
172+
"metadata": {
173+
"id": "S4SgwGUH7TTJ",
174+
"outputId": "215442df-d6e7-4451-c703-b8cb3dff5543",
175+
"colab": {
176+
"base_uri": "https://localhost:8080/",
177+
"height": 34
178+
}
179+
},
180+
"source": [
181+
"dec_new=[103, 105, 103, 101, 109, 123, 83, 97, 118, 97, 103, 101, 95, 83, 105, 120, 95, 70, 108, 121, 105, 110, 103, 95, 84, 105, 103, 101, 114, 115, 125]\n",
182+
"\n",
183+
"dec_str=''\n",
184+
"\n",
185+
"for _ in dec_new:\n",
186+
" dec_str += chr(_)\n",
187+
" \n",
188+
"print dec_str"
189+
],
190+
"execution_count": null,
191+
"outputs": [
192+
{
193+
"output_type": "stream",
194+
"text": [
195+
"gigem{Savage_Six_Flying_Tigers}\n"
196+
],
197+
"name": "stdout"
198+
}
199+
]
200+
},
201+
{
202+
"cell_type": "markdown",
203+
"metadata": {
204+
"id": "_d-E9Viu2mAY"
205+
},
206+
"source": [
207+
"http://www.oxfordmathcenter.com/drupal7/node/206\n"
208+
]
209+
},
210+
{
211+
"cell_type": "code",
212+
"metadata": {
213+
"id": "wRLBGdNJVpD-"
214+
},
215+
"source": [
216+
"def multiplicative_inverse(e, phi):\n",
217+
" d = 0\n",
218+
" x1 = 0\n",
219+
" x2 = 1\n",
220+
" y1 = 1\n",
221+
" temp_phi = phi\n",
222+
"\n",
223+
" while e > 0:\n",
224+
" temp1 = temp_phi/e\n",
225+
" temp2 = temp_phi - temp1 * e\n",
226+
" temp_phi = e\n",
227+
" e = temp2\n",
228+
"\n",
229+
" x = x2- temp1* x1\n",
230+
" y = d - temp1 * y1\n",
231+
" \n",
232+
" x2 = x1\n",
233+
" x1 = x\n",
234+
" d = y1\n",
235+
" y1 = y\n",
236+
" \n",
237+
" if temp_phi == 1:\n",
238+
" return d + phi"
239+
],
240+
"execution_count": null,
241+
"outputs": []
242+
},
243+
{
244+
"cell_type": "code",
245+
"metadata": {
246+
"id": "nasUAOZXVgpZ",
247+
"outputId": "08f249d5-3e9e-4355-cb24-57065cbd44ba",
248+
"colab": {
249+
"base_uri": "https://localhost:8080/",
250+
"height": 34
251+
}
252+
},
253+
"source": [
254+
"p=45000107\n",
255+
"q=48000089\n",
256+
"e=17\n",
257+
"phi=(p-1)*(q-1)\n",
258+
"n=p*q\n",
259+
"\n",
260+
"print multiplicative_inverse(e, phi) - phi\n"
261+
],
262+
"execution_count": null,
263+
"outputs": [
264+
{
265+
"output_type": "stream",
266+
"text": [
267+
"635296778826273\n"
268+
],
269+
"name": "stdout"
270+
}
271+
]
272+
},
273+
{
274+
"cell_type": "markdown",
275+
"metadata": {
276+
"id": "zzdNpry0gHR3"
277+
},
278+
"source": [
279+
"VIRSECCON CTF 2020 - Classic\n",
280+
"\n",
281+
"```\n",
282+
"n = 77627938360345301510724699969247652387657633828943576274039402978346703944383\n",
283+
"\n",
284+
"e = 65537\n",
285+
"\n",
286+
"c = 62899945974090753231979111677615029855602721049941681356856158761811378918268\n",
287+
"```\n",
288+
"\n"
289+
]
290+
}
291+
]
292+
}

0 commit comments

Comments
 (0)