今天我制作了一个.py文件来解密用vigenere square加密的字符串。我已经做到这一点,但我似乎无法在ciphr列表和encr_txt中添加空格,因为它会使解密的消息变得混乱。而不是"消息是,你好我的名字是苗条的阴影",你得到"消息是,hellprvmwhwebwrw kd thady",好像我从encr_txt和ciphr列表中留出空格一个好消息。我不知道怎么解决这个也没有错误,我刚刚开始在python中编码,所以如果它显而易见我很抱歉。另外我知道这可以做得更容易,但我学习列表所以我选择这样做而不是这样的事情:
<a href="https://stackoverflow.com/questions/35711747/ascii-vigenere-cipher-not-decrypting-properly">Another question i found relating my problem but does not describe my situation</a>
&#13;
代码:
# -*- coding: utf-8 -*-
# ^ encoding
# Encrypted text
# encr_txt = 'tkedobaxoudqrrffhhhalbmmcnedeo'
encr_txt = 'qexpg vy zeen ie wdrm elsmy'
#encr_list = list(encr_txt)
txtpos = 0
# Key to ^
key = 'james'
keypos = 0
limit = len(encr_txt)
limitpos = 0
# Vigenere square
ciphr = ['abcdefghijklmnopqrstuvwxyz ',
'bcdefghijklmnopqrstuvwxyz a',
'cdefghijklmnopqrstuvwxyz ab',
'defghijklmnopqrstuvwxyz abc',
'efghijklmnopqrstuvwxyz abcd',
'fghijklmnopqrstuvwxyz abcde',
'ghijklmnopqrstuvwxyz abcdef',
'hijklmnopqrstuvwxyz abcdefg',
'ijklmnopqrstuvwxyz abcdefgh',
'jklmnopqrstuvwxyz abcdefghi',
'klmnopqrstuvwxyz abcdefghij',
'lmnopqrstuvwxyz abcdefghijk',
'mnopqrstuvwxyz abcdefghijkl',
'nopqrstuvwxyz abcdefghijklm',
'opqrstuvwxyz abcdefghijklmn',
'pqrstuvwxyz abcdefghijklmno',
'qrstuvwxyz abcdefghijklmnop',
'rstuvwxyz abcdefghijklmnopq',
'stuvwxyz abcdefghijklmnopqr',
'tuvwxyz abcdefghijklmnopqrs',
'uvwxyz abcdefghijklmnopqrst',
'vwxyz abcdefghijklmnopqrstu',
'wxyz abcdefghijklmnopqrtsuv',
'xyz abcdefghijklmnopqrtsuvw',
'yz abcdefghijklmnopqrtsuvwx',
'z abcdefghijklmnopqrtsuvwxy',
'abcdefghijklmnopqrtsuvwxyz ']
first = ciphr[0]
string = ''
def start():
global limitpos
limitpos += 1
global keypos
for i in ciphr:
if keypos == len(key):
keypos = 0
else:
pass
if i[0] == key[keypos]:
#print "%s, %s" % (i[0], i)
global currenti
currenti = i
#print currenti
finder()
break
else:
pass
def finder():
global keypos
global txtpos
done = False
position = 0
while done == False:
for i in currenti[position]:
if i == '_':
pass
if i == encr_txt[txtpos]:
global string
string = string + first[position]
#print "message is, %s" % string
keypos += 1
txtpos += 1
done = True
if limitpos == limit:
print "message is, %s" % string
break
else:
start()
else:
position += 1
pass
start()
答案 0 :(得分:2)
向表中添加空格会改变密码的工作方式。您不能期望进行这种更改,也不会影响邮件的加密和解密方式!
顺便说一句,表格的最后一行是不正确的。它与第一行相同,但它应该在第一个位置有空格。