{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# CRYPTOLOGY LAB 1\n", "\n", "_______________\n", "\n", "##### Experiment 1.1\n", "\n", "Glance over the code and comments below. The comments are the sentences that follow the # symbol and explain what the code is doing. Read all the comments.\n", "\n", "Write a few questions and observations about how this code is working. \n", "__________________" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Well look at this. Your code cracking skills are super duper!\n", "LTAA ADDZ PI IWXH. NDJG RDST RGPRZXCV HZXAAH PGT HJETG SJETG!\n", "WELL LOOK AT THIS. YOUR CODE CRACKING SKILLS ARE SUPER DUPER!\n" ] } ], "source": [ "# The alphabet is the list of symobls that you want to be able to translate using your cipher.\n", "# This list of letters is called a string. Whenever you enter a string into python it must have quotes around it\n", "# Can you spot other strings farther down in this code?\n", "alphabet = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ.'!() \" # Note the space at the end, which I kept missing.\n", "\n", "# The key is used to encode plaintext to ciphertext. It is also used to decode ciphertext to plain text \n", "# The key MUST contain all the alphabet entries once!\n", "# This key encodes a Ceasar cipher that shifts the letters by +10 positions. \n", "# A, the letter in position 1, gets shifted to K the letter in position 11 = 1+10, and so on.\n", "key = \"PQRSTUVWXYZABCDEFGHIJKLMNO.'!() \"\n", "\n", "plaintext = \"Well look at this. Your code cracking skills are super duper!\"\n", "\n", "# the commands below define a function that encrypts the plaintext using the key and the stated alphabet.\n", "def encrypt(plaintext, key, alphabet):\n", " keyIndices = [alphabet.index(k.upper()) for k in plaintext]\n", " return ''.join(key[keyIndex] for keyIndex in keyIndices)\n", "\n", "# the commands below define a function that decrypts the ciphertext using the key and the stated alphabet.\n", "def decrypt(cipher, key, alphabet):\n", " keyIndices = [key.index(k) for k in cipher]\n", " return ''.join(alphabet[keyIndex] for keyIndex in keyIndices)\n", "\n", "# this command creates the ciphertext by encrypting the plaintext. \n", "#It uses the encryption function, \"encrypt\", that is defined above.\n", "cipher = encrypt(plaintext, key, alphabet)\n", "\n", "print(plaintext)\n", "print(cipher)\n", "print(decrypt(cipher, key, alphabet))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "_________________________________________________\n", "\n", "##### Experiment 1.2\n", "\n", "The following text is encoded with a Ceaser cipher. Write down the formula for the cipher, decode the text, and state the name of the author.\n", "\n", "R SIVVQV ILWWCVU KYV EVRK YVUXVJ FW GIZMVK UIZMV, NYZTY CRP JZCVEK REU KZUP LEUVI KYV ZEBP JBP, KYV MVIP CRJK GCRTV PFL NFLCU VOGVTK RJKFEZJYZEX KYZEXJ KF YRGGVE. YRIIP GFKKVI IFCCVU FMVI ZEJZUV YZJ SCREBVKJ NZKYFLK NRBZEX LG. FEV JDRCC YREU TCFJVU FE KYV CVKKVI SVJZUV YZD REU YV JCVGK FE, EFK BEFNZEX YV NRJ JGVTZRC, EFK BEFNZEX YV NRJ WRDFLJ, EFK BEFNZEX YV NFLCU SV NFBVE ZE R WVN YFLIJ' KZDV SP DIJ ULIJCVP'J JTIVRD RJ JYV FGVEVU KYV WIFEK UFFI KF GLK FLK KYV DZCB SFKKCVJ.\n", "\n", "\n", "But WAIT!! I see a symbol in this ciphercode that is not in our alphabet!! What should we do about this? If we do nothing, our code will not run.\n", "_________________________________________________________________________________________________________________" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "R SIVVQV ILWWCVU KYV EVRK YVUXVJ FW GIZMVK UIZMV, NYZTY CRP JZCVEK REU KZUP LEUVI KYV ZEBP JBP, KYV MVIP CRJK GCRTV PFL NFLCU VOGVTK RJKFEZJYZEX KYZEXJ KF YRGGVE. YRIIP GFKKVI IFCCVU FMVI ZEJZUV YZJ SCREBVKJ NZKYFLK NRBZEX LG. FEV JDRCC YREU TCFJVU FE KYV CVKKVI SVJZUV YZD REU YV JCVGK FE, EFK BEFNZEX YV NRJ JGVTZRC, EFK BEFNZEX YV NRJ WRDFLJ, EFK BEFNZEX YV NFLCU SV NFBVE ZE R WVN YFLIJ' KZDV SP DIJ ULIJCVP'J JTIVRD RJ JYV FGVEVU KYV WIFEK UFFI KF GLK FLK KYV DZCB SFKKCVJ.\n", "A BREEZE RUFFLED THE NEAT HEDGES OF PRIVET DRIVE, WHICH LAY SILENT AND TIDY UNDER THE INKY SKY, THE VERY LAST PLACE YOU WOULD EXPECT ASTONISHING THINGS TO HAPPEN. HARRY POTTER ROLLED OVER INSIDE HIS BLANKETS WITHOUT WAKING UP. ONE SMALL HAND CLOSED ON THE LETTER BESIDE HIM AND HE SLEPT ON, NOT KNOWING HE WAS SPECIAL, NOT KNOWING HE WAS FAMOUS, NOT KNOWING HE WOULD BE WOKEN IN A FEW HOURS' TIME BY MRS DURSLEY'S SCREAM AS SHE OPENED THE FRONT DOOR TO PUT OUT THE MILK BOTTLES.\n", "R SIVVQV ILWWCVU KYV EVRK YVUXVJ FW GIZMVK UIZMV, NYZTY CRP JZCVEK REU KZUP LEUVI KYV ZEBP JBP, KYV MVIP CRJK GCRTV PFL NFLCU VOGVTK RJKFEZJYZEX KYZEXJ KF YRGGVE. YRIIP GFKKVI IFCCVU FMVI ZEJZUV YZJ SCREBVKJ NZKYFLK NRBZEX LG. FEV JDRCC YREU TCFJVU FE KYV CVKKVI SVJZUV YZD REU YV JCVGK FE, EFK BEFNZEX YV NRJ JGVTZRC, EFK BEFNZEX YV NRJ WRDFLJ, EFK BEFNZEX YV NFLCU SV NFBVE ZE R WVN YFLIJ' KZDV SP DIJ ULIJCVP'J JTIVRD RJ JYV FGVEVU KYV WIFEK UFFI KF GLK FLK KYV DZCB SFKKCVJ.\n" ] } ], "source": [ "# The alphabet is the list of symobls that you want to be able to translate using your cipher.\n", "# This list of letters is called a string. Whenever you enter a string into python it must have quotes around it\n", "key = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ.'!(), \" # Note the space at the end, which I kept missing.\n", "\n", "# The key is used to encode plaintext to ciphertext. It is also used to decode ciphertext to plain text \n", "# The key MUST contain all the alphabet entries once!\n", "# This key encodes a Ceasar cipher that shifts the letters by +10 positions. \n", "# A, the letter in position 1, gets shifted to K the letter in position 11 = 1+10, and so on.\n", "alphabet = \"RSTUVWXYZABCDEFGHIJKLMNOPQ.'!(), \"\n", "\n", "plaintext = \"R SIVVQV ILWWCVU KYV EVRK YVUXVJ FW GIZMVK UIZMV, NYZTY CRP JZCVEK REU KZUP LEUVI KYV ZEBP JBP, KYV MVIP CRJK GCRTV PFL NFLCU VOGVTK RJKFEZJYZEX KYZEXJ KF YRGGVE. YRIIP GFKKVI IFCCVU FMVI ZEJZUV YZJ SCREBVKJ NZKYFLK NRBZEX LG. FEV JDRCC YREU TCFJVU FE KYV CVKKVI SVJZUV YZD REU YV JCVGK FE, EFK BEFNZEX YV NRJ JGVTZRC, EFK BEFNZEX YV NRJ WRDFLJ, EFK BEFNZEX YV NFLCU SV NFBVE ZE R WVN YFLIJ' KZDV SP DIJ ULIJCVP'J JTIVRD RJ JYV FGVEVU KYV WIFEK UFFI KF GLK FLK KYV DZCB SFKKCVJ.\"\n", "\n", "# the commands below define a function that encrypts the plaintext using the key and the stated alphabet.\n", "def encrypt(plaintext, key, alphabet):\n", " keyIndices = [alphabet.index(k.upper()) for k in plaintext]\n", " return ''.join(key[keyIndex] for keyIndex in keyIndices)\n", "\n", "# the commands below define a function that decrypts the ciphertext using the key and the stated alphabet.\n", "def decrypt(cipher, key, alphabet):\n", " keyIndices = [key.index(k) for k in cipher]\n", " return ''.join(alphabet[keyIndex] for keyIndex in keyIndices)\n", "\n", "# this command creates the ciphertext by encrypting the plaintext. \n", "#It uses the encryption function, \"encrypt\", that is defined above.\n", "cipher = encrypt(plaintext, key, alphabet)\n", "\n", "print(plaintext)\n", "print(cipher)\n", "print(decrypt(cipher, key, alphabet))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "_______________________________\n", "\n", "##### Experiment 1.3\n", "\n", "The following text is encoded with a Ceaser cipher. Write down the formula for the cipher, decode the text, and state the name of the author.\n", "\n", "S RYZO S CRKVV LO KLVO DY MYXPSNO OFOBIDRSXQ DY IYE, KC S RKFO XOFOB LOPYBO LOOX KLVO DY MYXPSNO SX KXIYXO, KXN S RYZO IYE GSVV LO K QBOKD CYEBMO YP MYWPYBD KXN CEZZYBD.\n", "\n", "_________________________________________________________________________________________" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.5" } }, "nbformat": 4, "nbformat_minor": 2 }