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
#2
I don't know who taught you that computers will figure out the decypher method by themselves :-)
Sounds like AI.
Maybe what was said involves the most common letter list in a particular language: e.g. E,N,R,I,S,T,O,D,A
Making a frequency table of the cyphered text, might get you a long way with a Cesar cypher.
Paul
Reply
#3
Decrypting the message would require either knowing the shift pattern used or use a character frequency table as mentioned above.

Encrypting the input:
def encrypt(text,s): result = "" # transverse the plain text for i in range(len(text)): char = text[i] # Encrypt uppercase characters in plain text if (char.isupper()): result += chr((ord(char) + s-65) % 26 + 65) # Encrypt lowercase characters in plain text else: result += chr((ord(char) + s - 97) % 26 + 97) return result #check the above function text = "CEASER CIPHER DEMO" s = 4 print "Plain Text : " + text print "Shift pattern : " + str(s) print "Cipher: " + encrypt(text,s)
Decrypting the message:
message = 'GIEWIVrGMTLIVrHIQS' #encrypted message LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' for key in range(len(LETTERS)): translated = '' for symbol in message: if symbol in LETTERS: num = LETTERS.find(symbol) num = num - key if num < 0: num = num + len(LETTERS) translated = translated + LETTERS[num] else: translated = translated + symbol print('Hacking key #%s: %s' % (key, translated))
(Code is not my work)
"Often stumped... But never defeated."
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Cesar Cipher ForsakenDusk 5 2,959 Apr-07-2024, 04:30 PM
Last Post: Pedroski55
Question Rsa Cipher Paragoon2 3 2,079 Nov-27-2023, 12:30 PM
Last Post: snippsat
  RSA Cipher with blocks Paragoon2 0 1,702 Nov-26-2023, 04:35 PM
Last Post: Paragoon2
  Transposition Cipher Issues beginner_geek07 0 2,202 Apr-08-2022, 07:44 PM
Last Post: beginner_geek07
  Encrypt and decrypt in python using own fixed key SriRajesh 3 13,721 Feb-20-2022, 01:18 PM
Last Post: dboxall123
  Caesar Cipher Help pbrowne 2 3,667 Jun-30-2021, 02:36 PM
Last Post: deanhystad
  Trying to encrypt and decrypt password into a file rpizw 4 6,860 Aug-12-2020, 05:15 PM
Last Post: bowlofred
  Coding caesar's cipher drewbty 3 4,731 May-16-2020, 10:05 AM
Last Post: DPaul
  cipher decryption tool nightfox82 0 2,195 Mar-25-2020, 06:36 AM
Last Post: nightfox82
  Paul Rubin p3.py lightweight encrypt/decrypt - is there a python3 version? mason28 0 2,521 Feb-19-2020, 03:38 AM
Last Post: mason28

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.