In: Computer Science
COP2271 MATLAB HW9 Homework: Modified Vigenere Cipher Implement a decryption cipher to decode messages using a secret key. You are required to submit the solution and screenshots for this question. Key programming concepts: if statements, loops, strings Approximate lines of code: 27 (does not include comments or white space) Commands you can’t use: None... Program Inputs • Enter message to decrypt: • Enter secret key: – The user will always enter text for all prompts, no error checking needed. The secret key will always be lower case to start. Program Outputs • Updated key: XXX – Replace XXX with the adjusted secret key • Decrypted msg: YYY – Replace YYY with the deciphered message Assignment Details This assignment will give you a brief introduction into cryptography using a modified Vigenere Cipher! Cryptography allows us to encode and decode messages that are difficult to decipher without knowledge of a secret key/table/rules. Cryptography is a rich subject in its own right, and we will not have time to cover it in detail. Please check out the numerous online resources if you want more information: http://www.braingle.com/brainteasers/codes/index.php This particular cipher depends upon a secret key (a single word) selected by the user that only contains letters which is paired with a phrase. For example, given the phrase: Attack Now! the user could choose the secret key: woot The first step to encryption is to repeat the letters in secret key until it has the same amount of letters as the message, skipping any spaces or punctuation! So with woot as the key, repeat the letters w, o, o, t for each letter in Attack Now!. Note that you must also change the letters in the key to upper case if the letters in phrase are upper case. COP2271 MATLAB HW9 A t t a c k N o w ! W o o t w o O t w ! Now each letter in the secret message determines how far to shift the corresponding letter in the updated key. Essentially, take the position in the alphabet (starting from 0) of the letters in message and then shift key by that amount (like a Caesar Cipher). Also, treat upper case and lower case letters as two different alphabets. Here is a detailed breakdown: Letter in message Alphabet position Letter in key Decrypted letter A 0 W W t 19 o h t 19 o h a 0 t t c 2 w y k 10 o y N 13 O B o 14 t h w 22 w s ! ! Following this table, Attack Now! becomes Whhtyy Bhs!. To decode this message for the homework, do the reverse of this process! Sample Output The following test cases do not cover all possible scenarios (develop your own!) but should indicate if your code is on the right track. To guarantee full credit, your program’s output should exactly match the output below. Test Case 1: Enter message to decrypt: Whhtyy Bhs! Enter secret key: woot Updated key: Wootwo Otw! Decrypted msg: Attack Now! COP2271 MATLAB HW9 Test Case 2: Enter message to decrypt: Rr pathf! Enter secret key: edna Updated key: Ed naedn! Decrypted msg: No capes! Test Case 3: Enter message to decrypt: Qmh frisll kr pfbapgehleu! Enter secret key: syndrome Updated key: Syn dromes yn dromesyndro! Decrypted msg: You caught me monologuing! Test Case 4: Enter message to decrypt: Kbsm urm lhrrfdr uzwcxb! Enter secret key: rusty Updated key: Rust yru styrust yrusty! Decrypted msg: That was totally wicked! Test Case 5: Enter message to decrypt: Hbgzy’k xs ucjwc mwqn? Enter secret key: lucius Updated key: Luciu’s lu ciusl uciu? Decrypted msg: Where’s my super suit?
PYTHON code given below :-
LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
letters = 'abcdefghijklmnopqrstuvwxyz'
ciphertext = str(input("Enter message to decrypt:")) # get
ciphertext from user
key = str(input("Enter secret key:")) # get secret key from
user
Updated_key=""
result = ""
count=0
for i in ciphertext: #in each character in ciphertext do
if i.isupper()==True and i != ' ': # when character is Capital
letter in ciphertext and not space then
Updated_key +=key[count].upper()
j = (LETTERS.index(i) -(ord(key[count].upper())-65)) % 26 #
decrytion
count=count+1
result += LETTERS[j]
elif i.islower()==True and i!=' ': # when character is small letter
in ciphertext and not space then
Updated_key +=key[count].lower()
j = (letters.index(i) -(ord(key[count].lower())-97)) % 26 #
decrytion
count=count+1
result += letters[j]
if count ==len(key): # to made Updated key length and ciphertext
length same
count=0
if(i.isalpha()!=True) and i!=' ' : # if ciphertext contain any
symbols then
Updated_key += i # add symbol to Updated key
result += i # add symbol to decrypted message
if i == ' ': # if ciphertext has a space then
Updated_key += ' '
result += ' '
print ("Updated key:",Updated_key,"\nDecrypted msg :",result) #
print Updated key and decrypted message