Created
March 22, 2016 17:53
-
-
Save mgeeky/ca7daea262b968b45859 to your computer and use it in GitHub Desktop.
Characters frequency counting utility, handy when it boils down to Al-Kindi cryptanalysis.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Prints out characters occurences frequency | |
# This can be used in Al-Kindi related cryptographic | |
# analysis | |
from sys import argv | |
import operator | |
text = "" | |
occurs = {} | |
freqs = {} | |
print "Characters frequency counter v0.1" | |
for a in range(256): | |
freqs[`a`] = 0. | |
occurs[`a`] = 0 | |
if len(argv) == 1: text = raw_input("Input text to analyse:\n") | |
else: text = open(argv[1]).read() | |
for a in text: occurs[`ord(a)`] += 1 | |
for a in range(256): | |
freqs[`a`] = (float(occurs[`a`]) / len(text)) * 100 | |
print "\n[+] Input text length:", len(text), "\n" | |
f = reversed(sorted(freqs.iteritems(), key=operator.itemgetter(1))) | |
n=0 | |
for k, v in f: | |
if occurs[k] == 0: continue | |
else: n+=1 | |
c = chr(int(k)) | |
if int(k) < 0x20: c = '' | |
print "%03d (%02Xh) '%s'; occurs: %d; freq: %.3f%%" \ | |
% (int(k), int(k), c, occurs[k], v) | |
print "\n[+] In total occured %d unique characters." % n |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment