CS 177 Lab #3 | Complete Solution
- ExpertT
- Rating : 109
- Grade : A+
- Questions : 1
- Solutions : 1026
- Blog : 0
- Earned : $53187.54
CS 177 – Lab #3
Summer 2015
Due Date:
=========
Problem Description:
==================
Suppose you want to send a message to a friend, however you want to ensure that the
message remains private and no one can intercept and read it. To scramble the message in a
way that it can only be decoded by your friend is called ciphering/deciphering and is a part of
cryptography. In Lab-3, you need to write two cryptography functions:
• cipher() – this function will encode a string using a second string as a key
• decipher() – this function decodes the string using a second string as a key
TO DO #1: The cipher() function
===========================
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. Start with the “P” from the message and the “L” from the key
2. “P” is represented by the number 16 and “L” is 12.
3. 16 + 12 = 28
4. If the result is greater than 26, (ie: 28 in this case) subtract 26
5. 2 represents “B” in our cipher, so a “B” would be the first encoded character
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.
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. Start with the “B” from the cipher string and the “L” from the key string
2. “B” is represented by the number 2 and “L” is 12.
3. 2 - 12 = -10
4. If the result is less than 0, (ie: -10 in this case), add 26
5. 16 represents “P” in our cipher, so a “P” would be the first decoded character
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’ ! note: first resulting character is a space
>>> decipher(“RNSJDM”, “BOY”)
‘PYTHON’
>>>!decipher("RXIUOEQAASCP RLBSYT NKNOUHLOSX", "LINCOLN")
'FOUR SCORE AND SEVEN YEARS AGO’
LAB 3 GRADING RUBRIC Points
TODO #1: The cipher() function accepts 2 parameters 2
TODO #1: cipher() properly encodes each character based on the key 8
TODO #2: The decipher() function accepts 2 parameters 2
TODO #2: decipher() properly decodes each character based on the key 8
Total Points 20
[Solved] CS 177 Lab #3 | Complete Solution
- This solution is not purchased yet.
- Submitted On 10 Jul, 2015 08:33:10
- ExpertT
- Rating : 109
- Grade : A+
- Questions : 1
- Solutions : 1026
- Blog : 0
- Earned : $53187.54