Question 2110 marks: In this question, you will write a program to encrypt and d
ID: 3731356 • Letter: Q
Question
Question 2110 marks: In this question, you will write a program to encrypt and decrypt a string using a simple substitution capher, In particular, you will by settsg up an alphabet string (note there is a space character at the end of this string) Next, input an offset and a phrase (another string) to be encrypitied. The characters in the phrase must be characters from the alphabet that you defined above. The ofset indicates how to encrypt cach character. In particular, cach character will be roplaced in the new encrypted string by a character that is "offset" mumber of characters beyond it in the alphabet If a characler in the input string is "c and the offset is 2 then the character in the encrypted string will be "e" (which occurs 2 lemens lar the alphabet). Note that you need to handle wrap-around at the end of the alpbet. For example, if a character in the input string is-z". then the encrypted chracter will be if the offset is 2, You can assume that the offset will be less than the number of characters in the alphabet The encryption problem is divided into three parts The fourth part is to reverse this peocess and therefore create a function to decrypt a string To begin, set up your main program as the following #Maan Program offset-int (tnput ("Ester de offset->> pbrase- input("Enter the phrase to encrypt: Note that there is a space character at the end of the alphabet string so that you can include spaces in your phrases. Note also tha the TAs may change the alpha string when they test your program so make your program general for different alphabets 1) You will need to be able to find where achrader ocors the alphabet, write a function called findposA.ch) that returns the position of character ch in string A Posstions start then o sn the string For example, alphabet A-s as defined above and ch is "d",then the function returns 3 2) You will also need to be able to find the letler thait is a cartain distance (number of letters) away from the position of another leter Write a function called findaltchar(A.pos1.cnt) that returns the letter that is offset cat positions after the letter that is at position posl1 in alphabet A. For example, for the alphabet above, if pos is 7 and ent is 3, then the function returms lettler k Note, handle the wrap-anound 3) Write a function called encrypt(AP.omwhere A is an alphabet string, P is a string to be encrypted and Off is the offset. The function returns a new string newP where each character of newP is representied by the character that is OIT positions after the corresponding character in PExplanation / Answer
You can find the code here - http://tpcg.io/RirmHd
In this editor, it loses indentation, that's why I have generated the link.
I am also pasting the code below.
alpha="abcdefghijklmnopqrstuvwxyz "
offset=int(input("Enter the offset:") )
phrase=input("Enter the phrase to encrypt:")
def findPos(A,ch):
length=len(A)
for i in range(0,length):
if(A[i]==ch):
return i;
def findaltchar(A,pos1,cnt):
length=len(A)
pos=pos1+cnt
if(pos>=length):
pos=pos%length
return A[pos]
def decryptfindaltchar(A,pos1,cnt):
length=len(A)
pos=pos1-cnt
if(pos<0):
pos=pos+length
return A[pos]
def encrypt(A,P,off):
ret=""
length=len(P)
for i in range(0,length):
ret+=findaltchar(A,findPos(A,P[i]),off)
return ret;
def decrypt(A,ephrase,eoff):
ret=""
length=len(ephrase)
for i in range(0,length):
ret+=decryptfindaltchar(A,findPos(A,ephrase[i]),eoff)
return ret
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.