Question

In: Computer Science

In cryptography, Caesar cipher is one of the simplest encryption techniques. The key idea of this...

In cryptography, Caesar cipher is one of the simplest encryption techniques. The key idea of this method is to replace each plaintext letter with one fixed number of places down the alphabet. Below is an example with a shift of three:
 
Plain:   ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
Cipher:  DEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABC
 
To cipher a string, ‘A’ is replaced by ‘D’, ‘B’ is substituted by ‘E’, and so on. To decode a string, ‘A’ is replaced by ‘x’, etc. 
By using python with vs code:
Write a program that takes “independence.txt” file as an input, encodes the content of this file, and outputs the encoded content to an output file (independence2.txt). 
Write another python script, which reads independence2.txt as an input, decodes the content of independence2.txt, and outputs the decoded content to another file, indepdence3.txt. 

Please include screenshots for your outputs.

independence.txt file is:

We the People of the United States, in Order to form a more perfect Union, establish Justice, insure domestic Tranquility, provide for the common defence, promote the general Welfare, and secure the Blessings of Liberty to ourselves and our Posterity, do ordain and establish this Constitution for the United States of America.

Article. I.
Section. 1.
All legislative Powers herein granted shall be vested in a Congress of the United States, which shall consist of a Senate and House of Representatives.

Section. 2.
The House of Representatives shall be composed of Members chosen every second Year by the People of the several States, and the Electors in each State shall have the Qualifications requisite for Electors of the most numerous Branch of the State Legislature.

No Person shall be a Representative who shall not have attained to the Age of twenty five Years, and been seven Years a Citizen of the United States, and who shall not, when elected, be an Inhabitant of that State in which he shall be chosen.

Representatives and direct Taxes shall be apportioned among the several States which may be included within this Union, according to their respective Numbers, which shall be determined by adding to the whole Number of free Persons, including those bound to Service for a Term of Years, and excluding Indians not taxed, three fifths of all other Persons. The actual Enumeration shall be made within three Years after the first Meeting of the Congress of the United States, and within every subsequent Term of ten Years, in such Manner as they shall by Law direct. The Number of Representatives shall not exceed one for every thirty Thousand, but each State shall have at Least one Representative; and until such enumeration shall be made, the State of New Hampshire shall be entitled to chuse three, Massachusetts eight, Rhode-Island and Providence Plantations one, Connecticut five, New-York six, New Jersey four, Pennsylvania eight, Delaware one, Maryland six, Virginia ten, North Carolina five, South Carolina five, and Georgia three.

When vacancies happen in the Representation from any State, the Executive Authority thereof shall issue Writs of Election to fill such Vacancies.

The House of Representatives shall chuse their Speaker and other Officers; and shall have the sole Power of Impeachment.

Section. 3.
The Senate of the United States shall be composed of two Senators from each State, chosen by the Legislature thereof, for six Years; and each Senator shall have one Vote.

Immediately after they shall be assembled in Consequence of the first Election, they shall be divided as equally as may be into three Classes. The Seats of the Senators of the first Class shall be vacated at the Expiration of the second Year, of the second Class at the Expiration of the fourth Year, and of the third Class at the Expiration of the sixth Year, so that one third may be chosen every second Year; and if Vacancies happen by Resignation, or otherwise, during the Recess of the Legislature of any State, the Executive thereof may make temporary Appointments until the next Meeting of the Legislature, which shall then fill such Vacancies.

No Person shall be a Senator who shall not have attained to the Age of thirty Years, and been nine Years a Citizen of the United States, and who shall not, when elected, be an Inhabitant of that State for which he shall be chosen.

The Vice President of the United States shall be President of the Senate, but shall have no Vote, unless they be equally divided.

The Senate shall chuse their other Officers, and also a President pro tempore, in the Absence of the Vice President, or when he shall exercise the Office of President of the United States.

The Senate shall have the sole Power to try all Impeachments. When sitting for that Purpose, they shall be on Oath or Affirmation. When the President of the United States is tried, the Chief Justice shall preside: And no Person shall be convicted without the Concurrence of two thirds of the Members present.

Judgment in Cases of Impeachment shall not extend further than to removal from Office, and disqualification to hold and enjoy any Office of honor, Trust or Profit under the United States: but the Party convicted shall nevertheless be liable and subject to Indictment, Trial, Judgment and Punishment, according to Law.

Solutions

Expert Solution

Here our task is to encrypt a file called independence1.txt by using ceaser cipher for position 3 and save the ciper text file to independence2.txt

After the completion of encryption ,we have to open the file independence2,txt and decrypt the file into plain text and

write it to independenc3.txt

ceaser cipher is a simple ciphering technology in which position of characters change by a fixed position

here we took it as a 3

Now lets see how to implement this in python

Please read all comments for the better understanding of the program

Encrypt script

Decrypt script

Results

I am also attaching the text form of code incase you need to copy paste


def encrypt(line): #function to decrypt line
   result = "" # initializing a string result as empty

  
   for i in range(len(line)): #iterating thrugh all characters in line
       ch = line[i]

      
       if (ch.isupper()): # Encrypting uppercase characters
           result += chr((ord(ch) + 3-65) % 26 + 65)

      
       else: # Encrypting lowercase characters
           result += chr((ord(ch) + 3 - 97) % 26 + 97)

   return result #returning result


f1 = open("C:\\Users\\Sangeeth\\Desktop\\independence1.txt", "r") #open file 1 in read mode

f2 = open("C:\\Users\\Sangeeth\\Desktop\\independence2.txt", "w") #open file 2 in write mode

for line in f1: #iterate through each line

