Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 32 additions & 91 deletions caesarCipher.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,11 @@
"\n",
"Here is an algorithm to follow:\n",
"\n",
"- outside of the calling function\n",
" - Ask the user to enter the plaintext\n",
" - Ask the user to enter the key\n",
"\n",
"- BEGIN caesarCipher with the **plaintext** and **key**\n",
"-BEGIN\n",
"- intialise a ciphertext string to the empty string\n",
"- Ask the user to enter the plaintext\n",
"- change plaintext into all CAPS\n",
"- Ask the user to enter the key\n",
"- For every letter in the plaintext\n",
"- if the letter is alphabetic\n",
" - convert the letter into a number (alphabet number A=0, B=1... Z=25 or ASCII number A=65 B=66...Z=90 )\n",
Expand All @@ -49,7 +47,6 @@
" - convert the number back into a character\n",
"- add the character to the cipherttext\n",
"- are there more letters in the plaintext? \n",
"- return the ciphertext to the calling program\n",
"- END\n"
]
},
Expand Down Expand Up @@ -102,20 +99,20 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"65,66,67,32,88,89,90,"
"84,72,73,83,32,73,83,32,65,32,83,84,82,73,78,71,32,65,76,76,32,73,78,32,85,80,80,69,82,67,65,83,69,"
]
}
],
"source": [
"# changing a character into ascii values:\n",
"myString = \"abc xyz\".upper()\n",
"myString = \"this is a string all in uppercase\".upper()\n",
"for i in range(len(myString)):\n",
" asciiNum = ord (myString[i])\n",
" print(asciiNum, end= \",\")"
Expand Down Expand Up @@ -173,131 +170,75 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## Caeser Cipher\n"
"## Caser Cipher\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 36,
"metadata": {},
"outputs": [],
"source": [
"#function that will create ciphertext from plaintext and a key\n",
"import string\n",
"def caesarCipher(plaintext, key):\n",
" key=int(key)\n",
" alpha = string.ascii_uppercase\n",
" ciphertext = \"\"\n",
" \n",
" message = plaintext.upper()\n",
" newMessage = \"\"\n",
" for i in range(0,len(message)):\n",
" if message[i] in alpha: # use the in operator\n",
" newMessage += message[i]\n",
" asciiNum =int(ord (newMessage[i]))+key\n",
" if asciiNum>90:\n",
" asciiNum-=26\n",
" ciphertext+=chr(asciiNum)\n",
" elif message[i] not in alpha:\n",
" newMessage += message[i]\n",
" #implement caesar cipher here\n",
" \n",
" \n",
" return ciphertext\n",
"\n",
" "
"\n",
" return ciphertext\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 37,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"your encrypted message = \n"
"your encrypted message = €tuumqo~q€ym…",
"ym…",
"\n"
]
}
],
"source": [
"#test your caeserCipher function here\n",
"secret = caesarCipher(\"this is a secret\", 9) # this should return : CQRB RB J BNLANC\n",
"secret = caesarCipher(\"this is a secret maymay \", 70) # this should return : CQRB RB J BNLANC\n",
"print(\"your encrypted message = \", secret)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 32,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"your encrypted message = \n"
"your encrypted message = CQRBRBJBNLANC\n"
]
}
],
"source": [
"secret = caesarCipher(\"this is a secret\", 22) # should return PDEO EO W OAYNAP\n",
"secret = caesarCipher(\"this is a secret\", 9) # should return PDEO EO W OAYNAP\n",
"print(\"your encrypted message = \", secret)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"please enter your message to encodehello world\n",
"please enter the key - between 1 and 25100\n",
"that was not a valid key range, please try again\n",
"please enter the key - between 1 and 25r\n",
"you did not enter a number, the key must be a number, try again\n",
"please enter the key - between 1 and 2512\n",
"that was not a valid key range, please try again\n",
"please enter the key - between 1 and 2512\n",
"that was not a valid key range, please try again\n",
"please enter the key - between 1 and 25\n",
"you did not enter a number, the key must be a number, try again\n",
"please enter the key - between 1 and 25\n",
"you did not enter a number, the key must be a number, try again\n"
]
},
{
"ename": "KeyboardInterrupt",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m/usr/local/lib/python3.5/dist-packages/ipykernel/kernelbase.py\u001b[0m in \u001b[0;36m_input_request\u001b[0;34m(self, prompt, ident, parent, password)\u001b[0m\n\u001b[1;32m 729\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 730\u001b[0;31m \u001b[0mident\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mreply\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msession\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrecv\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstdin_socket\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 731\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mException\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/usr/local/lib/python3.5/dist-packages/jupyter_client/session.py\u001b[0m in \u001b[0;36mrecv\u001b[0;34m(self, socket, mode, content, copy)\u001b[0m\n\u001b[1;32m 795\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 796\u001b[0;31m \u001b[0mmsg_list\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0msocket\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrecv_multipart\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmode\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcopy\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mcopy\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 797\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mzmq\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mZMQError\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/usr/local/lib/python3.5/dist-packages/zmq/sugar/socket.py\u001b[0m in \u001b[0;36mrecv_multipart\u001b[0;34m(self, flags, copy, track)\u001b[0m\n\u001b[1;32m 394\u001b[0m \"\"\"\n\u001b[0;32m--> 395\u001b[0;31m \u001b[0mparts\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrecv\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mflags\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcopy\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mcopy\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtrack\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mtrack\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 396\u001b[0m \u001b[0;31m# have first part already, only loop while more to receive\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32mzmq/backend/cython/socket.pyx\u001b[0m in \u001b[0;36mzmq.backend.cython.socket.Socket.recv (zmq/backend/cython/socket.c:7683)\u001b[0;34m()\u001b[0m\n",
"\u001b[0;32mzmq/backend/cython/socket.pyx\u001b[0m in \u001b[0;36mzmq.backend.cython.socket.Socket.recv (zmq/backend/cython/socket.c:7460)\u001b[0;34m()\u001b[0m\n",
"\u001b[0;32mzmq/backend/cython/socket.pyx\u001b[0m in \u001b[0;36mzmq.backend.cython.socket._recv_copy (zmq/backend/cython/socket.c:2344)\u001b[0;34m()\u001b[0m\n",
"\u001b[0;32m/usr/local/lib/python3.5/dist-packages/zmq/backend/cython/checkrc.pxd\u001b[0m in \u001b[0;36mzmq.backend.cython.checkrc._check_rc (zmq/backend/cython/socket.c:9621)\u001b[0;34m()\u001b[0m\n",
"\u001b[0;31mKeyboardInterrupt\u001b[0m: ",
"\nDuring handling of the above exception, another exception occurred:\n",
"\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-7-f8bf7913f29c>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;32mwhile\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mvalid\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 7\u001b[0;31m \u001b[0mkey\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"please enter the key - between 1 and 25\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 8\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mkey\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0;32min\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m25\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 9\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"that was not a valid key range, please try again\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/usr/local/lib/python3.5/dist-packages/ipykernel/kernelbase.py\u001b[0m in \u001b[0;36mraw_input\u001b[0;34m(self, prompt)\u001b[0m\n\u001b[1;32m 703\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_parent_ident\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 704\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_parent_header\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 705\u001b[0;31m \u001b[0mpassword\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 706\u001b[0m )\n\u001b[1;32m 707\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/usr/local/lib/python3.5/dist-packages/ipykernel/kernelbase.py\u001b[0m in \u001b[0;36m_input_request\u001b[0;34m(self, prompt, ident, parent, password)\u001b[0m\n\u001b[1;32m 733\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mKeyboardInterrupt\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 734\u001b[0m \u001b[0;31m# re-raise KeyboardInterrupt, to truncate traceback\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 735\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mKeyboardInterrupt\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 736\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 737\u001b[0m \u001b[0;32mbreak\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mKeyboardInterrupt\u001b[0m: "
]
}
],
"source": [
"# now test the function with user input (think about your test cases!!)\n",
"plaintext = input(\"please enter your message to encode\")\n",
"valid = False # assume the user does not enter a validkey - validate!!\n",
"#validate that the key is 1) a number and 2) within the valid range 1-25\n",
"while not valid:\n",
" try:\n",
" key = int(input(\"please enter the key - between 1 and 25\"))\n",
" if key not in range(1,25]:\n",
" print(\"that was not a valid key range, please try again\")\n",
" else:\n",
" valid=True\n",
" except ValueError:\n",
" print(\"you did not enter a number, the key must be a number, try again\")\n",
"\n",
"ciphertext = caesarCipher(plaintext, key)\n",
"print(\"your encoded message is:\", ciphertext)\n",
" "
]
}
],
"metadata": {
Expand Down