示例#1
0
from ngramscore import NGramScore 


alphabet = alphabets.GERMAN

fitness = NGramScore('freq/de-2.txt')
cipher = Caesar()

def crack(ctext):
    max_score = 0 
    key = 0 
    for i in range(0, len(alphabet)):
        dec = cipher.decrypt(ctext, i, alphabet)
        score = fitness.score(dec)
        print("key = " + str(i) + ", decrypted text: " + str(dec.upper()) + ", score: " + str(score))
        if score > max_score:
            max_score = score
            key = i
    return key 

# example ciphertext
enc = 'äbööemökglbgäßnm'
print("encrypted text: " + enc)
print()

key = crack(enc)

print()
print('best key = ' + str(key) + ':')
print(cipher.decrypt(enc, key, alphabet).upper())
示例#2
0
#!/usr/bin/python
# -*- encoding: utf-8 -*-

from secretpy import Caesar
from secretpy import alphabets

alphabet = alphabets.GERMAN
plaintext = u"thequickbrownfoxjumpsoverthelazydog"
key = 3
cipher = Caesar()

print(plaintext)
enc = cipher.encrypt(plaintext, key, alphabet)
print(enc)
dec = cipher.decrypt(enc, key, alphabet)
print(dec)

print('=====================================')

print(plaintext)
# use default english alphabet
enc = cipher.encrypt(plaintext, key)
print(enc)
dec = cipher.decrypt(enc, key)
print(dec)
'''
Output:

thequickbrownfoxjumpsoverthelazydog
wkhtxlfneurzqirämxpsvryhuwkhodüögrj
thequickbrownfoxjumpsoverthelazydog