python希尔(Hill)密码加解密实现
作者:互联网
学校的实验之一。找个地方保存一下。。。。
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('这也能输错?')
# 统计字符出现的次数
程序实现如下:
标签:python,加解密,len,PlainListALL,range,Hill,CipherList,print,PlainList 来源: https://blog.csdn.net/VAEaaaa/article/details/114745017