Text Encrypted using Caesar Cipher Formula

Posts 1–2 of 2 · Page 1 of 1
Text Encrypted using Caesar Cipher Formula
Just a little project I had to code for my Python class. You input the text and a distance value (for the cipher) and it encrypts the text and creates a .txt file on your desktop with the encrypted text. It also opens that .txt file and copies the encryption into another newly created .txt file.

Code:
plainMessage = input('Enter a message: ') #user inputs text
distance = int(input('Enter the distance value: ')) #user inputs distance value
encryptText = ''
for ch in plainMessage: #caesar cipher formula
    ordValue = ord(ch)
    cipherValue = ordValue + distance
    encryptText += chr(cipherValue)
file = open('encrypt.txt', 'w') #cipher text to .txt file
file.write(encryptText) 
file.close()

with open('encrypt.txt') as file: #.txt file read/copied to another .txt file
    copyText = file.read()
    file = open('copyfile.txt', 'w')
    file.write(copyText)
    file.close()
Coded this in IDLE and to anyone learning Python I recommend being OCD about syntax and how you format your code (either single quotes all throughout not mixed, indentation, proper variable naming format, etc)
Nice beginner project. The idea is good and the code is easy to follow.

One thing I would improve is the Caesar cipher logic. Right now it shifts every character by its ASCII value, so spaces and symbols will also change. A more traditional Caesar cipher usually only shifts letters and keeps numbers, spaces and punctuation unchanged.

Also, using `with open()` for both files would make the file handling cleaner because it automatically closes the files.

Good job for a first encryption project though.
Posts 1–2 of 2 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Need help?