encryption - Ceasar cipher: python -


the decrypting section doesn't work because randomly put wrong letters doesn't make enter code herethe word encrypted in first place. example if encrypted 'hello' 7 encryption 'olssv' when decrypted become 'cebbe'

i think problem on line 22 ' cipher2 += alphabet[(alphabet.index(a)-key)%len(cipher)]' i'm not 100% sure.

here code.

alphabet = 'abcdefghijklmnopqrstuvwxyz' la = len(alphabet) message = input("insert message: ") key = int(input("insert key: ")) cipher = ''  in message:     if in alphabet:         cipher += alphabet[(alphabet.index(a)+key)%la]     else:         print ("error") print(cipher)  cipher2 = ''  question = input("do wish decrypt? y/n: ")  if question.lower() == 'y':     in cipher:         if in alphabet:             print((cipher.index(a)-key))         cipher2 += alphabet[(alphabet.index(a)-key)%len(cipher)]     else:           print(cipher2) else:     print("thank you") 

comparing 2 lines:

     cipher += alphabet[(alphabet.index(a)+key)%la]     cipher2 += alphabet[(alphabet.index(a)-key)%len(cipher)] 

aside expected difference in addition/subtraction, looks you're changing you're using modulus. try sticking la both.

    cipher += alphabet[(alphabet.index(a)+key)%la]     cipher2 += alphabet[(alphabet.index(a)-key)%la] 

Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

css - Can I use the :after pseudo-element on an input field? -