f2.write( encrypt(line.rstrip('\n'))) #calling function encrypt
f2.write("\n") #newline
f1.close()
f2.close()
print("Done")

def decrypt(line): #function to decrypt line
   result = "" # initializing a string result as empty

   for i in range(len(line)): #iterating thrugh all characters in line
       ch = line[i]

         
       if (ch.isupper()): # decrypting uppercase characters
           result += chr((ord(ch) - 3-65) % 26 + 65) #changing char by pos

      
       else: # decrypting uppercase characters
           result += chr((ord(ch) - 3 - 97) % 26 + 97)

   return result #returning result

f1 = open("C:\\Users\\Sangeeth\\Desktop\\independence2.txt", "r") #open file 1 in read mode
f2 = open("C:\\Users\\Sangeeth\\Desktop\\independence3.txt", "w") #open file 2 in write mode

for line in f1: #iterate through each line
f2.write(decrypt(line.rstrip('\n'))) #calling function decrypt
f2.write("\n") #newline
f1.close()   
f2.close()
print("Done")


Related Solutions

In cryptography, a Caesar cipher, also known as Caesar's cipher, the shift cipher, Caesar's code or...
In cryptography, a Caesar cipher, also known as Caesar's cipher, the shift cipher, Caesar's code or Caesar shift, is one of the simplest and most widely known encryption techniques. It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. Given an arbitrary cipher text file, you need to write a C++ program to find out the value of the shift going down the...
In C++, write a program to implement the Caesar Cipher for both encryption and decryption. The...
In C++, write a program to implement the Caesar Cipher for both encryption and decryption. The program should be able to handle different keys by deciding the key at run time. Thank you :)
The mathematical expression of the encryption and decryption process of a Caesar cipher algorithm is expressed...
The mathematical expression of the encryption and decryption process of a Caesar cipher algorithm is expressed respectively as: c=Ep, k=p+k%26                                                                                                                         (1) p=Dc,k=c-k%26                                                                                                                         (2) Please do the following: Write at least two paragraphs to explain the principle of operation of the algorithm. For a full credit, your explanation must show the architectural diagram of the encryption and decryption process. Write a program to implement the Caesar algorithm Code must have two functions; encryption and decryption Test your codes with p as...
Caesar Cipher Encryption] Write a method that takes two parameters: A parameter of type str and...
Caesar Cipher Encryption] Write a method that takes two parameters: A parameter of type str and a parameter of type int. The first parameter is the plaintext message, and the second parameter is the encryption key. The method strictly does the following tasks: a. Convert the string into a list (let us refer to it as lista). An element in the generated list is the position of the corresponding letter in the parameter string in the English alphabet. Example: ‘C’...
Cryptography: Using columnar cipher, find the plaintext and the key that generated this ciphertext: ykccjosaiawiekhriogrrlrni Keep...
Cryptography: Using columnar cipher, find the plaintext and the key that generated this ciphertext: ykccjosaiawiekhriogrrlrni Keep in mind that only letter j was used for padding. (Show your detailed work)
One research topic in cryptography is to create homomorphic encryption schemes. In such schemes, it is...
One research topic in cryptography is to create homomorphic encryption schemes. In such schemes, it is possible to do operations on the ciphertext without performing decryption. Give an example of where homomorphic encryption would be useful. You do not have to give a specific cryptographic method but describe a situation where you would want to do computations on ciphertext. Be specific: what is the information, what is encrypted, and what the computation is. Related concept: given an RSA scheme with...
A Caesar cipher encrypts a message by shifting letters in the alphabet. For example, a shift...
A Caesar cipher encrypts a message by shifting letters in the alphabet. For example, a shift of 4 maps 'a' to 'e' and maps 'p' to 't' Here is a famous line from Shakespeare encrypted with a shift of 4: 'vq dg qt pqv vq dg: vjcv ku vjg swguvkqp.' (a) Write a program that takes as input a string to be encrypted and an integer encrpytion shift (such as 4 mentioned earlier), and prints the encrypted string. [Hint: zip()...
The Vigenère Cipher is an encryption algorithm that combines the use of a keyword with the...
The Vigenère Cipher is an encryption algorithm that combines the use of a keyword with the message to be encrypted. A tableau is provided that shows an encrypted character for each combination of characters in the message and the keyword. Using the Vigenère Tableau encryption scheme with a keyword of KEYWORD, encrypt the following message: IS INFORMATION SECURITY ESSENTIAL After encrypting the message, decrypt the following message, using KEYWORD as the keyword: YRJUW WWRIG JTFUW ERECE LCMKL CIWKR R For...
Assignment: Square Matrix ("Caesar Block")Cipher C++ ASSIGNMENTS *REMEMBER IT SHOULD NOT ONLY READ ONE LINE OR...
Assignment: Square Matrix ("Caesar Block")Cipher C++ ASSIGNMENTS *REMEMBER IT SHOULD NOT ONLY READ ONE LINE OR A SINGLE WORD remember, read, understand, and use the waterpump.cpp program will process an arbitrary amount of input text, however long. Arrange a stream in a two dimensional array of characters by filling up the array a line at a time, from leftmost column to rightmost column. DO NOT USE A GETLINE: this program should be able to input an entire library, not a...
Based on Rectangle transposition techniques, decrypt the following cipher text “LTHBPEEMOSRAIAESIGCVDENTUUBWEFSONES”. Then use the same key...
Based on Rectangle transposition techniques, decrypt the following cipher text “LTHBPEEMOSRAIAESIGCVDENTUUBWEFSONES”. Then use the same key to encrypt the following plain text “the automorphism group is more difficult”.           there is no given key
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT