Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Q3 Encryption [10 marks For this question, we will implement a shift cypher, as

ID: 3802097 • Letter: Q

Question

Q3 Encryption [10 marks For this question, we will implement a shift cypher, as discussed in Dr. Kapron's guest lecture, also called a Caesar cypher: https:ALen.wikipedia.org/wikiACaesar cipher For this question, you will use shiftcyper.py and mytestcypher.py In shiftcypher.py, you need to implement encryptString(mystr, shift) and decryptString(mystr, shift) which encrypt and decrypt mystr using the shift given. We have provided the functions getNumForChar and getCharForNum to convert between characters A-Z and numbers 0-25, which will allow you to more easily do the shifting. For example, getNumForCharl will return 1, and ("B") getCharForNum(2) will return "C". For this question, you may assume that the input will always be capital (uppercase) letters A-Z To test your code, you should type on mytestcypher.py" from the directory containing your code. For this question, you will need to access the individual characters of astring. Behind the scenes, a string in pyth is actually an array of characters. So, just like on when we used arrays of numbers, we can access the individual characters of a string using the same syntax. For example, if we declare the following variable test then a[1] will return "e" and len(a) will return 4. To append a character to a string, or to append two strings, you can use a For example, if we execute: a test a a another test then "print a" will print testanother test When you have successfully implemented the two functions, when you execute "python mytestcypher.py you will get the following output: CDEZAB True ABCXYZ True

Explanation / Answer

def getNumForChar(char):
#This was wrong in the given code in the question, replaced 97 with 65
return ord(char) - 65

def getCharForNum(num):
return chr(num+65)

def encryptString(mystr,shift):
newStr = ""
#for all char in mystr
for ch in mystr:
#get position in alphabet, 0 for A, 1 for B and so on
numChar = getNumForChar(ch)
#Apply the circular shift using modulus
numChar = (numChar + shift) % 26
#get new char based on rotated position
newChar = getCharForNum(numChar)
#append to the string
newStr += newChar
return newStr

def decryptString(mystr,shift):
newStr = ""
for ch in mystr:
numChar = getNumForChar(ch)
#Reverse the circular shift
numChar = (numChar - shift) % 26
#get new char based on rotated position
newChar = getCharForNum(numChar)
newStr += newChar
return newStr