维吉尼亚密码、Hill密码和仿射密码的加解密及字母出现的频率统计(python实现)

import string

print('------------------------------------Vigenere加解密---------------------------------------------------------------------------------------------------------------------------------------------------------------')
key=''
plaintext=''
key=input('请输入你的密钥:')
plaintext=input('请输入你想要的加密的内容:')
a='abcdefghijklmnopqrstuvwxyz'
keylen=len(key)
ptlen=len(plaintext)
ciphertext = ''
i = 0
while i < ptlen:
    j = i % keylen
    k = a.index(key[j])
    m = a.index(plaintext[i])
    ciphertext += a[(m+k)%26]
    i += 1
print(ciphertext)
key=input('请输入你的密钥:')
ciphertext=input('请输入你想要的解密的内容:')
a='abcdefghijklmnopqrstuvwxyz'
keylen=len(key)
ctlen=len(ciphertext)
plaintext = ''
i = 0
while i < ctlen:
    j = i % keylen
    k = a.index(key[j])
    m = a.index(ciphertext[i])
    if m < k:
        m += 26
    plaintext += a[m-k]
    i += 1
print(plaintext)
print('------------------------------------HILL加解密-------------------------------------------------------------------------------------------------------------------------------------------------------------------')
encryptKeys=[[10,5,12,0,0],[3,14,21,0,0],[8,9,11,0,0],[0,0,0,11,8],[0,0,0,3,7]]
decryptKeys=[[21,15,17,0,0],[23,2,16,0,0],[25,4,3,0,0],[0,0,0,7,18],[0,0,0,23,11]]
first = 97
temp=0
num=0
dir = {
     }
for i in range(26):
    dir[chr(first)]=i
    first=first+1
plaintext = 'thestatekeylaboratoryofnetworkingandswitchingtechnologybelongstobeijinguniversityofpostsandtelecommunicationthelaboratorywasopenedinnineteenninetytwoinnineteenninetyfivethelaboratorypassedacceptanceinspectionbygovernmentandanevaluationorganizedbyministryofscienceandtechnologyintwothousandandtwosincetwothousandandfourthelaboratoryhasbeenrenamedasthestatekeylaboratoryofnetworkingandswitchingtechnologybyministryofscienceandtechnology' #明文
PlainListALL=list(plaintext)
PlainList=[]
CipherList=[]
CipherText=''
for i in range(len(PlainListALL)):
    PlainListALL[i]=dir[PlainListALL[i]]
for i in PlainListALL:
    PlainList.append(i)
    num=num+1
    if(num%int(len(encryptKeys))==0):
            for i in range(len(encryptKeys)):
                for j in range(len(encryptKeys)):
                    temp+=PlainList[j]*encryptKeys[j][i]
                CipherList.append(temp)
                temp=0
            PlainList=[]
num=0
for i in range(len(CipherList)):
    CipherList[i]=CipherList[i]%26
    for keys,values in dir.items():
        if(CipherList[i]==values):
            CipherList[i]=keys
for i in CipherList:
    CipherText+=i
print('当前密文为')
print(CipherText)
Statistics={
     }
for i in  CipherText:
    if(i  not in Statistics):
        Statistics[i]=1
    else:
        Statistics[i]=Statistics[i]+1
print("各字符出现次数统计如下:")
print(Statistics)
print('是否要进行解密?(Y/N)')
judge =''
judge=input()
if(judge=='Y' or judge=='y'):
    CipherTextALL=CipherText
    CipherListALL=list(CipherTextALL)
    CipherList=[]
    PlainList=[]
    PlainText=''
    for i in range(len(CipherTextALL)):
        CipherListALL[i]=dir[CipherListALL[i]]
    for i in CipherListALL:
        CipherList.append(i)
        # print(PlainList)
        num=num+1
        if(num%int(len(decryptKeys))==0):
                for i in range(len(decryptKeys)):
                    for j in range(len(decryptKeys)):
                        temp+=CipherList[j]*decryptKeys[j][i]
                    PlainList.append(temp)
                    temp=0
                CipherList=[]
    for i in range(len(PlainList)):
        PlainList[i]=PlainList[i]%26
        for keys,values in dir.items():
            if(PlainList[i]==values):
                PlainList[i]=keys
    for i in PlainList:
        PlainText+=i
    print('解密后的明文为')
    print(PlainText)

print('--------------------------------仿射密码加解密-------------------------------------------------------------------------------------------------------------------------------------------------------------------')
plaintext_ = string.ascii_lowercase
ciphertext_ = string.ascii_uppercase
#加密算法
def encryption(plaintext):
    cipherarr = [0 for i in range(len(plaintext))]
    plaintext_list = list(plaintext)
    j = 0
    for plaintext_item in plaintext_list:
        for i in range(len(plaintext_)):
            if plaintext_item == plaintext_[i]:
                ciphertext = (11*i+4)%26
                cipherarr[j] = ciphertext_[ciphertext]
                j = j+1

    cipher = ''.join(cipherarr)
    return cipher


plaintext = input('请输入明文:')
cipher = encryption(plaintext)
print('密文是:',cipher)

Statistics={
     }
for i in  cipher:
    if(i  not in Statistics):
        Statistics[i]=1
    else:
        Statistics[i]=Statistics[i]+1
print("各字符出现次数统计如下:")
print(Statistics)

def decryption(ciphertext):
    plaintext_arr = [0 for i in range(len(ciphertext))]
    cipherlist = list(ciphertext)

    j = 0
    for cipheritem in cipherlist:
        for i in range(len(ciphertext_)):
            if cipheritem == ciphertext_[i]:
                plaintext = (19*i-24)%26
                plaintext_arr[j] = plaintext_[plaintext]
                j = j+1

    plain = ''.join(plaintext_arr)
    return plain

while True:
    ciphertext = input('请输入密文:')
    plain = decryption(ciphertext)
    if ciphertext == 'EXIT':
        break
    print('明文输出为:',plain)

你可能感兴趣的:(Cryptography,python,加密解密,算法,密码学)