Shoaib Shoaib - 7 months ago 19
Python Question

How can i make sure that all punctuation isn't encrypted?

while 1 == 1 :

print("")

option = input("Do you want to encrypt or decrypt? Write 'E' for encrypt or 'D' for decrypt! ").upper()


if option == "E":
file = open("message.txt", "r")
plaintext = file.read()
key = input("Key: ").upper()

encrypt = ''

for n in range(0, len(plaintext)):
if plaintext[n] == ' ':
new = ord(plaintext[n])
else:
new = ord(plaintext[n]) + ord(key[n%len(key)]) - 65
if new > 90:
new -= 26
encrypt += chr(new)

ET = (encrypt)

plaintext = ET
key = input("Key: ").upper()


encrypt = ''

for n in range(0, len(plaintext)):
if plaintext[n] == ' ':
new = ord(plaintext[n])
else:
new = ord(plaintext[n]) + ord(key[n%len(key)]) - 65
if new > 90:
new -= 26
encrypt += chr(new)

ET = (encrypt)
print(ET)
file = open("encrypt.txt", "w")
file.write(ET)
file.close()


elif option == "D":
file = open("encrypt.txt", 'r')
plaintext = file.read()
key = input("Key: ").upper()

decrypt = ''

for n in range(0, len(plaintext)):
if plaintext[n] == ' ':
new = ord(plaintext[n])
else:
new = ord(plaintext[n]) - ord(key[n%len(key)]) + 65
if new < 65:
new += 26
decrypt += chr(new)

plaintext = (decrypt)

key = input("Key: ").upper()

decrypt = ''

for n in range(0, len(plaintext)):
if plaintext[n] == ' ':
new = ord(plaintext[n])
else:
new = ord(plaintext[n]) - ord(key[n%len(key)]) + 65
if new < 65:
new += 26
decrypt += chr(new)

DT = decrypt
print (decrypt)
file = open("message.txt", "w")
file.write(DT)
file.close()

else:
print(option)


So i want to know how i can make sure that all punctuation isn't encrypted and only letters are encrypted? I know that this isn't a good way to encrypt but it is for a school project so it would be great if someone can help me. Also when i decrypt it doesn't decrypt properly and sometimes forgets full stops and stuff how can i fix this? Thanks.

Answer

Define all punctuation;

punctuation = " ',.;:.!?\r\n"

Replace all instances of

if plaintext[n] == ' ':

by

if plaintext[n] in punctuation:

Addition

While your code is functional, it doesn't use a lot of the powerful tools that Python puts at your disposal. Let me illustrate. The encryption/decryption (with punctuation stripped from the text) could be done like this, using list comprehensions;

In [42]: plaintext = 'THISISAPLAINTEXT' # Your algorithm only works for capitals.

In [43]: key = 'SPAMEGGS'

In [44]: count = int(len(plaintext)/len(key))+1

In [45]: stretchedkey = [ord(c) for c in key*count]

In [46]: # Encryption

In [47]: plainnum = [ord(c) for c in plaintext]

In [48]: ciphernum = [a+b-65 for a, b in zip(plainnum, stretchedkey)]

In [49]: ciphertext = ''.join([chr(c) if c <= 90 else chr(c-26) for c in ciphernum])

In [50]: ciphertext
Out[50]: 'LWIEMYGHDPIZXKDL'

In [51]: # Decryption

In [52]: ciphernum = [ord(c) for c in ciphertext]

In [53]: decryptnum = [a-b+65 for a, b in zip(ciphernum, stretchedkey)]

In [54]: decrypt = ''.join([chr(c) if c >= 65 else chr(c+26) for c in decryptnum])

In [55]: decrypt
Out[55]: 'THISISAPLAINTEXT'
Comments