Question

In: Computer Science

you may not use the Python ord() or chr() functions you may not use the Python...

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:

  1. c_encrypt()
  2. c_decrypt()
  3. vig_encrypt()
  4. vig_decrypt()

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:

  • the first letter of our message with a Caesar cipher with a key/rotation of 0, because A is the first letter of the alphabet,
  • the second letter with a key/rotation of 1 (for the 'B'),
  • the third letter with a rotation of 0 (for the second 'A' of ABACUS),
  • the fourth letter with a key/rotation of 2 for the 'C',
  • the fifth letter with a key/rotation of 20 for the 'U',
  • the sixth letter with a key/rotation of 18 for the 'S',
  • and wrap back to the start of our keyword ABACUS and encrypt the seventh letter with a key/rotation of 0 for the first 'A' in ABACUS

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):

  • 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).

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

Solutions

Expert Solution

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"))


Related Solutions

Use Python for this quetions: Write a python functions that use Dictionary to: 1) function name...
Use Python for this quetions: Write a python functions that use Dictionary to: 1) function name it addToDictionary(s,r) that take a string and add it to a dictionary if the string exist increment its frequenc 2) function named freq(s,r) that take a string and a record if the string not exist in the dictinary it return 0 if it exist it should return its frequancy.
PYTHON PYTHON Recursive Functions. In this problem, you are asked to write three recursive functions. Implement...
PYTHON PYTHON Recursive Functions. In this problem, you are asked to write three recursive functions. Implement all functions in a module called problem1.py. (10 points) Write a recursive function called remove char with two parameters: a string astr and a character ch. The function returns a string in which all occurrences of ch in astr are removed. For example, remove char("object oriented", ’e’) returns the string "objct orintd". Your implementation should not contain any loops and may use only the...
Python #For question 1 and 2 you may not use ''' ''' Question 1 Print the...
Python #For question 1 and 2 you may not use ''' ''' Question 1 Print the messages below without using variables. Output must be 2 lines identical to below: What is the snake's favorite language? "I love Python", said the snake. Question 2 Write a python statements to match the 5 lines of output below. Use a single print statement to produce the list of 4 languages The 3 most popular programming languages of 2020 are: 1.         Python 2.         Javascript 3.         Java 4.         C#...
Solve using PYTHON 3 and use functions. You have been hired by a restaurant named "Burger...
Solve using PYTHON 3 and use functions. You have been hired by a restaurant named "Burger Shack" Per the client, you have the following information: the client’s name, burger’s name, time of the day, and the total bill. By the end of the day, your program will provide the following information: 1. Top three best clients (highest bills) 2. Name of the client with the second-to-last lowest bill 3. Busiest hour of the day (number of clients) Assumptions: 1. doesn't...
HIMT 345 Homework 05: Functions Overview: Examine PyCharm’s “Introduction to Python” samples for Functions. Use PyCharm...
HIMT 345 Homework 05: Functions Overview: Examine PyCharm’s “Introduction to Python” samples for Functions. Use PyCharm to work along with exercises from Chapter 4. Modify the grade assignment program of Hwk 04 to utilize a function by copying the Hwk04 project and creating the Hwk05 project. Prior Task Completion: 1. Read Chapter 04 2. View Chapter 04’s video notes. 3. As you view the video, work along with each code sample in PyCharm. The examples used in the video are...
HIMT 345 Homework 05: Functions Overview: Examine PyCharm’s “Introduction to Python” samples for Functions. Use PyCharm...
HIMT 345 Homework 05: Functions Overview: Examine PyCharm’s “Introduction to Python” samples for Functions. Use PyCharm to work along with exercises from Chapter 4. Modify the grade assignment program of Hwk 04 to utilize a function by copying the Hwk04 project and creating the Hwk05 project. Prior Task Completion: 1. Read Chapter 04 2. View Chapter 04’s video notes. 3. As you view the video, work along with each code sample in PyCharm. The examples used in the video are...
which statements are true about Python functions? a)Different functions cannot use same function name b)a function...
which statements are true about Python functions? a)Different functions cannot use same function name b)a function always returns some value c)different function cannot use the same variable names d) function must use the same parameter names as the corresponding variables in the caller what benefits does structuring a program through defining functions bring? a) there is a possibility of reducing the number of variables and/or objects that must be managed at any cost at any one point b)the program is...
You may not use the java.util.Stack class. You may not use the java.util.Queue class. Write a...
You may not use the java.util.Stack class. You may not use the java.util.Queue class. Write a method public static Object removeSecond (): It removes and returns the element just behind the front element. Precondition: The queue has at least two elements. \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ // Queue.java // demonstrates queue // to run this program: C>java QueueApp //////////////////////////////////////////////////////////////// class Queue { private int maxSize; private long[] queArray; private int front; private int rear; private int nItems; //-------------------------------------------------------------- public Queue(int s) // constructor {...
You may not use the java.util.Stack class. You may not use the java.util.Queue class. Write a...
You may not use the java.util.Stack class. You may not use the java.util.Queue class. Write a method public static void removeDownTo (long n): It pops all values off the stack down to but not including the first element it sees that is equal to the second parameter. If none are equal, leave the stack empty. \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ // stack.java // demonstrates stacks //////////////////////////////////////////////////////////////// class StackX { private int maxSize; // size of stack array private long[] stackArray; private int top; //...
Directly using mathematical expression, DO NOT USE the built-in Python functions, evaluate the binomial function for...
Directly using mathematical expression, DO NOT USE the built-in Python functions, evaluate the binomial function for n = 20, p = 3/8. Make sure you plot your results (Remember the the binomial distribution is discrete)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT