2017-06-13 57 views
1

今天我做了一個.py文件,它解密用vigenere方塊加密的字符串。我已經得到了這麼多,但我似乎不能爲ciphr列表和encr_txt添加空格,因爲它會對解密的消息進行加密。 「消息是,你好,我的名字很苗條」,你會得到「消息是,hellprvmwhwebwrw k d 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>

代碼:

# -*- 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() 

回答

2

添加空格來表改變了密碼的工作方式。你不能指望做出這種改變,也不會影響消息加密和解密的方式!

另外,表格的最後一行不正確。它與第一行相同,但它應該佔據第一位的空間。

+0

有關如何添加空間而不篡改郵件的任何想法? – PizzaCat

+0

@PizzaCat你不能。您嘗試解密的郵件使用跳過其輸入中的空格字符的密碼進行加密。您有_have_離開空間獨自正確地解密它! – duskwuff

+0

我想知道我是否可以像這樣寫https://pastebin.com/LCfhwRys – PizzaCat