python希尔(Hill)密码加解密实现

本文介绍了一种基于矩阵运算的文本加密与解密方法,通过定义加密与解密密钥实现了对英文文本的安全处理。文章详细展示了加密过程,包括明文到密文的转换,并提供了密文统计分析。此外,还给出了相应的解密步骤,验证了方法的有效性。

开发板推荐:天空星STM32F407VET6开发板

超高性价比 STM32主控 | 超高主频 | 一板兼容百芯 | 比赛神器 | 沉金彩色丝印

学校的实验之一。找个地方保存一下。。。。

encryptionKeys=[[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]]
decryptionKeys=[[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
PlainTextALL = 'In several distributed systems a user should only be able to access data if a user posses a certain set of credentials or attributes Currently the only method for enforcing such policies is to employ a trusted server to store the data and mediate access control However if any server storing the data is compromised then the confidentiality of the data will be compromised In this paper we present a system for realizing complex access control on encrypted data that we call ciphertext-policy attribute-based encryption By using our techniques encrypted data can be kept confidential even if the storage server is untrusted moreover our methods are secure against collusion attacks' #明文
PlainTextALL=PlainTextALL.lower().replace(' ','').replace('-','')  #去掉字符之间的空格和-,全部变成小写
PlainListALL=list(PlainTextALL)
PlainList=[]
CipherList=[]
CipherText=''
# print(PlainListALL)
for i in range(len(PlainListALL)):
    PlainListALL[i]=dir[PlainListALL[i]]
# print("PlainListALL如下:")
# print(PlainListALL)
print('\n')
for i in PlainListALL:
    PlainList.append(i)
    # print(PlainList)
    num=num+1
    if(num%int(len(encryptionKeys))==0):
            for i in range(len(encryptionKeys)):
                for j in range(len(encryptionKeys)):
                    temp+=PlainList[j]*encryptionKeys[j][i]
                CipherList.append(temp)
                temp=0
            PlainList=[]
num=0
# print('CipherList如下')
# print(CipherList)

for i in range(len(CipherList)):
    CipherList[i]=CipherList[i]%26
    # print(CipherList[i],end=' ')
    for keys,values in dir.items():
        if(CipherList[i]==values):
            CipherList[i]=keys
for i in CipherList:
    CipherText+=i
print('\n密文为')
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("解密方法如下:\n")
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]]
    # print("密文列表如下:")
    # print(CipherListALL)
    print('\n')
    for i in CipherListALL:
        CipherList.append(i)
        # print(PlainList)
        num=num+1
        if(num%int(len(decryptionKeys))==0):
                for i in range(len(decryptionKeys)):
                    for j in range(len(decryptionKeys)):
                        temp+=CipherList[j]*decryptionKeys[j][i]
                    PlainList.append(temp)
                    temp=0
                CipherList=[]
    # print('明文列表如下')
    # print(PlainList)
    for i in range(len(PlainList)):
        PlainList[i]=PlainList[i]%26
        # print(PlainList[i],end=' ')
        for keys,values in dir.items():
            if(PlainList[i]==values):
                PlainList[i]=keys
    for i in PlainList:
        PlainText+=i
    print('\n明文为')
    print(PlainText)
elif(judge=='N' or 'n'):
    print('拜拜~~')
else:
    print('这也能输错?')
# 统计字符出现的次数

程序实现如下:
在这里插入图片描述

开发板推荐:天空星STM32F407VET6开发板

超高性价比 STM32主控 | 超高主频 | 一板兼容百芯 | 比赛神器 | 沉金彩色丝印

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值