Question

Using Python; Caesar part of the homework: Write c_encrypt() and c_decrypt(), both of which take two...

Using Python;

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.

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. I

mportant: If the plaintext to be encrypted has non-alphabetic characters (e.g., spaces or punctuation):

  • Leave the non-alphabetic character unchanged just as you did for Caesar Cipher.
  • Do not advance in use of the key for non-alphabetic characters in the plaintext. So for vig_encrypt('Hi Mom!', 'LEMON'), the H is encrypted according to the L from the key, the i (after conversion to I) is encrypted according to the E in the key, and the M in the plaintext is encrypted according to the M in the key (rather than according to the O in the key).

You may not use the Python ord() or chr() functions

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!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Using these two lists we can encrypt without having to use ascii values of the characters:

lower_letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
upper_letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']

Now, for ceaser encryption:

def c_encrypt(text,s):
lower_letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
upper_letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
  
result = ""
  
# Going through text one character at a time
for i in range(len(text)):
char = text[i]
  
# Encrypt uppercase characters
if (char.isupper()):
result += upper_letters[(s + upper_letters.index(char)) % 26]
  
# Encrypt lowercase characters
elif(char.islower()):
result += upper_letters[(s + lower_letters.index(char)) % 26]
  
# Does not encrypt if number or special character
else:
result += char
  
return result

Now, for ceaser decryption:

def c_decrypt(text,s):
lower_letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
upper_letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
  
result = ""
  
# Going through text one character at a time
for i in range(len(text)):
char = text[i]
  
# Decrypt uppercase characters
if (char.isupper()):
result += upper_letters[(upper_letters.index(char) - s) % 26]
  
# Decrypt lowercase characters
elif(char.islower()):
result += upper_letters[(lower_letters.index(char) - s) % 26]
  
# Does not decrypt if number or special character
else:
result += char
  
return result

For Vigenère encryption:

def vig_encrypt(text,key):
lower_letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
upper_letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
  
result = ""

#Making key as long as plaintext
key = list(key)
if len(text) == len(key):
return(key)
else:
for i in range(len(text) - len(key)):
key.append(key[i % len(key)])


#Key is assumed to be all upper case letters
#Keeping a seperate counter for key so that it only moves for alphabets
key_counter =0;


# Going through text one character at a time
for i in range(len(text)):
char = text[i]
  
# Encrypt uppercase characters
if (char.isupper()):
  
result += upper_letters[(upper_letters.index(char) + upper_letters.index(key[key_counter])) % 26]
key_counter+=1
  
# Encrypt lowercase characters
elif(char.islower()):
  
result += upper_letters[(lower_letters.index(char) + upper_letters.index(key[key_counter])) % 26]
key_counter+=1
  
# Does not encrypt if number or special character
else:
result += char
  
return result

Plain text Input: Hi Mom!

Key Input: LEMON

Output: SM YCZ!

For Vigenère Decryption:

def vig_decrypt(text,key):
lower_letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
upper_letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
  
result = ""

#Making key as long as text
key = list(key)
if len(text) == len(key):
return(key)
else:
for i in range(len(text) - len(key)):
key.append(key[i % len(key)])


#Key is assumed to be all upper case letters
#Keeping a seperate counter for key so that it only moves for alphabets
key_counter =0;


# Going through text one character at a time
for i in range(len(text)):
char = text[i]
  
# Decrypt uppercase characters
if (char.isupper()):
  
result += upper_letters[(upper_letters.index(char) - upper_letters.index(key[key_counter])) % 26]
key_counter+=1
  
# Decrypt lowercase characters
elif(char.islower()):
  
result += upper_letters[(lower_letters.index(char) - upper_letters.index(key[key_counter])) % 26]
key_counter+=1
  
# Does not decrypt if number or special character
else:
result += char
  
return result

ciphered text Input: SM YCZ!

Key Input: LEMON

Output: HI MOM!

