コード例 #1
0
ファイル: subattack.py プロジェクト: Tethik/learningcrypto
	print m
	words = m.split(alphabet.wordsplitter)
	word_counts = dict()
	for w in words:
		if not w:
			continue
		if not w in word_counts.keys():
			word_counts[w] = 0
		word_counts[w] += 1
		
	def cmp_top(a,b):
		return sort_valuation(a,b,word_counts)
	
	top10words = [(w, word_counts[w]) for w in sorted(word_counts.keys(), cmp=cmp_top)][0:30]
		
	found = dictionary.word_count(m, alphabet)
	print top10words
	print found
	print "Key:",key
	print [alphabet.chr(k) for k in key]
	swap = raw_input("Swap?")
	if not swap:
		break
	parts = swap.split(" ")
	a = alphabet.ord(parts[0])
	b = alphabet.ord(parts[1])
	print parts, a, b
	key[a], key[b] = key[b], key[a]	
	for i in xrange(len(alphabet)):
		assert(i in key)
	print "Key:",key
コード例 #2
0
ファイル: shiftattack.py プロジェクト: Tethik/learningcrypto
from .. import shift, substitution
from .. import alphabet
#~ from cipher2 import ciphertext
from test_shift_cipher import ciphertext, alphabet
import dictionary

_max = 0

for k in xrange(len(alphabet)):
	print "Attempting with key:", k
	try:
		message = shift.decrypt(k, ciphertext, alphabet)
		found = dictionary.word_count(message)
		#~ print message
		if found > _max:
			_max = found
			_maxkey = k
			
	except Exception as e:
		print "Failed:", e
	#~ s = raw_input("Continue?")
	
print " DONE! "
print
print
print "Most likely from dictionary"
message = shift.decrypt(_maxkey, ciphertext, alphabet)
print message
print "Number of words found from dictionary: ", _max
print "Key:", _maxkey