Write a program in python please show a picture of the code

Write a program in python (please show a picture of the code in order to be able to see the indentations) that can decrypt a message that has been encoded using a Caesar cipher. Stated another way, you need to find the “shift” for the cipher. Once you determine the shift, you know the mapping so you can decrypt the message.

Your high level algorithm will be:

1. Input the cipher-text.

2. Find the most common character.

3. Find the shift from “E” to that most common character.

4. Use the shift to decode each character of the cipher-text and print the resulting plain-text.

5. If the plain-text isn’t readable English, try the next-most common character and continue checking successive next-most-common characters until the resulting plain-text is readable

Sample Interaction

Test Case 1

Cracking a Caesar cypher.

Input cipherText: Frzdugv glh pdqb wlphv ehiruh wkhlu ghdwkv; Wkh ydoldqw qhyhu wdvwh ri ghdwk exw rqfh. Ri doo wkh zrqghuv wkdw L bhw kdyh khdug, Lw vhhpv wr ph prvw vwudqjh wkdw phq vkrxog ihdu; Vhhlqj wkdw ghdwk, d qhfhvvdub hqg, Zloo frph zkhq lw zloo frph.

COWARDS DIE MANY TIMES BEFORE THEIR DEATHS; THE VALIANT NEVER TASTE OF DEATH BUT ONCE. OF ALL THE WONDERS THAT I YET HAVE HEARD, IT SEEMS TO ME MOST STRANGE THAT MEN SHOULD FEAR; SEEING THAT DEATH, A NECESSARY END, WILL COME WHEN IT WILL COME.

Is the plaintext readable as English? (yes/no): yes

Test Case 2

Cracking a Caesar cypher.

Input cipherText: Wlv d frpprq surri, Wkdw orzolqhvv lv brxqj dpelwlrq\'v odgghu

JYI Q SECCED FHEEV, JXQJ BEMBYDUII YI OEKDW QCRYJYED\'I BQTTUH

Is the plaintext readable as English? (yes/no): no

PEO W YKIIKJ LNKKB, PDWP HKSHEJAOO EO UKQJC WIXEPEKJ\'O HWZZAN

Is the plaintext readable as English? (yes/no): no

FUE M OAYYAZ BDAAR, FTMF XAIXUZQEE UE KAGZS MYNUFUAZ\'E XMPPQD

Is the plaintext readable as English? (yes/no): no

XMW E GSQQSR TVSSJ, XLEX PSAPMRIWW MW CSYRK EQFMXMSR\'W PEHHIV

Is the plaintext readable as English? (yes/no): no

KZJ R TFDDFE GIFFW, KYRK CFNCZEVJJ ZJ PFLEX RDSZKZFE\'J CRUUVI

Is the plaintext readable as English? (yes/no): no

ETD L NZXXZY ACZZQ, ESLE WZHWTYPDD TD JZFYR LXMTETZY\'D WLOOPC

Is the plaintext readable as English? (yes/no): no

MBL T VHFFHG IKHHY, MATM EHPEBGXLL BL RHNGZ TFUBMBHG\'L ETWWXK

Is the plaintext readable as English? (yes/no): no

LAK S UGEEGF HJGGX, LZSL DGODAFWKK AK QGMFY SETALAGF\'K DSVVWJ

Is the plaintext readable as English? (yes/no): no

UJT B DPNNPO QSPPG, UIBU MPXMJOFTT JT ZPVOH BNCJUJPO\'T MBEEFS

Is the plaintext readable as English? (yes/no): no

TIS A COMMON PROOF, THAT LOWLINESS IS YOUNG AMBITION\'S LADDER

Is the plaintext readable as English? (yes/no): yes

Solution

print \"Cracking a Caesar cypher.\"

cipher = raw_input(\"Input cipherText: \")
#cipher = \"Wlv d frpprq surri, Wkdw orzolqhvv lv brxqj dpelwlrq\'v odgghu\"

cipher = cipher.upper()

def get_char_dict(s):
dict_char = {}
for c in s:
if c.isalpha():
if c in dict_char:
dict_char[c] += 1
else:
dict_char[c] = 1

return dict_char

def find_max_key(dict_char):
max_value = -1
max_char = \'\'
  
for key in dict_char:
if dict_char[key] > max_value:
max_value = dict_char[key]
max_char = key
dict_char[max_char] = 0
return (max_char, dict_char)

def find_shift_from_E(c):
return (ord(c) - ord(\'E\'))%26

dict_char = get_char_dict(cipher)

def create_shift_dict(shift):
shift_dict = {}
for i in range(0, 26):
shift_from_A = ord(\'A\') + (i+shift)%26
shift_dict[chr(shift_from_A)] = chr(ord(\'A\') + i)
return shift_dict

response = \"no\"
while response != \"yes\":
(max_char, dict_char) = find_max_key(dict_char)
shift = find_shift_from_E(max_char)
shift_dict = create_shift_dict(shift)
result = \"\"
for c in cipher:
if c.isalpha():
result += shift_dict[c]
else:
result += c
print result
response = raw_input(\"Is the plaintext readable as English? (yes/no): \")

pastebin link of code: http://pastebin.com/UmkvXD39

Write a program in python (please show a picture of the code in order to be able to see the indentations) that can decrypt a message that has been encoded using
Write a program in python (please show a picture of the code in order to be able to see the indentations) that can decrypt a message that has been encoded using
Write a program in python (please show a picture of the code in order to be able to see the indentations) that can decrypt a message that has been encoded using

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site