Question
Using Python create a code that will decrypt a mono-alphabetic substitution cipher. The code must not require a key and must have the option to
Using Python create a code that will decrypt a mono-alphabetic substitution cipher. The code must not require a key and must have the option to ignore spaces.
This is what I have so far:
import string
def decrypt_cipher(ciphertext, ignore_spaces=True):
# Create a frequency dictionary of the letters in the English alphabet
freq_dict = {'a': 8.167, 'b': 1.492, 'c': 2.782, 'd': 4.253, 'e': 12.702, 'f': 2.228,
'g': 2.015, 'h': 6.094, 'i': 6.966, 'j': 0.153, 'k': 0.772, 'l': 4.025,
'm': 2.406, 'n': 6.749, 'o': 7.507, 'p': 1.929, 'q': 0.095, 'r': 5.987,
's': 6.327, 't': 9.056, 'u': 2.758, 'v': 0.978, 'w': 2.360, 'x': 0.150,
'y': 1.974, 'z': 0.074}
# Create a dictionary to store the decryption mapping
decryption_map = {}
# Remove spaces from the ciphertext if the ignore_spaces flag is set
if ignore_spaces:
ciphertext = ciphertext.replace(" ", "")
# Count the frequency of each letter in the ciphertext
ciphertext_freq = {c: ciphertext.count(c) for c in string.ascii_lowercase}
# Sort the letters in the ciphertext by frequency
sorted_ciphertext_freq = sorted(ciphertext_freq, key=ciphertext_freq.get, reverse=True)
# Sort the letters in the English alphabet by frequency
sorted_english_freq = sorted(freq_dict, key=freq_dict.get, reverse=True)
# Create the decryption mapping
for i in range(len(sorted_ciphertext_freq)):
decryption_map[sorted_ciphertext_freq[i]] = sorted_english_freq[i]
# Decrypt the ciphertext using the decryption mapping
decrypted_text = ""
for c in ciphertext:
if c in decryption_map:
decrypted_text += decryption_map[c]
else:
decrypted_text += c
return decrypted_text
I need it to ask for user input of the encrypted text then output the decrypted plain text.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started