Skip to content

Commit ac199da

Browse files
authoredJan 9, 2022
Add files via upload
1 parent c5a945d commit ac199da

File tree

1 file changed

+551
-0
lines changed

1 file changed

+551
-0
lines changed
 

‎Coded Correspondence.ipynb

+551
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,551 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Casual Coded Correspondence: The Project\n",
8+
"\n",
9+
"In this project, you will be working to code and decode various messages between you and your fictional cryptography enthusiast pen pal Vishal. You and Vishal have been exchanging letters for quite some time now and have started to provide a puzzle in each one of your letters. Here is his most recent letter:\n",
10+
"\n",
11+
" Hey there! How have you been? I've been great! I just learned about this really cool type of cipher called a Caesar Cipher. Here's how it works: You take your message, something like \"hello\" and then you shift all of the letters by a certain offset. For example, if I chose an offset of 3 and a message of \"hello\", I would code my message by shifting each letter 3 places to the left (with respect to the alphabet). So \"h\" becomes \"e\", \"e\" becomes, \"b\", \"l\" becomes \"i\", and \"o\" becomes \"l\". Then I have my coded message,\"ebiil\"! Now I can send you my message and the offset and you can decode it. The best thing is that Julius Caesar himself used this cipher, that's why it's called the Caesar Cipher! Isn't that so cool! Okay, now I'm going to send you a longer coded message that you have to decode yourself!\n",
12+
" \n",
13+
" xuo jxuhu! jxyi yi qd unqcfbu ev q squiqh syfxuh. muhu oek qrbu je tusetu yj? y xefu ie! iudt cu q cuiiqwu rqsa myjx jxu iqcu evviuj!\n",
14+
" \n",
15+
" This message has an offset of 10. Can you decode it?\n",
16+
" \n",
17+
"\n",
18+
"#### Step 1: Decode Vishal's Message\n",
19+
"In the cell below, use your Python skills to decode Vishal's message and print the result. Hint: you can account for shifts that go past the end of the alphabet using the modulus operator, but I'll let you figure out how!"
20+
]
21+
},
22+
{
23+
"cell_type": "code",
24+
"execution_count": 2,
25+
"metadata": {},
26+
"outputs": [
27+
{
28+
"name": "stdout",
29+
"output_type": "stream",
30+
"text": [
31+
"hey there! this is an example of a caesar cipher. were you able to decode it? i hope so! send me a message back with the same offset!\n"
32+
]
33+
}
34+
],
35+
"source": [
36+
"# Create Two Strings to House All of the Letters and Punctuation that are Used in Vishal's Message\n",
37+
"alphabet = \"abcdefghijklmnopqrstuvwxyz\"\n",
38+
"punctuation = \"!.?,' \"\n",
39+
"\n",
40+
"# Define a Variable that Contains the String Message\n",
41+
"message = \"xuo jxuhu! jxyi yi qd unqcfbu ev q squiqh syfxuh. muhu oek qrbu je tusetu yj? y xefu ie! iudt cu q cuiiqwu rqsa myjx jxu iqcu evviuj!\"\n",
42+
"\n",
43+
"# Create an Empty String to Which the Decoded Message will be A\n",
44+
"translated_message = \"\"\n",
45+
"\n",
46+
"# Use a For Loop to Iterate through Each Letter in Vishal's Message\n",
47+
"for letter in message:\n",
48+
" # If the letter is not a Punctuation Mark\n",
49+
" if not letter in punctuation:\n",
50+
" # Assign a Numerical Value to the Letter Using the Find Method \n",
51+
" letter_position = alphabet.find(letter)\n",
52+
" # Using the Plus Equals Operator, Add the Alphabet Letter by Indexing its Position and Adding 10\n",
53+
" # If there is a Shift Past the End of the Alphabet, Use the Modulus Operator for 26 to get the Letter\n",
54+
" translated_message += alphabet[(letter_position + 10) % 26]\n",
55+
" # Using an Else Statement to Add Punctuation Items that are Not Letters to the Translated Message\n",
56+
" else:\n",
57+
" translated_message += letter\n",
58+
"print(translated_message)\n"
59+
]
60+
},
61+
{
62+
"cell_type": "markdown",
63+
"metadata": {},
64+
"source": [
65+
"#### Step 2: Send Vishal a Coded Message\n",
66+
"Great job! Now send Vishal back a message using the same offset. Your message can be anything you want! Remember, coding happens in opposite direction of decoding."
67+
]
68+
},
69+
{
70+
"cell_type": "code",
71+
"execution_count": 4,
72+
"metadata": {},
73+
"outputs": [
74+
{
75+
"name": "stdout",
76+
"output_type": "stream",
77+
"text": [
78+
"xy lyixqb! y wej oekh cuiiqwu. jxyi yi q fhujjo seeb mqo je secckdysqju, y tekrj qdoedu sqd jubb mxqj mu'hu iqoydw!\n"
79+
]
80+
}
81+
],
82+
"source": [
83+
"# Define a Variable that Contains the String Message to Vishal\n",
84+
"message_to_vishal = \"hi vishal! i got your message. this is a pretty cool way to communicate, i doubt anyone can tell what we're saying!\"\n",
85+
"\n",
86+
"# Create an Empty String to Which the Coded Message will be Added\n",
87+
"translated_message = \"\"\n",
88+
"\n",
89+
"# Use a For Loop to Iterate through Each Letter in My Message to Vishal\n",
90+
"for letter in message_to_vishal:\n",
91+
" # If the letter is not a Punctuation Mark\n",
92+
" if not letter in punctuation:\n",
93+
" # Assign a Numerical Value to the Letter Using the Find Method \n",
94+
" letter_position = alphabet.find(letter)\n",
95+
" # Using the Plus Equals Operator, Add the Alphabet Letter by Indexing its Position and Subtracting 10\n",
96+
" # If there is a Shift Past the End of the Alphabet, Use the Modulus Operator for 26 to get the Letter\n",
97+
" translated_message += alphabet[(letter_position - 10) % 26]\n",
98+
" # Using an Else Statement to Add Punctuation Items that are Not Letters to My Message to Vishal\n",
99+
" else:\n",
100+
" translated_message += letter\n",
101+
"print(translated_message)"
102+
]
103+
},
104+
{
105+
"cell_type": "markdown",
106+
"metadata": {},
107+
"source": [
108+
"#### Step 3: Make functions for decoding and coding \n",
109+
"\n",
110+
"Vishal sent over another reply, this time with two coded messages!\n",
111+
" \n",
112+
" You're getting the hang of this! Okay here are two more messages, the first one is coded just like before with an offset of ten, and it contains the hint for decoding the second message!\n",
113+
" \n",
114+
" First message:\n",
115+
" \n",
116+
" jxu evviuj veh jxu iusedt cuiiqwu yi vekhjuud.\n",
117+
" \n",
118+
" Second message:\n",
119+
" \n",
120+
" bqdradyuzs ygxfubxq omqemd oubtqde fa oapq kagd yqeemsqe ue qhqz yadq eqogdq!\n",
121+
" \n",
122+
"Decode both of these messages. \n",
123+
"\n",
124+
"If you haven't already, define two functions `decoder(message, offset)` and `coder(message, offset)` that can be used to quickly decode and code messages given any offset."
125+
]
126+
},
127+
{
128+
"cell_type": "code",
129+
"execution_count": 6,
130+
"metadata": {},
131+
"outputs": [
132+
{
133+
"name": "stdout",
134+
"output_type": "stream",
135+
"text": [
136+
"the offset for the second message is fourteen.\n"
137+
]
138+
}
139+
],
140+
"source": [
141+
"# Define a Function with 2 Parameters for the Input Message and the Offset Amount\n",
142+
"def decoder(message, offset):\n",
143+
" translated_message = \"\"\n",
144+
" # Use a For Loop to Iterate through Each Letter in the Coded Input Message\n",
145+
" for letter in message:\n",
146+
" # If the letter is not a Punctuation Mark\n",
147+
" if not letter in punctuation:\n",
148+
" # Assign a Numerical Value to the Letter Using the Find Method \n",
149+
" letter_position = alphabet.find(letter)\n",
150+
" # Using the Plus Equals Operator, Add the Alphabet Letter by Indexing its Position and Adding 10\n",
151+
" # If there is a Shift Past the End of the Alphabet, Use the Modulus Operator for 26 to get the Letter\n",
152+
" translated_message += alphabet[(letter_position + offset) % 26]\n",
153+
" # Using an Else Statement to Add Punctuation Items that are Not Letters to the Translated Message\n",
154+
" else:\n",
155+
" translated_message += letter\n",
156+
" return translated_message\n",
157+
" \n",
158+
"# Define a Function with 2 Parameters for the Input Message and the Offset Amount \n",
159+
"def coder(message, offset):\n",
160+
" translated_message = \"\"\n",
161+
" # Use a For Loop to Iterate through Each Letter in the Decoded Input Message\n",
162+
" for letter in message:\n",
163+
" # If the letter is not a Punctuation Mark\n",
164+
" if not letter in punctuation:\n",
165+
" # Assign a Numerical Value to the Letter Using the Find Method\n",
166+
" letter_position = alphabet.find(letter)\n",
167+
" # Using the Plus Equals Operator, Add the Alphabet Letter by Indexing its Position and Subtracting 10\n",
168+
" # If there is a Shift Past the End of the Alphabet, Use the Modulus Operator for 26 to get the Letter\n",
169+
" translated_message += alphabet[(letter_position - offset) % 26]\n",
170+
" # Using an Else Statement to Add Punctuation Items that are Not Letters to the Translated Message\n",
171+
" else:\n",
172+
" translated_message += letter\n",
173+
" return translated_message\n",
174+
"\n",
175+
"first_message = \"jxu evviuj veh jxu iusedt cuiiqwu yi vekhjuud.\"\n",
176+
"\n",
177+
"# Use the Decoder Function to Determine the Message from Vishal with the Offset of 10 Like Before\n",
178+
"\n",
179+
"print(decoder(first_message, 10))"
180+
]
181+
},
182+
{
183+
"cell_type": "code",
184+
"execution_count": 7,
185+
"metadata": {},
186+
"outputs": [
187+
{
188+
"name": "stdout",
189+
"output_type": "stream",
190+
"text": [
191+
"performing multiple caesar ciphers to code your messages is even more secure!\n"
192+
]
193+
}
194+
],
195+
"source": [
196+
"# Use the Decoded First Message and the New Offset of 14 to determine the Translated Second Message\n",
197+
"\n",
198+
"second_message = \"bqdradyuzs ygxfubxq omqemd oubtqde fa oapq kagd yqeemsqe ue qhqz yadq eqogdq!\"\n",
199+
"print(decoder(second_message, 14))\n",
200+
" "
201+
]
202+
},
203+
{
204+
"cell_type": "markdown",
205+
"metadata": {},
206+
"source": [
207+
"#### Step 4: Solving a Caesar Cipher without knowing the shift value\n",
208+
"\n",
209+
"Awesome work! While you were working to decode his last two messages, Vishal sent over another letter! He's really been bitten by the crytpo-bug. Read it and see what interesting task he has lined up for you this time.\n",
210+
"\n",
211+
" Hello again friend! I knew you would love the Caesar Cipher, it's a cool simple way to encrypt messages. Did you know that back in Caesar's time, it was considered a very secure way of communication and it took a lot of effort to crack if you were unaware of the value of the shift? That's all changed with computers! Now we can brute force these kinds of ciphers very quickly, as I'm sure you can imagine.\n",
212+
" \n",
213+
" To test your cryptography skills, this next coded message is going to be harder than the last couple to crack. It's still going to be coded with a Caesar Cipher but this time I'm not going to tell you the value of the shift. You'll have to brute force it yourself.\n",
214+
" \n",
215+
" Here's the coded message:\n",
216+
" \n",
217+
" vhfinmxkl atox kxgwxkxw tee hy maxlx hew vbiaxkl tl hulhexmx. px'ee atox mh kxteer lmxi ni hnk ztfx by px ptgm mh dxxi hnk fxlltzxl ltyx.\n",
218+
" \n",
219+
" Good luck!\n",
220+
" \n",
221+
"Decode Vishal's most recent message and see what it says!"
222+
]
223+
},
224+
{
225+
"cell_type": "code",
226+
"execution_count": 12,
227+
"metadata": {},
228+
"outputs": [
229+
{
230+
"name": "stdout",
231+
"output_type": "stream",
232+
"text": [
233+
"offest = 1\n",
234+
"\n",
235+
"wigjonylm bupy lyhxylyx uff iz nbymy ifx wcjbylm um ivmifyny. qy'ff bupy ni lyuffs mnyj oj iol augy cz qy quhn ni eyyj iol gymmuaym muzy.\n",
236+
"\n",
237+
"offest = 2\n",
238+
"\n",
239+
"xjhkpozmn cvqz mziyzmzy vgg ja ocznz jgy xdkczmn vn jwnjgzoz. rz'gg cvqz oj mzvggt nozk pk jpm bvhz da rz rvio oj fzzk jpm hznnvbzn nvaz.\n",
240+
"\n",
241+
"offest = 3\n",
242+
"\n",
243+
"ykilqpano dwra najzanaz whh kb pdaoa khz yeldano wo kxokhapa. sa'hh dwra pk nawhhu opal ql kqn cwia eb sa swjp pk gaal kqn iaoowcao owba.\n",
244+
"\n",
245+
"offest = 4\n",
246+
"\n",
247+
"zljmrqbop exsb obkaboba xii lc qebpb lia zfmebop xp lyplibqb. tb'ii exsb ql obxiiv pqbm rm lro dxjb fc tb txkq ql hbbm lro jbppxdbp pxcb.\n",
248+
"\n",
249+
"offest = 5\n",
250+
"\n",
251+
"amknsrcpq fytc pclbcpcb yjj md rfcqc mjb agnfcpq yq mzqmjcrc. uc'jj fytc rm pcyjjw qrcn sn msp eykc gd uc uylr rm iccn msp kcqqyecq qydc.\n",
252+
"\n",
253+
"offest = 6\n",
254+
"\n",
255+
"bnlotsdqr gzud qdmcdqdc zkk ne sgdrd nkc bhogdqr zr narnkdsd. vd'kk gzud sn qdzkkx rsdo to ntq fzld he vd vzms sn jddo ntq ldrrzfdr rzed.\n",
256+
"\n",
257+
"offest = 7\n",
258+
"\n",
259+
"computers have rendered all of these old ciphers as obsolete. we'll have to really step up our game if we want to keep our messages safe.\n",
260+
"\n",
261+
"offest = 8\n",
262+
"\n",
263+
"dpnqvufst ibwf sfoefsfe bmm pg uiftf pme djqifst bt pctpmfuf. xf'mm ibwf up sfbmmz tufq vq pvs hbnf jg xf xbou up lffq pvs nfttbhft tbgf.\n",
264+
"\n",
265+
"offest = 9\n",
266+
"\n",
267+
"eqorwvgtu jcxg tgpfgtgf cnn qh vjgug qnf ekrjgtu cu qduqngvg. yg'nn jcxg vq tgcnna uvgr wr qwt icog kh yg ycpv vq mggr qwt oguucigu uchg.\n",
268+
"\n",
269+
"offest = 10\n",
270+
"\n",
271+
"frpsxwhuv kdyh uhqghuhg doo ri wkhvh rog flskhuv dv revrohwh. zh'oo kdyh wr uhdoob vwhs xs rxu jdph li zh zdqw wr nhhs rxu phvvdjhv vdih.\n",
272+
"\n",
273+
"offest = 11\n",
274+
"\n",
275+
"gsqtyxivw lezi virhivih epp sj xliwi sph gmtlivw ew sfwspixi. ai'pp lezi xs vieppc wxit yt syv keqi mj ai aerx xs oiit syv qiwwekiw weji.\n",
276+
"\n",
277+
"offest = 12\n",
278+
"\n",
279+
"htruzyjwx mfaj wjsijwji fqq tk ymjxj tqi hnumjwx fx tgxtqjyj. bj'qq mfaj yt wjfqqd xyju zu tzw lfrj nk bj bfsy yt pjju tzw rjxxfljx xfkj.\n",
280+
"\n",
281+
"offest = 13\n",
282+
"\n",
283+
"iusvazkxy ngbk xktjkxkj grr ul znkyk urj iovnkxy gy uhyurkzk. ck'rr ngbk zu xkgrre yzkv av uax mgsk ol ck cgtz zu qkkv uax skyygmky yglk.\n",
284+
"\n",
285+
"offest = 14\n",
286+
"\n",
287+
"jvtwbalyz ohcl yluklylk hss vm aolzl vsk jpwolyz hz vizvslal. dl'ss ohcl av ylhssf zalw bw vby nhtl pm dl dhua av rllw vby tlzzhnlz zhml.\n",
288+
"\n",
289+
"offest = 15\n",
290+
"\n",
291+
"kwuxcbmza pidm zmvlmzml itt wn bpmam wtl kqxpmza ia wjawtmbm. em'tt pidm bw zmittg abmx cx wcz oium qn em eivb bw smmx wcz umaaioma ainm.\n",
292+
"\n",
293+
"offest = 16\n",
294+
"\n",
295+
"lxvydcnab qjen anwmnanm juu xo cqnbn xum lryqnab jb xkbxuncn. fn'uu qjen cx anjuuh bcny dy xda pjvn ro fn fjwc cx tnny xda vnbbjpnb bjon.\n",
296+
"\n",
297+
"offest = 17\n",
298+
"\n",
299+
"mywzedobc rkfo boxnobon kvv yp droco yvn mszrobc kc ylcyvodo. go'vv rkfo dy bokvvi cdoz ez yeb qkwo sp go gkxd dy uooz yeb wocckqoc ckpo.\n",
300+
"\n",
301+
"offest = 18\n",
302+
"\n",
303+
"nzxafepcd slgp cpyopcpo lww zq espdp zwo ntaspcd ld zmdzwpep. hp'ww slgp ez cplwwj depa fa zfc rlxp tq hp hlye ez vppa zfc xpddlrpd dlqp.\n",
304+
"\n",
305+
"offest = 19\n",
306+
"\n",
307+
"oaybgfqde tmhq dqzpqdqp mxx ar ftqeq axp oubtqde me aneaxqfq. iq'xx tmhq fa dqmxxk efqb gb agd smyq ur iq imzf fa wqqb agd yqeemsqe emrq.\n",
308+
"\n",
309+
"offest = 20\n",
310+
"\n",
311+
"pbzchgref unir eraqrerq nyy bs gurfr byq pvcuref nf bofbyrgr. jr'yy unir gb ernyyl fgrc hc bhe tnzr vs jr jnag gb xrrc bhe zrffntrf fnsr.\n",
312+
"\n",
313+
"offest = 21\n",
314+
"\n",
315+
"qcadihsfg vojs fsbrsfsr ozz ct hvsgs czr qwdvsfg og cpgczshs. ks'zz vojs hc fsozzm ghsd id cif uoas wt ks kobh hc yssd cif asggousg gots.\n",
316+
"\n",
317+
"offest = 22\n",
318+
"\n",
319+
"rdbejitgh wpkt gtcstgts paa du iwtht das rxewtgh ph dqhdatit. lt'aa wpkt id gtpaan hite je djg vpbt xu lt lpci id ztte djg bthhpvth hput.\n",
320+
"\n",
321+
"offest = 23\n",
322+
"\n",
323+
"secfkjuhi xqlu hudtuhut qbb ev jxuiu ebt syfxuhi qi eriebuju. mu'bb xqlu je huqbbo ijuf kf ekh wqcu yv mu mqdj je auuf ekh cuiiqwui iqvu.\n",
324+
"\n",
325+
"offest = 24\n",
326+
"\n",
327+
"tfdglkvij yrmv iveuvivu rcc fw kyvjv fcu tzgyvij rj fsjfcvkv. nv'cc yrmv kf ivrccp jkvg lg fli xrdv zw nv nrek kf bvvg fli dvjjrxvj jrwv.\n",
328+
"\n",
329+
"offest = 25\n",
330+
"\n",
331+
"ugehmlwjk zsnw jwfvwjwv sdd gx lzwkw gdv uahzwjk sk gtkgdwlw. ow'dd zsnw lg jwsddq klwh mh gmj ysew ax ow osfl lg cwwh gmj ewkksywk ksxw.\n",
332+
"\n"
333+
]
334+
}
335+
],
336+
"source": [
337+
"coded_message = \"vhfinmxkl atox kxgwxkxw tee hy maxlx hew vbiaxkl tl hulhexmx. px'ee atox mh kxteer lmxi ni hnk ztfx by px ptgm mh dxxi hnk fxlltzxl ltyx.\"\n",
338+
"\n",
339+
"# To Use Brute Force to Decode the Message Means to Run Through Each Possible Shift and Return the Output Until\n",
340+
"# There is a Message that is Readable in English. This will Involve Creating a Range and Iterating through\n",
341+
"# Each Offset Shift in the Range\n",
342+
"\n",
343+
"# Create a For Loop to Iterate through All Possible Offesets using a Range Function\n",
344+
"for i in range(1,26):\n",
345+
" # Print Statement to ID which Offset is Being Used, Convert Integer to String\n",
346+
" print(\"offest = \" + str(i))\n",
347+
" # Print while Using Line Skips to More Easily Read Output\n",
348+
" print(\"\\n\" + decoder(coded_message, i) + \"\\n\")\n",
349+
" \n",
350+
" # Offset 7 is Being Used for this Particular Caesar Cipher "
351+
]
352+
},
353+
{
354+
"cell_type": "markdown",
355+
"metadata": {},
356+
"source": [
357+
"#### Step 5: The Vigenère Cipher\n",
358+
"\n",
359+
"Great work! While you were working on the brute force cracking of the cipher, Vishal sent over another letter. That guy is a letter machine!\n",
360+
"\n",
361+
" Salutations! As you can see, technology has made brute forcing simple ciphers like the Caesar Cipher extremely easy, and us crypto-enthusiasts have had to get more creative and use more complicated ciphers. This next cipher I'm going to teach you is the Vigenère Cipher, invented by an Italian cryptologist named Giovan Battista Bellaso (cool name eh?) in the 16th century, but named after another cryptologist from the 16th century, Blaise de Vigenère.\n",
362+
" \n",
363+
" The Vigenère Cipher is a polyalphabetic substitution cipher, as opposed to the Caesar Cipher which was a monoalphabetic substitution cipher. What this means is that opposed to having a single shift that is applied to every letter, the Vigenère Cipher has a different shift for each individual letter. The value of the shift for each letter is determined by a given keyword.\n",
364+
" \n",
365+
" Consider the message\n",
366+
" \n",
367+
" barry is the spy\n",
368+
"\n",
369+
" If we want to code this message, first we choose a keyword. For this example, we'll use the keyword\n",
370+
" \n",
371+
" dog\n",
372+
" \n",
373+
" Now we use the repeat the keyword over and over to generate a _keyword phrase_ that is the same length as the message we want to code. So if we want to code the message \"barry is the spy\" our _keyword phrase_ is \"dogdo gd ogd ogd\". Now we are ready to start coding our message. We shift the each letter of our message by the place value of the corresponding letter in the keyword phrase, assuming that \"a\" has a place value of 0, \"b\" has a place value of 1, and so forth. Remember, we zero-index because this is Python we're talking about!\n",
374+
"\n",
375+
" message: b a r r y i s t h e s p y\n",
376+
" \n",
377+
" keyword phrase: d o g d o g d o g d o g d\n",
378+
" \n",
379+
" resulting place value: 4 14 15 12 16 24 11 21 25 22 22 17 5\n",
380+
" \n",
381+
" So we shift \"b\", which has an index of 1, by the index of \"d\", which is 3. This gives us an place value of 4, which is \"e\". Then continue the trend: we shift \"a\" by the place value of \"o\", 14, and get \"o\" again, we shift \"r\" by the place value of \"g\", 15, and get \"x\", shift the next \"r\" by 12 places and \"u\", and so forth. Once we complete all the shifts we end up with our coded message:\n",
382+
" \n",
383+
" eoxum ov hnh gvb\n",
384+
" \n",
385+
" As you can imagine, this is a lot harder to crack without knowing the keyword! So now comes the hard part. I'll give you a message and the keyword, and you'll see if you can figure out how to crack it! Ready? Okay here's my message:\n",
386+
" \n",
387+
" dfc aruw fsti gr vjtwhr wznj? vmph otis! cbx swv jipreneo uhllj kpi rahjib eg fjdkwkedhmp!\n",
388+
" \n",
389+
" and the keyword to decode my message is \n",
390+
" \n",
391+
" friends\n",
392+
" \n",
393+
" Because that's what we are! Good luck friend!\n",
394+
" \n",
395+
"And there it is. Vishal has given you quite the assignment this time! Try to decode his message. It may be helpful to create a function that takes two parameters, the coded message and the keyword and then work towards a solution from there.\n",
396+
"\n",
397+
"**NOTE:** Watch out for spaces and punctuation! When there's a space or punctuation mark in the original message, there should be a space/punctuation mark in the corresponding repeated-keyword string as well! "
398+
]
399+
},
400+
{
401+
"cell_type": "code",
402+
"execution_count": 14,
403+
"metadata": {},
404+
"outputs": [
405+
{
406+
"name": "stdout",
407+
"output_type": "stream",
408+
"text": [
409+
"you were able to decode this? nice work! you are becoming quite the expert at crytography!\n"
410+
]
411+
}
412+
],
413+
"source": [
414+
"# Define a Function to Accept 2 Parameters, a Coded Message and Keyword that is the Value of the Shift\n",
415+
"\n",
416+
"def vigenere_decoder(coded_message, keyword):\n",
417+
" # Define 2 Variables, one for the Index of the Letter being Iterated and the Other for the Generated Keyword Phrase\n",
418+
" letter_index = 0\n",
419+
" keyword_phrase = ''\n",
420+
" # Use a For Loop to Iterate the Length of the Coded Message\n",
421+
" for i in range(0,len(coded_message)):\n",
422+
" # If the Character in the Message is a Punctuation, Add it to the Keyword Phrase using Plus Equals Operator\n",
423+
" if coded_message[i] in punctuation:\n",
424+
" keyword_phrase += coded_message[i]\n",
425+
" # Use an Else Statement if it is Not Punctuation to Add the Indexed Keyword Letter to the Keyword Phrase\n",
426+
" else:\n",
427+
" keyword_phrase += keyword[letter_index]\n",
428+
" # Advance the Letter Index by One Place for Each Iteration, Ensure it Falls in the Range with a Modulus\n",
429+
" letter_index = (letter_index+1)%len(keyword)\n",
430+
" # Define an Empty Variable to Hold the String of the Translated Message\n",
431+
" translated_message = \"\"\n",
432+
" \n",
433+
" # Use Another For Loop to Now Add to the Translated Message Variable, Iterating the Length of the Input Message\n",
434+
" for i in range(0,len(coded_message)): \n",
435+
" # For All Letters that Are Not Punctuation\n",
436+
" if coded_message[i] not in punctuation:\n",
437+
" # Create a Variable to Hold the Value of the Character in the Coded Message and Subtract the Alphanumeric\n",
438+
" # Value of the Keyword Phrase for the Letter Being Iterated\n",
439+
" length = alphabet.find(coded_message[i]) - alphabet.find(keyword_phrase[i])\n",
440+
" # Add the Decoded Letter to the Translated Message using the Plus Equals Operator at Each Index\n",
441+
" translated_message += alphabet[length % 26]\n",
442+
" # Use an Else Statement to Otherwise Add Punctuation\n",
443+
" else:\n",
444+
" translated_message += coded_message[i]\n",
445+
" return translated_message\n",
446+
"\n",
447+
"message = \"dfc aruw fsti gr vjtwhr wznj? vmph otis! cbx swv jipreneo uhllj kpi rahjib eg fjdkwkedhmp!\"\n",
448+
"keyword = \"friends\"\n",
449+
"\n",
450+
"print(vigenere_decoder(message, keyword))"
451+
]
452+
},
453+
{
454+
"cell_type": "markdown",
455+
"metadata": {
456+
"collapsed": true
457+
},
458+
"source": [
459+
"#### Step 6: Send a message with the Vigenère Cipher\n",
460+
"Great work decoding the message. For your final task, write a function that can encode a message using a given keyword and write out a message to send to Vishal!\n",
461+
"\n",
462+
"*As a bonus, try calling your decoder function on the result of your encryption function. You should get the original message back!*"
463+
]
464+
},
465+
{
466+
"cell_type": "code",
467+
"execution_count": 16,
468+
"metadata": {},
469+
"outputs": [
470+
{
471+
"name": "stdout",
472+
"output_type": "stream",
473+
"text": [
474+
"k dogpo loig uehic fsrp hbxi es rb voie atxvbwa yay! elwf kz aiidsar!\n",
475+
"i would have never been able to do this without you! this is awesome!\n"
476+
]
477+
}
478+
],
479+
"source": [
480+
"# Running the Coder Function to Create a Message for Vishal using the Vigenère Cipher\n",
481+
"\n",
482+
"# Define a Function to Accept 2 Parameters, an Uncoded Message and Keyword that is the Value of the Shift\n",
483+
"def vigenere_coder(message, keyword):\n",
484+
" # Define 2 Variables, one for the Index of the Letter being Iterated and the Other for the Generated Keyword Phrase\n",
485+
" letter_index = 0\n",
486+
" keyword_phrase = ''\n",
487+
" # Use a For Loop to Iterate the Length of the Uncoded Message\n",
488+
" for i in range(0,len(message)):\n",
489+
" # If the Character in the Message is a Punctuation, Add it to the Keyword Phrase using Plus Equals Operator\n",
490+
" if message[i] in punctuation:\n",
491+
" keyword_phrase += message[i]\n",
492+
" # Use an Else Statement if it is Not Punctuation to Add the Indexed Keyword Letter to the Keyword Phrase\n",
493+
" else:\n",
494+
" keyword_phrase += keyword[letter_index]\n",
495+
" # Advance the Letter Index by One Place for Each Iteration, Ensure it Falls in the Range with a Modulus\n",
496+
" letter_index = (letter_index+1)%len(keyword)\n",
497+
" # Define an Empty Variable to Hold the String of the Translated Message\n",
498+
" translated_message = \"\"\n",
499+
" \n",
500+
" # Use Another For Loop to Now Add to the Translated Message Variable, Iterating the Length of the Input Message\n",
501+
" for i in range(0,len(message)):\n",
502+
" # For All Letters that Are Not Punctuation\n",
503+
" if message[i] not in punctuation:\n",
504+
" # Create a Variable to Hold the Value of the Character in the Uncoded Message and Subtract the Alphanumeric\n",
505+
" # Value of the Keyword Phrase for the Letter Being Iterated\n",
506+
" length = alphabet.find(message[i]) + alphabet.find(keyword_phrase[i])\n",
507+
" # Add the Coded Letter to the Translated Message using the Plus Equals Operator at Each Index\n",
508+
" translated_message += alphabet[length % 26]\n",
509+
" # Use an Else Statement to Otherwise Add Punctuation\n",
510+
" else:\n",
511+
" translated_message += message[i]\n",
512+
" return translated_message\n",
513+
"\n",
514+
"message_for_vishal = \"i would have never been able to do this without you! this is awesome!\"\n",
515+
"keyword = \"chameleon\"\n",
516+
"\n",
517+
"print(vigenere_coder(message_for_vishal, keyword))\n",
518+
"print(vigenere_decoder(vigenere_coder(message_for_vishal, keyword), keyword))"
519+
]
520+
},
521+
{
522+
"cell_type": "markdown",
523+
"metadata": {},
524+
"source": [
525+
"#### Conclusion\n",
526+
"Over the course of this project you've learned about two different cipher methods and have used your Python skills to code and decode messages. There are all types of other facinating ciphers out there to explore, and Python is the perfect language to implement them with, so go exploring! "
527+
]
528+
}
529+
],
530+
"metadata": {
531+
"kernelspec": {
532+
"display_name": "Python 3 (ipykernel)",
533+
"language": "python",
534+
"name": "python3"
535+
},
536+
"language_info": {
537+
"codemirror_mode": {
538+
"name": "ipython",
539+
"version": 3
540+
},
541+
"file_extension": ".py",
542+
"mimetype": "text/x-python",
543+
"name": "python",
544+
"nbconvert_exporter": "python",
545+
"pygments_lexer": "ipython3",
546+
"version": "3.9.7"
547+
}
548+
},
549+
"nbformat": 4,
550+
"nbformat_minor": 2
551+
}

0 commit comments

Comments
 (0)
Please sign in to comment.