Add a comment
Know the answer?
Add Answer to:
Using Python; Caesar part of the homework: Write c_encrypt() and c_decrypt(), both of which take two...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • WE ARE USING PYTHON TO COMPLETE THIS ASSIGNMENT :) THANK YOU! In this programming assignment, you...

    WE ARE USING PYTHON TO COMPLETE THIS ASSIGNMENT :) THANK YOU! In this programming assignment, you will write functions to encrypt and decrypt messages using simple substitution ciphers. Your solution MUST include: a function called encode that takes two parameters: key, a 26-character long string that identifies the ciphertext mapping for each letter of the alphabet, in order; plaintext, a string of unspecified length that represents the message to be encoded. encode will return a string representing the ciphertext. a...

  • C Program In this assignment you'll write a program that encrypts the alphabetic letters in a...

    C Program In this assignment you'll write a program that encrypts the alphabetic letters in a file using the Vigenère cipher. Your program will take two command line parameters containing the names of the file storing the encryption key and the file to be encrypted. The program must generate output to the console (terminal) screen as specified below. Command Line Parameters Your program must compile and run from the command line. The program executable must be named “vigenere” (all lower...

  • Computer Science C++ Help, here's the question that needs to be answered (TASK D): Task D....

    Computer Science C++ Help, here's the question that needs to be answered (TASK D): Task D. Decryption Implement two decryption functions corresponding to the above ciphers. When decrypting ciphertext, ensure that the produced decrypted string is equal to the original plaintext: decryptCaesar(ciphertext, rshift) == plaintext decryptVigenere(ciphertext, keyword) == plaintext Write a program decryption.cpp that uses the above functions to demonstrate encryption and decryption for both ciphers. It should first ask the user to input plaintext, then ask for a right...

  • Please write functions to decrypt both the shift (Caesar) and linear ciphers. Also, answer the questions...

    Please write functions to decrypt both the shift (Caesar) and linear ciphers. Also, answer the questions in Python and show how it typed and ran inside the client. The def is what to start with 1) Write a function called decrypt that accepts three numbers (c, m, and k) and returns the corresponding plaintext (p) value as a number. You can assume the modulus (n) is 256. You will need to compute the multiplicative inverse of m mod 256 to...

  • This is Crypto Manager blank public class CryptoManager { private static final char LOWER_BOUND = '...

    This is Crypto Manager blank public class CryptoManager { private static final char LOWER_BOUND = ' '; private static final char UPPER_BOUND = '_'; private static final int RANGE = UPPER_BOUND - LOWER_BOUND + 1; /** * This method determines if a string is within the allowable bounds of ASCII codes * according to the LOWER_BOUND and UPPER_BOUND characters * @param plainText a string to be encrypted, if it is within the allowable bounds * @return true if all characters...

  • Using the website Repl.it Write a Java program that can perform the Caesar cipher for English...

    Using the website Repl.it Write a Java program that can perform the Caesar cipher for English messages that include both upper and lowercase alphabetic characters. The Caesar cipher replaces each plaintext letter with a different one, by a fixed number of places down the alphabet. The program should be able to shift a specific number of places based on the user's input number. For example Plaintext: THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG. Ciphertext: QEBNRFZH YOLTK CLU GRJMP...

  • Write a Python program which implements the following two classical cryptosystem which we covered n class:...

    Write a Python program which implements the following two classical cryptosystem which we covered n class: a) Affine Cipher b) Vigenere Cipher Your program should consist of at least five functions: a) Two functions named encrypt, one for each of the two algorithms which accepts a lowercase alphabetical plaintext string and key as input and outputs a corresponding cipher text string. b) Two functions named decrypt, one for each of the two algorithms which accepts a lowercase alphabetical ciphertext string...

  • LANGUAGE: PYTHON Write a function called: d_polybius(). The function applies the decryption scheme for the polybius...

    LANGUAGE: PYTHON Write a function called: d_polybius(). The function applies the decryption scheme for the polybius cipher scheme above. The start of the function call the get_polybius_square function to get the square as a string. The second scenario when the number of characters is not even, excluding ‘\n’. For instance: “71\n5” is an invalid cipher because there is no way that the last number correspond to a character (we need two numbers). A customized version of Polybius square will be...

  • using the website repl.it (must be done in Javascript) PGM #1 Write a Java program that can perform the Caesar cipher fo...

    using the website repl.it (must be done in Javascript) PGM #1 Write a Java program that can perform the Caesar cipher for English messages that include both upper and lowercase alphabetic characters. The Caesar cipher replaces each plaintext letter with a different one, by a fixed number of places down the alphabet. The cipher illustrated here uses a left shift of three, so that (for example) each occurrence of E in the plaintext becomes B in the ciphertext. For example...

  • Part 3: Transposition Ciphers #can't use ord or chr functions You must implement three transposition ciphers...

    Part 3: Transposition Ciphers #can't use ord or chr functions You must implement three transposition ciphers (the "backwards" cipher, the Rail Fence cipher, and the Column Transposition cipher) where the ciphertext is created via an altered presentation of the plaintext. The algorithm for each is detailed in the function descriptions in this section. (13 points) def backwards_cipher(plaintext, key): • Parameter(s): plaintext ----- a string; the message to be encrypted key ----- an integer; the number to control this cipher •...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT