Hello guys i'm having some trouble with this script. The script is supposed to basically prompt for an input and then encrypt whatever the user has input, the user can then go back and decrypt the message by typing in the encrypted message. The encryption part of the script works fine, it's just the decryption part that's giving me problems. Here is the code and ofcourse thanks in advance :D
def encrypt(key, msg):
encryped = []
for i, c in enumerate(msg):
key_c = ord(key[i % len(key)])
msg_c = ord(c)
encryped.append(chr((msg_c + key_c) % 127))
return ''.join(encryped)
def decrypt(key, encryped):
msg2 = []
for i, c in enumerate(encryped):
key_c = ord(key[i % len(key)])
enc_c = ord(c)
msg.append(chr((enc_c - key_c) % 127))
return ''.join(msg)
welcome = str(input("Press 1 to encrypt or anything else to decrypt: "))
if welcome in ['1', '1']:
var = input("Please type in what you want to encrypt: ")
if __name__ == '__main__':
key = 'This is the key'
msg = var
encrypted = encrypt(key, msg)
decrypted = decrypt(key, encrypted)
print ('Please take down the key and encryption message to decrypt this message at a later stage')
print ('This is what you want to encrypt:' , repr(msg))
print ('This is the encryption/decryption key:', repr(key))
print ('This is the encrypted message:', repr(encrypted))
else:
var2 = input("Please enter your encrypted message that you would like to decrypt: ")
if __name__ == '__main__':
key = 'This is the key'
msg = var2
decrypted = decrypt(key, var2)
print ('This is the decrypted message:', repr(decrypted))