Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

All published worksheets from http://sagenb.org

Views: 168745
Image: ubuntu2004
cipher = "OHNCCIBEDDYKXWFFDYIEGHFFCDODYFIQRXDXDMHWBWFDBPGXEDWYSINXTRFIYIQTBFNDXFIWYQTFCDSXDIFDBFSDYWHBDBWYFCWBIXFIBWYDJDXTGFCDXRGBBDBBIMHIQWFTGPWYFDQQDNFFCIFUDPWDBUDPWYWFWGYIYUOTBFWPWDBOGXDIJDXISDOWYUBEHFFCGHSCQDBBDXBGHQBNIYYGFCGRDFGDMHIQEDDFCGJDYGXOWNCDQIYSDQGFCDTYDDUYGFFCDXDPGXDUDNQWYDFGKCWBFQDFCDQIFDBFFHYDGXFGFGHNCIRDYNWQFCDDIXYDBFBFHUDYFWBNIRIEQDGPQDIXYWYSFCDXHQDBIYUIBIYIOIFDHXGPDYZGTWYSCWBINCWDJDODYF"
len(cipher)
398
key = dict()
for i in range(26): key[chr(ord('A')+i)]= chr(ord('A')+i)
sage: def apply_key(cipher_text,cipher_key): ... plain_text = "" ... for ch in cipher_text: ... plain_text += cipher_key[ch] ... return plain_text
sage: def list_cmp(x,y): ... if x[1]<y[1]: ... return int(1) ... if x[1]>y[1]: ... return int(-1) ... return int(0)
sage: #Based on most common letter sage: key['D'] = 'e' sage: #Based on most common digraph sage: key['C'] = 'h' sage: key['F'] = 't' sage: #Based on most common digraph not containing eth sage: key['W'] = 'i' sage: key['Y'] = 'n' sage: #Based on most common remaining letter sage: key['I'] = 'a' sage: #Based on common letter with lots of doubles occurances sage: key['B'] = 's' sage: #To make the work "been" after has sage: key['E'] = 'b' sage: #To make ab__t into about sage: key['G'] = 'o' sage: key['H'] = 'u' sage: #Based on frequency and high digraph of Qe sage: key['X'] = 'r' sage: #Based on the Mu digraph frequency sage: key['M'] = 'q' sage: #To make _ritten into written sage: key['K'] = 'w' sage: #To make _rerequisites into prerequisites sage: key['R'] = 'p' sage: #To make _u_h into much sage: key['O'] = 'm' sage: key['N'] = 'c' sage: #To make _ental into mental sage: key['Q'] = 'l' sage: #To make cr_ptanal_st into cryptanalyst sage: key['T'] = 'y' sage: #To make _or into for sage: key['P'] = 'f' sage: #To make bein_ into being sage: key['S'] = 'g' sage: #To make e_ery into every sage: key['J'] = 'v' sage: #To make _efies_efinition into defies definition sage: key['U'] = 'd' sage: #To make en_oy into enjoy sage: key['Z'] = 'j'
sage: data = [["Letter", "Frequency", "Percentage"]] sage: for i in range(26): ... data.append([key[chr(ord('A') + i)],cipher.count(chr(ord('A') + i)),100*(0.0+cipher.count(chr(ord('A') + i)))/len(cipher) ]) ... sage: data.sort(list_cmp) sage: html.table(data)
Letter Frequency Percentage
e 63 15.8291457286432
t 45 11.3065326633166
n 32 8.04020100502512
a 31 7.78894472361809
s 29 7.28643216080402
i 27 6.78391959798995
o 24 6.03015075376884
h 21 5.27638190954774
r 21 5.27638190954774
l 18 4.52261306532663
u 14 3.51758793969849
c 11 2.76381909547739
m 8 2.01005025125628
f 8 2.01005025125628
g 8 2.01005025125628
y 8 2.01005025125628
d 8 2.01005025125628
b 6 1.50753768844221
p 6 1.50753768844221
v 4 1.00502512562814
q 3 0.753768844221106
w 2 0.502512562814070
j 1 0.251256281407035
A 0 0.000000000000000
L 0 0.000000000000000
V 0 0.000000000000000
sage: data = list(range(27)) sage: for i in range(27): ... data[i] = list(range(27)) ... sage: for i in range(26): ... data[0][1+i] = key[chr(ord('A') + i)] ... sage: for i in range(26): ... data[1+i][0] = key[chr(ord('A') + i)] ...
sage: for i in range(26): ... f = chr(ord('A') + i) ... for j in range(26): ... s = chr(ord('A') + j) ... if cipher.count(f+s) == 0: ... data[i+1][j+1] = " " ... else: ... data[i+1][j+1] = cipher.count(f+s) ... ... sage: html.table(data)
0 A s h e b t o u a v w L q c m f l p g y d V i r n j
A
s 3 3 2 7 1 5 2 1 1 1 3
h 1 9 3 3 1 4
e 10 4 4 2 1 4 2 2 2 3 3 4 1 1 2 1 8 9
b 3 1 1 1
t 1 13 6 6 4 2 4 1 1 1 1 1 3
o 1 1 5 4 1 1 3 1 1 5 1
u 1 2 2 2 2 1 1 1 1 1
a 3 2 4 1 1 2 1 4 2 1 1 3 6
v 4
w 1 1
L
q 3
c 4 1 1 2 1 1 1
m 2 1 1 1 1 2
f 1 2 1 4
l 1 6 1 1 1 2 1 1 2 2
p 2 1 1 1 1
g 2 3 1 1 1
y 2 1 2 1 1 1
d 1 4 1 1 1
V
i 5 3 4 1 1 1 1 1 10
r 1 6 1 3 1 1 1 1 1 2 1 2
n 5 5 3 4 1 1 1 4 3 3 1 1
j 1
sage: apply_key(cipher,key)
'muchhasbeenwrittenaboutthementalprerequisitesforbeingacryptanalystcertainlythegreatestgeniusesinthisartasineveryotherpossessaqualityofintellectthatdefiesdefinitionandmystifiesmoreaveragemindsbutthoughlessersoulscannothopetoequalbeethovenormichelangelotheyneednotthereforedeclinetowhistlethelatesttuneortotouchapenciltheearneststudentiscapableoflearningtherulesandasanamateurofenjoyinghisachievement'