May-26-2020, 11:00 AM
(This post was last modified: May-26-2020, 11:01 AM by lazerwolf101.)
This is what I have done so far. I really need help on decryption. When I choose the decryption option I do not want it ask for a key from me. I want the code to figure it out by itself. I was taught that I could achieve this through finding the common characters in the encrypted text. The encryption works perfectly. But decryption is just really hard to figure out. Please help!!!
import collections print("Caesar Cipher") alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz " #alphabet2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" extra = ".,?'" selection = input("Do you want to encrypt [E] or decrypt [D] a message? ") ########################################################################################################### def encrypt(): global encrypted_message e_message = input("[E]Enter your message: ") e_key = input("[E]Enter your shift: ") e_key = int(e_key) print(e_message) print(e_key) seperate_message = [a for a in e_message] print(seperate_message) ascii_message = [ord(a) for a in e_message] print(ascii_message) shifted_message = [ord(a)+e_key for a in e_message] print(shifted_message) encrypted_message = " " # for A in e_message: #if A in alphabet2: # encrypted_message += alphabet2[(alphabet2.index(A)+e_key)%(len(alphabet2))] for a in e_message: if a in alphabet: encrypted_message += alphabet[(alphabet.index(a)+e_key)%(len(alphabet))] for a in e_message: if a in extra: encrypted_message += extra[(extra.index(a)+e_key)%(len(extra))] print("Your encrypted message is: " + encrypted_message) ########################################################################################################### def decrypt(): global decrypted_message d_message = input("[D]Enter you message: ") d_key = input("[D]Enter your shift: ") d_key = int(d_key) #print(d_message) #uprint(d_key) decrypted_message = " " # for A in d_message: #if A in alphabet2: # decrypted_message += alphabet2[(alphabet2.index(A)-d_key)%(len(alphabet2))] + "" for a in d_message: if a in alphabet: decrypted_message += alphabet[(alphabet.index(a)-d_key)%(len(alphabet))] + "" for a in d_message: if a in extra: decrypted_message += extra[(extra.index(a)-d_key)%(len(extra))] print("Your decrypted message is:" + decrypted_message) seperate_message = [a for a in decrypted_message] print(seperate_message) ascii_message = [ord(a) for a in decrypted_message] print(ascii_message) shifted_message = [ord(a)-d_key for a in decrypted_message] print(shifted_message) common_letter = collections.Counter(decrypted_message).most_common(1)[0] print(common_letter) ########################################################################################################### if selection == "E": encrypt() if selection == "D": decrypt()