-4
import enchant
message_decrypt= input("Enter the message you want to decrypt: ")
key= 0
def caesar_hack(message_decrypt,key):
    final_message=""
    d= enchant.Dict("en.US")
    f= d.check(message_decrypt)
    while f== False:
        for characters in message_decrypt:
            if ord(characters)<=90:
                if ord(characters)-key<ord("A"):
                    final_message= final_message+ chr(ord(characters)-key)
                else:
                    final_message= final_message+ chr(ord(characters)-key+26)
            else:
                if ord(characters)-key<ord("a"):
                    final_message=final_message+chr(ord(characters)-key)
                else:
                    final_message= final_message+chr(ord(characters)-key+26)
        key=key+1
        f= d.check(message_decrypt)
    else:
        print(final_message)
caesar_hack(message_decrypt, key)

Why doesn't this code work? I'm trying to do a caesar cipher hack using the brute force technique. I get an error as below Error

Can someone please help fix this code.

Kris
  • 8,680
  • 4
  • 39
  • 67
Anuj Dave
  • 3
  • 1
  • 1
    `ord(characters)-key` can go below 0, if your key was incremented many times. And `chr` take only `0-1,114,111` ranged values. – Kris Apr 20 '22 at 13:35
  • Show errors and other textual information as properly formatted text in the question, not as image or external link. – Michael Butscher Apr 20 '22 at 13:36

1 Answers1

0

There's a couple of tweaks I had to make to get your code to work, here's a working version:

import enchant
message_decrypt= input("Enter the message you want to decrypt: ")
key= 0
def caesar_hack(message_decrypt,key):
    final_message=""
    d= enchant.Dict("en.US")
    f= d.check(message_decrypt)
    while f== False:
        for characters in message_decrypt:
            if ord(characters)<=90:
                if ord(characters)-key<ord("A"):
                    final_message= final_message+ chr(ord(characters)-key+26)  # The additional 26 should be here, not below
                else:
                    final_message= final_message+ chr(ord(characters)-key)
            else:
                if ord(characters)-key<ord("a"):
                    final_message=final_message+chr(ord(characters)-key+26)  # The additional 26 should be here, not below
                else:
                    final_message= final_message+chr(ord(characters)-key)
        key=key+1
        f= d.check(final_message)  # Check should be on final_message, not message_decrypt
        if not f:
            final_message = ""  # Need to reset the final_message if not matched
    else:
        print(final_message)
caesar_hack(message_decrypt, key)

I've commented the main changes I made. One key one was checking final_message in the loop, not message_decrypt (and resetting it again for the next loop if no match).

The other was that your addition of 26 to the character ordinal if it was out of range needed to be moved. Without doing that, it was generating non-printable characters so the check was failing with an enchant error.