crazy_angel
crazy_angel

Reputation: 65

Writing brute force for a caeser cipher, getting error

So im writing a caeser cipher in python, and i have a brute force option to see if it can decode a word with a random offset. For some reason i get the error:

Traceback (most recent call last):
  File "C:\Users\nameredacted\Desktop\Python\CipherV2.py", line 60, in <module>
print(getTranslatedMessage(mode, message, key))
  File "C:\Users\nameredacted\Desktop\Python\CipherV2.py", line 47, in getTranslatedMessage
ciphertext += alpha2[ (alpha1[i] + brutekey)]
KeyError: 26

my code is :

from PyDictionary import PyDictionary
import enchant
MAX_KEY_SIZE = 26
dictionary = PyDictionary()
d = enchant.Dict("en_US")
alpha1 = dict(zip("ABCDEFGHIJKLMNOPQRSTUVWXYZ",range(26)))
alpha2 = dict(zip(range(26),"ABCDEFGHIJKLMNOPQRSTUVWXYZ"))

#gets if the user wants to encrypt or decrypt
def getMode():
  while True:
      print('Do you wish to encrypt, decrypt or brute force a message')
      mode = input().lower()
      if mode in 'encrypt e decrypt d bruteforce bf'.split():
         return mode
      else:
         print('Enter either "encrypt" or "e" or "decrypt" or "d" or "bruteforce" or "bf".')

#gets offset value if needed
def getKey(mode):
 key = 0
 if mode[0]=='b':
       pass
 else: 
       while True:
           key = int(input('Enter the offset number (1-%s)' % (MAX_KEY_SIZE)))
           if (key >= 1 and key <= MAX_KEY_SIZE):
              return key

#translates the message
def getTranslatedMessage(mode, message, key):
  ciphertext=''

  if mode[0] == 'd':
        key = -key
        for c in message.upper():
              if c.isalpha():
                    ciphertext += alpha2[ (alpha1[c] + key)]
              else: ciphertext += c
  elif mode[0] =='e':
        for x in message.upper():
              if x.isalpha():
                    ciphertext += alpha2[ (alpha1[x] + key) ]
              else:
                    ciphertext += x
  else:
        while True:
              for i in message.upper():
                    for brutekey in range (26):
                          ciphertext += alpha2[ (alpha1[i] + brutekey)]
              print(ciphertext)
              if d.check(ciphertext):
                    break

  return ciphertext

mode = getMode()
message = input("please input your message")
key = getKey(mode)
print('Your translated text is:')
print(getTranslatedMessage(mode, message, key))

Thanks for reading, also id appreciate if you could comment on any improvements you could see to make.

Upvotes: 0

Views: 137

Answers (2)

crazy_angel
crazy_angel

Reputation: 65

So i consildated all of your guys' help and kinda reworked the program. It is slightly messy but it works. Code:

from PyDictionary import PyDictionary
import enchant
MAX_KEY_SIZE = 26
Go='Yes'
dictionary = PyDictionary()
d = enchant.Dict("en_US")
alpha1 = dict(zip("ABCDEFGHIJKLMNOPQRSTUVWXYZ",range(26)))
alpha2 = dict(zip(range(26),"ABCDEFGHIJKLMNOPQRSTUVWXYZ"))
#gets if the user wants to encrypt or decrypt
def getMode():
  while True:
      print('Do you wish to encrypt, decrypt or brute force a message')
      mode = input().lower()
      if mode in 'encrypt e decrypt d bruteforce bf'.split():
         return mode
      else:
         print('Enter either "encrypt" or "e" or "decrypt" or "d" or "bruteforce" or "bf".')
#gets offset value
def getKey(mode):
 key = 0
 if mode[0]=='b':
       pass
 else: 
       while True:
           key = int(input('Enter the offset number (1-%s)' % (MAX_KEY_SIZE)))
           if (key >= 1 and key <= MAX_KEY_SIZE):
              return key
#translates the message
def getTranslatedMessage(mode, message, key):
  ciphertext=''

  if mode[0] == 'd':
        key = -key
        for c in message.upper():
              if c.isalpha():
                    ciphertext += alpha2[ (alpha1[c] + key)]
              else: ciphertext += c
  elif mode[0] =='e':
        for x in message.upper():
              if x.isalpha():
                    ciphertext += alpha2[ (alpha1[x] + key)% (MAX_KEY_SIZE) ]
              else:
                    ciphertext += x
  else:
        brutekey=0
        while True:
              for i in message.upper():
                    ciphertext += alpha2[ (alpha1[i] + brutekey)% (MAX_KEY_SIZE)]
              print(ciphertext)
              if d.check(ciphertext):
                    break
              else:
                    ciphertext=''
                    brutekey=brutekey+1

  return ciphertext
while Go=='yes' or 'Yes' or 'YES' or 'YEs' or 'yeS' :
  mode = getMode()
  message = input("please input your message")
  key = getKey(mode)
  print('Your translated text is:')
  print(getTranslatedMessage(mode, message, key))
  Go=input('Would you like to use the cipher again?')
  if Go in 'yes Yes YES YEs yeS':
        pass
  else:
        print("thank you for using the program")
        break

Upvotes: 1

J. Katzer
J. Katzer

Reputation: 107

As mentioned in the comment by @TemporalWolf, alpha1[i] + brutekey will be greater than 25 at some point. Suppose the message is "XYZ" and the broutekey, which starts from 0, would be 2. Then alpha1["X"] + brutekey = 25, which is ok. But alpha2[ alpha1["Y"] + brutekey ] will raise a KeyError. To fix this you could use the modulo operator. It allows you to calculate like one does with analog clocks: 8 + 6 = 2 (mod 12). In python the modulo operator is represented by a %. Your code would be:

else:
    while True:
          for i in message.upper():
                for brutekey in range (26):
                      ciphertext += alpha2[ (alpha1[i] + brutekey) % 26]
          print(ciphertext)

Also I think you want to change the order of those two for-loops. This way you fix a letter of the message and run through each brutekey for that letter. That is just the alphabet.

else:
    for brutekey in range (26):
        for i in message.upper():
            ciphertext += alpha2[ (alpha1[i] + brutekey) % 26]
        print(ciphertext)
        if d.check(ciphertext):
            break

Upvotes: 1

Related Questions