I'm writing a python program to encrypt and decrypt a custom string. This isn't going to be used for anything serious, it's just for fun. The encrypter seems to work fine, but here it is, the encrypter. The program writes everything to a file. As seen in the code below, the program loops through ASCII characters if the key (which is defined by the user) will put it above 127.
My issue is that when decrypting, I get some strange characters. It fails when using a key over 229.
Encrypter:
temp_key = 9999
message = "Hello"
result = ""
for char in message:
ecry_char_int = ord(char) - temp_key
while ecry_char_int < 0:
temp_key -= 128
ecry_char_int = 128 - temp_key
result += chr(ecry_char_int)
print(result)
Decrypter:
result2 = ""
encoded = result
ekey = 9999
for char in encoded:
decr_char_int = ord(char) + ekey
while decr_char_int > 127:
ekey -= 128
decr_char_int = ekey
result2 += chr(decr_char_int)
print(result2)
For example, encrypting "Hello"
with the key 9999
; I get the encrypted string of "qV]]`"
. Decrypting string "qV]]`"
with key 9999
I get this:
'\x0fello'
What I'm trying to figure out is how I'm supposed to stop this from happening, since it's only the first character this happens with.
Note: I don't want/know how to install any additional modules (i.e. Cryptography) since this is/was being mostly developed on my school computer.