我根据Vigenere密码的思想编写了这个加密器,但是它不是只使用一个密钥,而是从现有密钥中“生成”另一个密钥。第二个键的长度也取决于第一个键中的不同字符。因此,它在转换字母时使用了这两个键。
def scram(key):
key2 = []
#make the length of the second key varying from key to key for harder cracking
length = (key[0]-32)+(key[int(len(key)/2)]-32)+(key[len(key)-1]-32)
#make the max length = 256
length = length % 256
#if the length is less than 64, multiply by two to make it longer
while(length < 64):
length*=2
#scrambles the letters around
for x in range(length):
#basically shifts the key according to the current character,
#how many times it has looped the key, where it is in the key,
#and the modulo of x and a few prime numbers to make sure that
#an overlap/repeat doesn't happen.
toapp = (x%(len(key)-1)) + (x/(len(key)-1)) + (key[x%(len(key)-1)]) + (x%3) +(x%5)+(x%53)+(x%7)
toapp = int(toapp % 94)
key2.append(toapp)
return key2
def cipher(mes,key,ac):
#makes the second key
key2 = scram(key)
res=[]
#do proper shifting in the keys
for x in range(len(mes)):
temp=mes[x]
if(action == "2"):
temp = temp - key[x%(len(key)-1)] - key2[x%(len(key2)-1)]
else:
temp = temp + key[x%(len(key)-1)] + key2[x%(len(key2)-1)]
temp = int(temp % 94)
res.append(chr(temp+32))
return res
#encrypt or decrypt
action = input("type 1 to encypt. type 2 to decrypt:")
#input
m= input("Text:")
k= input("key - 4 or more char:")
#changes the letters to ascii value
mes= []
for x in m:
mes.append(ord(x)-32)
key= []
for x in k:
key.append(ord(x)-32)
#encrypts it
result = cipher(mes,key,action)
for x in result:
print(x,end="")
print("")
y = input("Press enter to continue...")有没有更有效的方法来做到这一点?这是加密文本的安全方法吗?你能破解这个程序加密的文本吗?
发布于 2015-08-29 21:36:03
m= input("Text:")
k= input("key - 4 or more char:")
#changes the letters to ascii value
mes= []
for x in m:
mes.append(ord(x)-32)
key= []
for x in k:
key.append(ord(x)-32)这一切都可以缩短为两个清单的理解:
mes = [ord(x) - 32 for x in input("Text: ")]
key = # Exercise for the reader中的全局
if(action == "2"):动作应该是函数的一个参数。
temp永存 temp=mes[x]您命名一个变量temp并使用它8行..。请给这个重要的变量起一个合理的名字。
toapp = (x%(len(key)-1)) + (x/(len(key)-1)) + (key[x%(len(key)-1)]) + (x%3) +(x%5)+(x%53)+(x%7) 请评论一下这个巨大的公式。
result = cipher(mes,key)
for x in result:
print(x,end="")
print("")在python中,避免外部变量赋值是惯用的,而显式循环很少是真正必要的,这一切都可以缩短为:
print(''.join(cipher(mes,key)))最后一行应该是:
input("Enter to continue...")https://codereview.stackexchange.com/questions/102246
复制相似问题