-
Write a function named “cipher” that accepts 2 strings and returns a string. The first string (the “message string”) is the text message you’re sending and the second string (the “key string”) will be used to encode it. Write this function assuming you are converting all text to upper case and that we are only encoding the characters A through Z plus any spaces in the text.
Assume that numbers 0 to 26 are used to represent a space as well as the 26 American alphabet characters. Any spaces will be represented by the number 0, while the character “A” will be represented by the number 1 (B will be 2, C will be 3 and so on). The cipher is accomplished by adding the values of the corresponding characters from the message and key strings to determine the encoded character. If the message string is “PURDUE” and the key string is “LIME”,
1. Startwiththe“P”fromthemessageandthe“L”fromthekey
2. “P”isrepresentedbythenumber16and“L”is12.
3. 16+12=28
4. Iftheresultisgreaterthan26,(ie:28inthiscase)subtract26
5. 2represents“B”inourcipher,soa“B”wouldbethefirstencodedcharacterFollow the same procedure with each character from the message and key strings. Eventually, you will have used all the key string’s characters, so you’ll need to go back to the first character of the key string again. Your function should continue to rotate through the characters of the key string as many times as necessary until the entire message string is encrypted.
Message Key Encrypted
P LàB U IàD R MàE D EàI U LàG E IàN
The cipher() function should return the completely encrypted string so that entering cipher(“PURDUE”, “LIME”) would return the string ‘BDEIGN’.
TO DO #2: The decipher() function
=============================
Write a function named “decipher” that accepts 2 strings (the “cipher string” and the “key string”) and then returns a string. This function reverses the encoding process used in the cipher() function, so you must again assume that numbers 0 to 26 are used to represent a space and the alphabetic characters “A” through “Z”. The decoding process is accomplished by subtracting the values of the corresponding characters of the cipher message and the key.
Assume the cipher string is “BDEIGN” and the key string is “LIME”
1. Startwiththe“B”fromthecipherstringandthe“L”fromthekeystring
2. “B”isrepresentedbythenumber2and“L”is12.
3. 2-12=-10
4. Iftheresultislessthan0,(ie:-10inthiscase),add26
5. 16represents“P”inourcipher,soa“P”wouldbethefirstdecodedcharacter
Follow the same procedure with each character from the message and key strings. Eventually, you will have used all the key string’s characters, so you’ll need to go back to the first character of the key string again. Your function should continue to rotate through the characters of the key string as many times as necessary until the entire message string is encrypted.
The decipher() function should return the completely encrypted string so that entering decipher(“BDEIGN”, “LIME”) would return the string ‘PURDUE’.
SAMPLE CIPHER/DECIPHER OUTPUT ================================
>>> cipher(“PYTHON”, “BOY”) ‘RNSJDM’
>>> cipher(“TURN DOWN”, “FOR WHAT”) ‘ JJNWLPQT’
>>> decipher(“RNSJDM”, “BOY”) ‘PYTHON’
ß note: first resulting character is a space
>>> decipher(“RXIUOEQAASCP RLBSYT NKNOUHLOSX“, “LINCOLN”) ‘FOUR SCORE AND SEVEN YEARS AGO’


0 comments