凯撒密码破译

凯撒密码破译

from my csdn blog

信息安全原理 hw1

Caesar: AWPLDPPYNCJAEJZFCYLXPHTESESPDLXPVPJLYODPYOEZESPEL

破译该密码,并做破译出的明文指定的事情

凯撒密码是位移密码,写个程序遍历出所有结果

#casesarTest.py

cipher = 'AWPLDPPYNCJAEJZFCYLXPHTESESPDLXPVPJLYODPYOEZESPEL'

def decoding(cipher):
    '''try to decoding cipher and show all result'''
    def transform(key):
        for alpha in key:
            beta = key[alpha]
            beta = chr(ord(beta) + 1)
            if ord(beta) > ord('Z'):
                beta = 'A'
            key[alpha] = beta
        return key

    key = {}
    cipherTest = ''
    for alpha in range(ord('A'), ord('A')+26):
        key[chr(alpha)] = chr(alpha)
    for i in range(26):
        cipherTest = ''
        for j in range(len(cipher)):
            cipherTest += key[cipher[j]]
        key = transform(key)
        print('{0:2} : {1}'.format(i, cipherTest))

decoding(cipher)

amrzs@ubuntu:security$ python3 caesarTest.py > result.txt

 0 : AWPLDPPYNCJAEJZFCYLXPHTESESPDLXPVPJLYODPYOEZESPEL
 1 : BXQMEQQZODKBFKAGDZMYQIUFTFTQEMYQWQKMZPEQZPFAFTQFM
 2 : CYRNFRRAPELCGLBHEANZRJVGUGURFNZRXRLNAQFRAQGBGURGN
 3 : DZSOGSSBQFMDHMCIFBOASKWHVHVSGOASYSMOBRGSBRHCHVSHO
 4 : EATPHTTCRGNEINDJGCPBTLXIWIWTHPBTZTNPCSHTCSIDIWTIP
 5 : FBUQIUUDSHOFJOEKHDQCUMYJXJXUIQCUAUOQDTIUDTJEJXUJQ
 6 : GCVRJVVETIPGKPFLIERDVNZKYKYVJRDVBVPREUJVEUKFKYVKR
 7 : HDWSKWWFUJQHLQGMJFSEWOALZLZWKSEWCWQSFVKWFVLGLZWLS
 8 : IEXTLXXGVKRIMRHNKGTFXPBMAMAXLTFXDXRTGWLXGWMHMAXMT
 9 : JFYUMYYHWLSJNSIOLHUGYQCNBNBYMUGYEYSUHXMYHXNINBYNU
10 : KGZVNZZIXMTKOTJPMIVHZRDOCOCZNVHZFZTVIYNZIYOJOCZOV
11 : LHAWOAAJYNULPUKQNJWIASEPDPDAOWIAGAUWJZOAJZPKPDAPW
12 : MIBXPBBKZOVMQVLROKXJBTFQEQEBPXJBHBVXKAPBKAQLQEBQX
13 : NJCYQCCLAPWNRWMSPLYKCUGRFRFCQYKCICWYLBQCLBRMRFCRY
14 : OKDZRDDMBQXOSXNTQMZLDVHSGSGDRZLDJDXZMCRDMCSNSGDSZ
15 : PLEASEENCRYPTYOURNAMEWITHTHESAMEKEYANDSENDTOTHETA
16 : QMFBTFFODSZQUZPVSOBNFXJUIUIFTBNFLFZBOETFOEUPUIFUB
17 : RNGCUGGPETARVAQWTPCOGYKVJVJGUCOGMGACPFUGPFVQVJGVC
18 : SOHDVHHQFUBSWBRXUQDPHZLWKWKHVDPHNHBDQGVHQGWRWKHWD
19 : TPIEWIIRGVCTXCSYVREQIAMXLXLIWEQIOICERHWIRHXSXLIXE
20 : UQJFXJJSHWDUYDTZWSFRJBNYMYMJXFRJPJDFSIXJSIYTYMJYF
21 : VRKGYKKTIXEVZEUAXTGSKCOZNZNKYGSKQKEGTJYKTJZUZNKZG
22 : WSLHZLLUJYFWAFVBYUHTLDPAOAOLZHTLRLFHUKZLUKAVAOLAH
23 : XTMIAMMVKZGXBGWCZVIUMEQBPBPMAIUMSMGIVLAMVLBWBPMBI
24 : YUNJBNNWLAHYCHXDAWJVNFRCQCQNBJVNTNHJWMBNWMCXCQNCJ
25 : ZVOKCOOXMBIZDIYEBXKWOGSDRDROCKWOUOIKXNCOXNDYDRODK

第15行是什么?拉出来看看

15 : PLEASE ENCRYPT YOUR NAME WITH THE SAME KEY AND SEND TO THE TA

那么先把密钥弄出来,已经可以看出是位移15位

A  B  C  D  E  F  G  H  I  J  K  L  M  N  O  P  Q  R  S  T  U  V  W  X  Y  Z  
P  Q  R  S  T  U  V  W  X  Y  Z  A  B  C  D  E  F  G  H  I  J  K  L  M  N  O  
KEY_SHIFT = 15

def getKey(shift):
    '''get the key with the shift'''
    key = {}
    for alpha in range(ord('A'), ord('A')+26):
        beta = alpha + shift - 26 if alpha + shift > ord('Z') else alpha + shift
        key[chr(alpha)] = chr(beta)
    return key

def printKey(key):
    '''print the key'''
    for alpha in range(ord('A'), ord('A')+26):
        print('{0} '.format(chr(alpha)), end=' ')
    print()
    for alpha in range(ord('A'), ord('A')+26):
        print('{0} '.format(key[chr(alpha)]), end=' ')
    print()

key = getKey(KEY_SHIFT)
printKey(key)

接下来就是加密自己的名字

def encrypt(key, plaintext):
    '''encrypt the string and return the cipher'''
    ciphertext = ''
    for i in range(len(plaintext)):
        ciphertext += key[plaintext[i]]
    return ciphertext

MY_NAME = 'AMRZS' #english name

cipher = encrypt(key, MY_NAME)
print(cipher)
amrzs@ubuntu:security$ python3 caesar.py 
PBGOH

你可能感兴趣的:(凯撒密码破译)