Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # program to implement ceasar cipher encryption
- import string
- # this method takes string and shiftvalue as input and returns encrypted string as output
- def encrypt(original_msg, shift_value = 1):
- allchars = string.digits + string.ascii_letters + " " + string.punctuation
- encrypted_msg = ""
- for letter in original_msg:
- if letter in allchars:
- position = allchars.index(letter)
- position += shift_value
- position %= len(allchars)
- e_letter = allchars[position]
- encrypted_msg += e_letter
- else:
- encrypted_msg += letter
- return encrypted_msg
- # method that uses the encrypt method and encrypts files by reading the file path and storing the encrypted file on desktop
- def encrypt_file(shift_value):
- file_path = input("Enter File path : ")
- try:
- file1 = open(file_path, 'r+')
- f2 = open("C:/Users/MOHIT/Desktop/encrypted_file.txt", 'w')
- print("File opened Successfully!!")
- file_data = file1.read()
- print("File read Successfully!!")
- encrypted_data = encrypt(file_data, shift_value)
- f2.write(encrypted_data)
- print("Encrypted 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")
- shift_value = int(input("Enter the shift value : "))
- encrypt_file(shift_value)
- # encrypt(data)
- '''
- Program to implement Ceasar Cipher
- Enter the shift value : 5
- Enter File path : F:/python_3.4/sample.txt
- File opened Successfully!!
- File read Successfully!!
- Encrypted File stored on Desktop successfully!!
- '''
Add Comment
Please, Sign In to add comment