# program to implement ceasar cipher decryption
import string
# this method takes string and shiftvalue as input and returns encrypted string as output
def decrypt(original_msg, shift_value = 1):
allchars = string.digits + string.ascii_letters + " " + string.punctuation
decrypted_msg = ""
for letter in original_msg:
if letter in allchars:
position = allchars.index(letter)
position -= shift_value
# if the position becomes negative then we start from the back of the allchars
if position < 0:
position += len(allchars)
position %= len(allchars)
e_letter = allchars[position]
decrypted_msg += e_letter
else:
decrypted_msg += letter
return decrypted_msg
# method that uses the decrypt method and decrypts files by reading the file path and storing the decrypted file on desktop
def decrypt_file(shift_value):
file_path = input("Enter File path : ")
try:
file1 = open(file_path, \'r+\')
f2 = open("C:/Users/MOHIT/Desktop/decrypted_file.txt", \'w\')
print("File opened Successfully!!")
file_data = file1.read()
print("File read Successfully!!")
decrypted_data = decrypt(file_data, shift_value)
f2.write(decrypted_data)
print("Decrypted File stored on Desktop successfully!!")
except Exception as e:
print("Problems with opening the file")
print(str(e))
## main function
# encrypt("hello my name is mohit" , 3)
print("Program to implement Ceasar Cipher decryption")
shift_value = int(input("Enter the shift value : "))
decrypt_file(shift_value)
# decrypt(data)
\'\'\'
Program to implement Ceasar Cipher decryption
Enter File path : F:/python_3.4/sample.txt
File opened Successfully!!
File read Successfully!!
Decrypted File stored on Desktop successfully!!
\'\'\'