document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. # program to implement ceasar cipher decryption
  2. import string
  3.  
  4.  
  5. # this method takes string and shiftvalue as input and returns encrypted string as output
  6. def decrypt(original_msg, shift_value = 1):
  7.  
  8.     allchars = string.digits + string.ascii_letters + " " + string.punctuation
  9.  
  10.     decrypted_msg = ""
  11.  
  12.     for letter in original_msg:
  13.         if letter in allchars:
  14.             position = allchars.index(letter)
  15.             position -= shift_value
  16.  
  17.             # if the position becomes negative then we start from the back of the allchars
  18.             if position < 0:
  19.                 position += len(allchars)
  20.  
  21.             position %= len(allchars)
  22.             e_letter = allchars[position]
  23.             decrypted_msg += e_letter
  24.         else:
  25.             decrypted_msg += letter
  26.  
  27.     return decrypted_msg
  28.  
  29. # method that uses the decrypt method and decrypts files by reading the file path and storing the decrypted file on desktop
  30. def decrypt_file(shift_value):
  31.     file_path = input("Enter File path : ")
  32.     try:
  33.         file1 = open(file_path, \'r+\')
  34.         f2 = open("C:/Users/MOHIT/Desktop/decrypted_file.txt", \'w\')
  35.         print("File opened Successfully!!")
  36.         file_data = file1.read()
  37.         print("File read Successfully!!")
  38.         decrypted_data = decrypt(file_data, shift_value)
  39.         f2.write(decrypted_data)
  40.         print("Decrypted File stored on Desktop successfully!!")
  41.  
  42.     except Exception as e:
  43.         print("Problems with opening the file")
  44.         print(str(e))
  45.  
  46. ## main function
  47.  
  48. # encrypt("hello my name is mohit" , 3)
  49. print("Program to implement Ceasar Cipher decryption")
  50. shift_value = int(input("Enter the shift value : "))
  51. decrypt_file(shift_value)
  52. # decrypt(data)
  53.  
  54. \'\'\'
  55.  
  56. Program to implement Ceasar Cipher decryption
  57. Enter File path : F:/python_3.4/sample.txt
  58. File opened Successfully!!
  59. File read Successfully!!
  60. Decrypted File stored on Desktop successfully!!
  61.  
  62. \'\'\'
');