Ceasar cipher encryption method. Using ord() and chr () to decrypt a string. If a value with the offset is < 32 or > 126 on the ASCII table it will loop back around to the appropriate value.
However, it is not working 100%, skips over symbols from the ASCII table for some reason, and isn't getting every value correct?
input - yohvldvk#wioH#$
offset - 5
expected output - tjcqg_qf}rdjC}~
user_decrypt_message = input("Please enter string to decrypt: ")
decrypted_message = ""
user_decrypt_offset = 0
while not user_decrypt_offset in range (1, 95):
user_decrypt_offset = int(input("Please enter offset value (1 to 94): "))
for letter in user_decrypt_message:
if ord(letter) + user_decrypt_offset < 32:
decrypted_message += chr(ord(letter) - user_decrypt_offset + 95)
elif ord(letter) + user_decrypt_offset > 126:
decrypted_message += chr(ord(letter) + user_decrypt_offset - 95)
else:
decrypted_message += chr(ord(letter) - user_decrypt_offset)
print()
print("Decrypted string: ")
print(decrypted_message)
I just rewrote your code and added a few print statements. If you run this code you will see that when the loop inside
decrypt()function come to '#'(whose ASCII value is 35) it will jump to 3. if statement and it will append a character with ASCII value 30(35 - 5) which is ␞(record separator delimiter). I guess there is a problem with it because it won't display.