Python Forum
The code to decrypt Caeser Cipher.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
The code to decrypt Caeser Cipher.
#1
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()
Reply


Messages In This Thread
The code to decrypt Caeser Cipher. - by lazerwolf101 - May-26-2020, 11:00 AM
RE: The code to decrypt Caeser Cipher. - by DPaul - May-26-2020, 03:11 PM
RE: The code to decrypt Caeser Cipher. - by DT2000 - May-26-2020, 04:01 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Cesar Cipher ForsakenDusk 5 518 Apr-07-2024, 04:30 PM
Last Post: Pedroski55
Question Rsa Cipher Paragoon2 3 674 Nov-27-2023, 12:30 PM
Last Post: snippsat
  RSA Cipher with blocks Paragoon2 0 523 Nov-26-2023, 04:35 PM
Last Post: Paragoon2
  Encrypt and decrypt in python using own fixed key SriRajesh 3 4,953 Feb-20-2022, 01:18 PM
Last Post: dboxall123
  Caesar Cipher Help pbrowne 2 2,207 Jun-30-2021, 02:36 PM
Last Post: deanhystad
  Trying to encrypt and decrypt password into a file rpizw 4 3,317 Aug-12-2020, 05:15 PM
Last Post: bowlofred
  Paul Rubin p3.py lightweight encrypt/decrypt - is there a python3 version? mason28 0 1,635 Feb-19-2020, 03:38 AM
Last Post: mason28
  Caeser Cypher Help Ararisvalerian 5 4,990 Nov-10-2019, 04:58 PM
Last Post: ichabod801
  How to decrypt Blowfish-encrypted text encrypted with Perl's Crypt::CBC? swechsler 0 2,198 Aug-15-2019, 05:24 PM
Last Post: swechsler
  Can someone please help me convert this simple C ROT cipher code to Python code? boohoo9 5 3,471 Jun-14-2019, 03:02 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020