In: Computer Science
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. 2. 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. 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. |
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")