In: Computer Science
you may not use the Python ord() or chr() functions
you may not use the Python ord() or chr() functions
you may not use the Python ord() or chr() functions
You will write a total of four functions, each of which will take two inputs and return a string:
The first argument will be a string containing the plaintext (or clear text) message to be encrypted for the two encrypt functions, and a string containing a ciphertext to be decrypted. The second argument will be the key.
For this assignment, you will always leave all characters in plaintext or ciphertext that are not letters (i.e., spaces and punctuation marks) unchanged.
Plaintext message letters may be either upper or lower case, but you should output only upper case ciphertext.
Remember the string function upper() you used in lab.
Note that you are writing two functions for each cryptosystem, an encrypt and a decrypt, so one partial test of the correctness of your work is whether first encrypting and then decrypting returns the original string (except that you may have converted some lower case letters to upper case).
Caesar Cipher
Caesar part of the homework: Write c_encrypt() and c_decrypt(), both of which take two arguments, the first one a string and the second one an integer key.
Both should return a string.
This will be much easier if both functions use the cencrypt() shift function you wrote for the lab.
As with the lab, you may not use the Python ord() or chr() functions
Vigenère Cipher
The Vigenère Cipher was more or less completely unbreakable from its introduction sometime in the 1500s until well into the 1800s.
The key in Vigenère is a key word that is used over and over again to give a different key to the Caesar cipher for each letter of the encryption (and decryption), with 'A', in good Python form, representing a rotation of 0. (We Pythonistas start at 0, not 1!)
So if the key is ABACUS, then we encrypt:
Back in the 1800s people wanting to use this system would make use of a Vigenère square, also known as the tabula recta, shown in the middle of the Wikipedia entry for the Vigenère cipher, but we can use Python.
Vigenère part of the homework: Write vig_encrypt() and vig_decrypt() functions. Each takes two strings as inputs, with the first being the plaintext/ciphertext, and the second being the key. Both should be calling functions you wrote earlier to help make the work easier.
The key will be a string consisting only of letters, but the letters might be in upper, lower, or mixed case. Important: If the plaintext to be encrypted has non-alphabetic characters (e.g., spaces or punctuation):
One check on your work: vig_encrypt('ATTACKATDAWN', 'LEMON') should return the string LXFOPVEFRNHR; another is that vig_encrypt('Hi Mom!', 'LEMON') should return the string SM YCZ!
you may not use the Python ord() or chr() functions
you may not use the Python ord() or chr() functions
you may not use the Python ord() or chr() functions
Answer:
alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" def c_encrypt(plaintext, key): result = "" plaintext=plaintext.upper() # traverse text for letter in plaintext: if letter in alpha: # if the letter is actually a letter # find the corresponding ciphertext letter in the alphabet letter_index = (alpha.find(letter) + key) % len(alpha) result = result + alpha[letter_index] else: result = result + letter return result def c_decrypt(ciphertext, key): result = "" ciphertext=ciphertext.upper() for letter in ciphertext: if letter in alpha: # if the letter is actually a letter # find the corresponding ciphertext letter in the alphabet letter_index = (alpha.find(letter) - key) % len(alpha) result = result + alpha[letter_index] else: result = result + letter return result def vig_encrypt(plaintext, key): result = "" plaintext = plaintext.upper() kpos = [] # return the index of characters ex: if k='d' then kpos= 3 for x in key: kpos.append(alpha.find(x)) i = 0 for x in plaintext: if i == len(kpos): i = 0 pos = alpha.find(x) + kpos[i] # find the number or index of the character and perform the shift with the key if pos > 25: pos = pos - 26 # check you exceed the limit result += alpha[pos] # because the cipher text always capital letters i += 1 return result def vig_decrypt(ciphertext,key): ciphertext = ciphertext.upper() result = "" kpos = [] for x in key: kpos.append(alpha.find(x)) i = 0 for x in ciphertext: if(i==" " or i=="!"): break if i == len(kpos): i = 0 pos = alpha.find(x) - kpos[i] if pos < 0: pos = pos + 26 result += alpha[pos] i += 1 return result print("Encrypted Message using Caesar Cipher: " ,c_encrypt("Attackatdawn",3)) print("Decrypted Message using Caesar Cipher: " ,c_decrypt("DWWDFNDWGDZQ",3)) print("Encrypted Message using Vigenere Cipher: " ,vig_encrypt("ATTACKATDAWN","LEMON")) print("Encrypted Message using Vigenere Cipher: " ,vig_encrypt("Hi Mom!","LEMON")) print("Decrypted Message using Vigenere Cipher: " ,vig_decrypt("LXFOPVEFRNHR","LEMON"))