I'm trying to read through a file and encrypt the message using my function. I want to only encrypt words and leave everything else alone. The file reading through is called plain.txt and I want to encrypt it in a new file called newPlain.txt. Also I need to justify how many shifts the cipher wants.
Here is my function:
# k is how many shifts you want
def CaeserCipher(string, k):
upper = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
lower = 'abcdefgihjklmnopqrstuvwxyz'
newCipher = ''
for letter in string:
if letter in upper:
if upper.index(letter) + k > 25:
indexPosition = (upper.index(letter) + k) - 26
newCipher = newCipher + upper[indexPosition]
else:
indexPosition = upper.index(letter) + k
newCipher = newCipher + upper[indexPosition]
elif letter in lower:
if lower.index(letter) + k > 25:
indexPosition = (lower.index(letter) + k) - 26
newCipher = newCipher + lower[indexPosition]
else:
indexPosition = lower.index(letter) + k
newCipher = newCipher + lower[indexPosition]
return newCipher
this is what I have so far:
# main file
# reading file and encrypting text
f = open('plain.txt', "r+")
for line in f:
newLine = CaeserCipher(line, 3)
line.replace(line, newLine)
f.close()
Would I want to break it up into a list? But then if so, how would I be able to put it back into the same spot? If anyone has some ideas of how to go about this it would be greatly appreciated.
plain.txt: (it doesn't have to start with a alpha character, could start with a space too, or anything that isn't a letter.
Hello, My name is Ed.
How are you?
from- Ed