From 657a447733382ebd1b3f0f064907f57d470dfebe Mon Sep 17 00:00:00 2001 From: spirkelmann Date: Tue, 28 Jan 2020 18:45:05 +0100 Subject: [PATCH] code for generating tex output for friedman test --- code/friedman.py | 83 +++++++++++++++++++++++++++++++++++++++++++ code/frost_cipher.txt | 19 ++++++++++ 2 files changed, 102 insertions(+) create mode 100644 code/friedman.py create mode 100644 code/frost_cipher.txt diff --git a/code/friedman.py b/code/friedman.py new file mode 100644 index 0000000..8ce9940 --- /dev/null +++ b/code/friedman.py @@ -0,0 +1,83 @@ +f = open('frost_cipher.txt') +cipher = f.readlines() +cipher = [c.strip('\n').split(' ') for c in cipher] + +clean = '' +for c in cipher: + clean += "".join(c) +# print("".join(c)) +ciphertext = clean.upper() + + +colors = ['red', 'blue', 'darkgreen', 'orange', 'black', 'yellow'] + +width = 110 +blocknum = 4 +blocks = ["" for i in range(0,blocknum)] +output0 = "\\texttt{" +output1 = "\\texttt{" + +for i in range(len(ciphertext)): + output0 += ciphertext[i] + + for j in range(blocknum): + if i % blocknum == j: + output1 += "\\textcolor{" + colors[j] + "}{" + ciphertext[i] + "}" + + blocks[i%blocknum] += ciphertext[i] + + if (i+1)%width == 0: + output0 += '\n' + output1 += '\n' + +output0 += "}" +output1 += "}" + +f0 = open('friedman_0.tex', 'w') +f0.write(output0) +f0.close() + +f1 = open('friedman_{}.tex'.format(blocknum), 'w') +f1.write(output1) +f1.close() + +def compute_coincidence(text): + letter_count = {} + letters = [chr(i) for i in range(ord('A'), ord('Z')+1)] + + for c in letters: + letter_count[c] = 0 + + for c in text: + if c in letter_count.keys(): + letter_count[c] += 1 + + N = len(text) + s = 0 + for n_i in letter_count.values(): + s += n_i * (n_i - 1) + kappa = float(s) / (N * (N - 1)) + print("kappa_o = ", kappa) + return kappa + + +def output_block(block, color, width=20): + output = "\\texttt{\\textcolor{" + color + "}{" + for i in range(len(block)): + output += block[i] + if (i+1)%width == 0: + output += '\n' + + output += "}}" + + kappa = compute_coincidence(blocks[j]) + output += "\\textcolor{"+color+"}{$$" + "\\kappa_o = {:6.4f}".format(kappa) + "$$}" + print(output) + + f = open('friedman_{}_'.format(blocknum) + color + '.tex', 'w') + f.write(output) + f.close() + +for j in range(blocknum): + output_block(blocks[j], colors[j]) + diff --git a/code/frost_cipher.txt b/code/frost_cipher.txt new file mode 100644 index 0000000..701e7be --- /dev/null +++ b/code/frost_cipher.txt @@ -0,0 +1,19 @@ +Ync jhfug vbavfyxi zb s rjczgp bfcv +Tsu ggkwp W uhzcr fhy kfsojc pgmm +Rbv uj fbw mwrjweji zggl Z glhtu +Ofw qfccxi ucog tes sl krf sl N tcmei +Kc oajis am gvbl bs kvw nsusjzwfkla +Yysf mtfy laj fhzxw rg bnxk ok yfzf +Sgi yonbsx dwkmrdk mmv pwmyvf uefza +Txhrikx nk ksl liokld rbv pfehww bvoj +Mmfiya fj tgk yyol mmv dslxzby mmvfw +Sgi scla yyol ftibagl vemtqcm dtd +Zb dxfmsk gt jhwi mrr lkturwg gcoud +Ty W cxuk hzx kzfkm kff sgtkvwk irm +Qxy bbgpneu zhb noq ejrrk hs kc otd +Z rgngksv bk Z gzhzcr woji qgfj soud +N jvseq ss lxqcwfz yywk pnkv s lnxv +Khrvkzxwv oyxx rbv tlvg zxsts +Lpt icswx uwnxwxsv bs r kghi rbv B +N kcgd yys ggj cskl yionxqvr tr +Fer lafk vsl rrrw tqc hzx iztxxwvbux