Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # program to implement vigenere cipher decryption
- import string
- # method that removes symbols, letters, numbers from the message
- def remove_ichars(message):
- other_chars = string.punctuation + string.digits + " "
- n_message = "".join([i for i in message if i not in other_chars])
- return n_message
- # this method takes string and shiftvalue as input and returns encrypted string as output
- def decrypt(original_msg, key):
- index = 0
- original_msg = original_msg.upper()
- original_msg = remove_ichars(original_msg)
- key = key.upper()
- allchars = string.ascii_uppercase
- decrypted_msg = ""
- for letter in original_msg:
- if letter in allchars:
- position = (allchars.index(letter) - allchars.index(key[index]) + 26) % len(allchars)
- decrypted_msg += allchars[position]
- index += 1
- # reset index is greater than length of key
- if index >= len(key):
- index %= len(key)
- else:
- decrypted_msg += letter
- index = 0
- 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(key):
- 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, key)
- 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")
- key = input("Enter the Decryption Key : ")
- decrypt_file(key)
- # print(decrypt(data, key))
- '''
- 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!!
- '''
Add Comment
Please, Sign In to add comment