Question

In: Computer Science

Vigenère Cipher The Vigenère Cipher was more or less completely unbreakable from its introduction sometime in...

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!

Solutions

Expert Solution

from itertools import starmap, cycle

def main():
  
    plain = 'ATTACKATDAWN'
    key = 'LEMON'

    cipher = vig_encrypt(plain, key)
    plain = vig_decrypt(cipher, key)

    print("Plain text : ",plain)
    print("Encrypted Text: ",cipher)
    print("Decrypted Text:",plain)
  
  
def vig_encrypt(pt, key):

    pt = filter(str.isalpha, pt.upper())

    def encr(c, k):
     
        return chr(((ord(k) + ord(c) - 2 * ord('A')) % 26) + ord('A'))

    return ''.join(starmap(encr, zip(pt, cycle(key))))


def vig_decrypt(pt, key):

    def decr(c, k):

        return chr(((ord(c) - ord(k) - 2 * ord('A')) % 26) + ord('A'))

    return ''.join(starmap(decr, zip(pt, cycle(key))))



if __name__ == '__main__':
    main()


Related Solutions

1 Introduction A cipher is an algorithm which encrypts a message into cipher text so that...
1 Introduction A cipher is an algorithm which encrypts a message into cipher text so that it can be safely transmitted without an eavesdropper being able to (easily) read it. For the purposes of this assignment the message and ciphertext will be stored as strings of ASCII characters. Cipher algorithms always perform two tasks: encryption and decryption. The encryption process takes a “message” and “key” as inputs and produces cipher text. The decryption process performs the reverse: it turns cipher...
A CEO wondered if her company received either more or less complaints from its workers on...
A CEO wondered if her company received either more or less complaints from its workers on Monday than any other day. She figured that if it were truly random, 20% of the complaints should have been filed on Monday. She randomly selected 50 complaints and checked the day that they were submitted. In those complaints 13 were submitted on a Monday. The CEO conducts a one-proportion hypothesis test at the 5% significance level, to test whether the true proportion of...
Introduction: Throughout the course the assignments tie important concepts to applications, sometimes mathematical but sometime veering...
Introduction: Throughout the course the assignments tie important concepts to applications, sometimes mathematical but sometime veering (not too deeply) into other fields. This week we consider a chemistry application. Methane is a chemical compound with the symbol CH4, having four hydrogen atoms and one carbon atom. The molecule is modeled in a tetrahedral shape, with the four hydrogen atoms connected to a central carbon atom (by C-H bonds, if you know a little chemistry). The goal is to determine the...
How a more or less elastic import demand curve, as well as a more or less...
How a more or less elastic import demand curve, as well as a more or less elastic export supply curve, determines who carries the main burden of a tariff – the importing consumers or the exporting suppliers?
Suppose a firm can sell its product for a price of $75 (no more, no less)....
Suppose a firm can sell its product for a price of $75 (no more, no less). Suppose that the marginal cost curve and average variable cost curves cross at $100 and a quantity of 10. Assume that fixed costs are $0. At a quantity of 10, total revenue would be: 750 At a quantity of 10, variable cost would be: ________ 0 or 1,000 Since fixed costs are $0, profit at 10 units of output would therefore be: ________ -250,...
If an asset is sold (market value) for more or less than its book value that...
If an asset is sold (market value) for more or less than its book value that used for tax purposes, and adjustment must be made to the taxable income during the year. How this difference in market value and book value affect the taxable income?
Encrypting Text with a Caesar Cipher Write a C program caesar.c which reads characters from its...
Encrypting Text with a Caesar Cipher Write a C program caesar.c which reads characters from its input and writes the characters to its output encrypted with a Caesar cipher. A Caesar cipher shifts each letter a certain number of positions in the alphabet. The number of positions to shift will be given to your program as a command line argument. Characters other than letters should not be encrypted. Your program should stop only at the end of input. Your program...
introduction about engineering negligence not less than one page
introduction about engineering negligence not less than one page
Strong acids and bases completely dissociate in water. Use the table in the introduction to classify...
Strong acids and bases completely dissociate in water. Use the table in the introduction to classify the following chemical compounds as strong acids, weak acids, strong bases, and weak bases. Part A H3PO4(aq) and KOH(aq) Express your answer as a chemical equation including phases. Part B HI(aq) and LiOH(aq) Express your answer as a chemical equation including phases. Part C HNO3(aq) and Cu(OH)2(s) Express your answer as a chemical equation including phases.
Answer the following completely: a). Relative to managers in more monopolistic industries, are managers in more...
Answer the following completely: a). Relative to managers in more monopolistic industries, are managers in more competitive industries more likely to spend their time on reducing costs or on pricing strategies? b). Describe the difference in economic profit between a competitive firm and a monopolist in both the short and long run. Which should take longer to reach the long-run equilibrium? c). Describe an important difference(s) in the way an economist and a businessperson might view a monopoly.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT