Reputation: 11
I am using python to encrypt and decrypt stream ciphers. It works perfectly fine on the idle editor shell when I run it but I get messed up outputs when I try to run the code with the python terminal (opening the python file from file explorer). It works on the terminal when decrypting a small word but any additional spaces or numbers messes it up but it doesn't mess up when run through idle.
I have no idea what's wrong and have tried searching up solutions but have not found anything.
import random
import urllib.request
from operator import add
word_site = "https://www.mit.edu/~ecprice/wordlist.10000"
class colours:
RED = '\033[93m'
WHITE = '\033[0m'
while True:
print ('Type [e] to encrypt a message')
print ('Type [d] to decrypt a message')
print ('Type [q] to quit')
select = input ('Selection: ')
if select.lower() == 'q':
quit()
elif select.lower() == 'e':
open_site = urllib.request.urlopen(word_site)
words = open_site.read().splitlines()
while True:
generate_key = str(random.choices([line for line in words], k=1))
key = (generate_key[3:-2])
if len(key) > 5 and len(key) < 15:
break
else:
continue
msg = input ('Enter a message to be encrypted: ')
split_msg = [*msg]
keystream = ''.join(key[i % len(key)] for i in range(len(msg)))
print ('Your stream key is: ',key)
plain_num = ([ord(c) for c in msg])
key_num = ([ord(c) for c in keystream])
added_num = (list(map(add, plain_num, key_num)))
cipher_num = []
for i in added_num:
cipher_num.append(i % 128)
print ('Your encrypted text is below:')
encrypted_txt = "".join(map(chr,cipher_num))
print (encrypted_txt)
elif select.lower() == 'd':
cipher_txt = input ('Enter cipher text to decrypt: ')
cipher_key = input ('Enter key used to ecrypt the text: ')
keystream_txt = ''.join(cipher_key[i % len(cipher_key)] for i in range(len(cipher_txt)))
cipher_txt_num = ([ord(c) for c in cipher_txt])
cipher_key_num = ([ord(c) for c in keystream_txt])
cipher_txt_num_added = ([i + 128 for i in cipher_txt_num])
subtracted_ciphers = ([a - b for a, b in zip(cipher_txt_num_added, cipher_key_num)])
print ('Your decrypted text is below:')
print (''.join(map(chr,subtracted_ciphers)))
I was expecting the code to work on both idle and the terminal. I thought the python terminal may have been outdated but I installed them both at the same time and it does say its python 3.12.
Upvotes: 1
Views: 39