use the text and code : cipher.txt xun gmr jznqymxu qzhhxzupmur ytmq dnvhq vavpncd vlvhpq mq txl xh mb ytn anhncxud lmii vpphnqq cnyxx nqenamviid vbynh ytn rxipnu rixgnq ltmat gnavcn v ozgmivuy axcmurxzy evhyd bxh ymcnq ze ytn cxfncnuy qenvhtnvpnp gd exlnhbzi txiidlxxp lxcnu ltx tnienp hvmqn cmiimxuq xb pxiivhq yx bmrty qnkzvi tvhvqqcnuy vhxzup ytn axzuyhd python code : #!/usr/bin/env python3 from collections import Counter import re TOP_K = 20 N_GRAM = 3 # Generate all the n-grams for value n def ngrams(n, text): for i in range(len(text) -n + 1): # Ignore n-grams containing white space if not re.search(r'\s', text[i:i+n]): yield text[i:i+n] # Read the data from the ciphertext with open('ciphertext.txt') as f: text = f.read() # Count, sort, and print out the n-grams for N in range(N_GRAM): print("-------------------------------------") print("{}-gram (top {}):".format(N+1, TOP_K)) counts = Counter(ngrams(N+1, text)) # Count sorted_counts = counts.most_common(TOP_K) # Sort for ngram, count in sorted_counts: print("{}: {}".format(ngram, count)) # Print
use the text and code :
cipher.txt
xun gmr jznqymxu qzhhxzupmur ytmq dnvhq vavpncd vlvhpq mq txl xh mb ytn
anhncxud lmii vpphnqq cnyxx nqenamviid vbynh ytn rxipnu rixgnq ltmat gnavcn
v ozgmivuy axcmurxzy evhyd bxh ymcnq ze ytn cxfncnuy qenvhtnvpnp gd
exlnhbzi txiidlxxp lxcnu ltx tnienp hvmqn cmiimxuq xb pxiivhq yx bmrty qnkzvi
tvhvqqcnuy vhxzup ytn axzuyhd
python code :
#!/usr/bin/env python3
from collections import Counter
import re
TOP_K = 20
N_GRAM = 3
# Generate all the n-grams for value n
def ngrams(n, text):
for i in range(len(text) -n + 1):
# Ignore n-grams containing white space
if not re.search(r'\s', text[i:i+n]):
yield text[i:i+n]
# Read the data from the ciphertext
with open('ciphertext.txt') as f:
text = f.read()
# Count, sort, and print out the n-grams
for N in range(N_GRAM):
print("-------------------------------------")
print("{}-gram (top {}):".format(N+1, TOP_K))
counts = Counter(ngrams(N+1, text)) # Count
sorted_counts = counts.most_common(TOP_K) # Sort
for ngram, count in sorted_counts:
print("{}: {}".format(ngram, count)) # Print
![1. use the frequency analysis to figure out the encryption key and the original
plaintext.
using the python code provided and the cipher
Using the frequency analysis, you can find out the plaintext.
It is better to use capital letters for plaintext, so for the same letter, to know
which is plaintext and which is ciphertext. use the tr command to do this.
For example, letters m, t, and y in in.txt with letters B, P, S, respectively; the results
are saved in out.txt.
$ tr 'mty' 'BPS' <in.txt > out.txt
cipher.txt
xun gmr jznaymxu gzhhxzupmur ytmq dnvhq vavpncd vlvhpq mq txl xh mbytn
anhncxud Imii vpphngg cnyxx ngenamviid vbynh ytn rxipnu rixgnq ltmat gnavcn
v ozgmivuy axcmurxzy evhyd bxh ymcng ze ytn cxfncnuy genvhtnvpnp gd
exlnhbzi txiidlxxp Ixcnu Itx tnienp hvman cmiimxuq xb pxiivhg yx bmrty gnkzvi
tvhvqgcnuy vhxzup ytn axzuyhd
python code:
#!/usr/bin/env python3
from collections import Counter
import re
TOP_K = 20
N_GRAM = 3
# Generate all the n-grams for value n
def ngrams (n, text):
for i in range (len(text) -n + 1):
# Ignore n-grams containing white space
if not re.search(r'\s', text[i:i+n]):
yield text[i:i+n]
# Read the data from the ciphertext
with open('ciphertext.txt') as f:
text = f.read()
# Count, sort, and print out the n-grams
for N in range(N_GRAM):
print("--
--")
print("{}-gram (top {}):".format(N+1, TOP_K))
counts = Counter (ngrams(N+1, text))
sorted_counts = counts.most_common(TOP_K) # Sort
for ngram, count in sorted_counts:
print("{}: {}".format(ngram, count)) # Print
# Count](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F1a03f387-3c7c-47a4-96eb-2cc1ae2384ed%2F9247a927-26ac-4861-805c-858124f6da96%2Fajbamz_processed.png&w=3840&q=75)

Trending now
This is a popular solution!
Step by step
Solved in 3 steps









