if not args: print "Need ciphertext as input" usage(1) key = chosen_keylength = None for opt, arg in opts: if opt == "--key": key = arg elif opt == "--keylength": chosen_keylength = int(arg) cipher = "".join(unicode(a, "utf8") for a in args) if key: print CryptoStuff.vigenere_decrypt(cipher, key) else: print "Possible key lengths (kasiski):" print "(The English language's coincidence index is around 0.065)" best_length = 0 goal = 0.065 for keylength in CryptoStuff.kasiski(cipher): idx = [CryptoStuff.index_of_coincidence(chunk) for chunk in CryptoStuff.chunk_split(cipher, keylength)] median = sorted(idx)[len(idx) / 2] if abs(goal - best_length) > abs(goal - median): best_length = keylength print "Length %6d : %.4f" % (keylength, median) if not chosen_keylength: keylength = best_length print "\nBest found keylength: %d" % best_length
u"U": u"10100", u"V": u"10101", u"W": u"10110", u"X": u"10111", u"Y": u"11000", u"Z": u"11001", u"Æ": u"11010", u"Ø": u"11011", u"Å": u"11100", u" ": u"11101", u".": u"11110", u",": u"11111", } decoding = dict((encoding[k], k) for k in encoding) keystream = CryptoStuff.lfsr_keystream((0, 1, 0, 1, 0, 1, 0, 1), (1, 1, 0, 0, 0, 1, 1, 0)) keys = "" cipher = u"NQFTRQBNCJK,ØDXDUVZØ,EAQDX" binary_cipher = "".join(encoding[s] for s in cipher) print "Coded input: %s" % binary_cipher for i in range(len(binary_cipher)): keys += str(keystream.next()) plain_coded = tuple(xor(binary_cipher, keys)) print "Keystream used: %s" % keys print "Coded output: %s" % "".join(str(p) for p in plain_coded) plain = "" for i in range(len(plain_coded) / 5):
#!/usr/bin/env python # -*- encoding: utf-8 -*- # Copyright Joakim Hovlandsvåg # Licenced by GPLv3. """ Script for handling affine cipher cryptosystems. """ import sys import CryptoStuff # TODO: handle input better, if more functionality is needed if __name__ == '__main__': if len(sys.argv) <= 3: print "Usage: affine.py a b <input-data to analyze>" sys.exit(1) a = int(sys.argv[1]) b = int(sys.argv[2]) for i in range(3, len(sys.argv)): #print "%d = %s" % (i, sys.argv[i]) print CryptoStuff.affine_decrypt(unicode(sys.argv[i], 'utf-8'), a, b, 29),