2012-09-04 86 views
-2
from random import shuffle 

alphabet="abcdefghijklmnopqrstuvwxyz" 

def substitution(alphabet,plaintext): 

    # Create array to use to randomise alphabet position 
    randarray=range(0,len(alphabet)) 
    shuffle(randarray) 

    key="Zebra" 

    #Create our substitution dictionary 
    dic={} 
    for i in range(0,len(alphabet)): 
     key+=alphabet[randarray[i]] 
     dic[alphabet[i]]=alphabet[randarray[i]] 

    #Convert each letter of plaintext to the corrsponding 
    #encrypted letter in our dictionary creating the cryptext 
    ciphertext="" 
    for l in plaintext: 
     if l in dic: 
      l=dic[l] 
     ciphertext+=l 
    for i in alphabet: 
     print i, 
    print 
    for i in key: 
     print i, 
    print 
    return ciphertext,key 

# This function decodes the ciphertext using the key and creating 
# the reverse of the dictionary created in substitution to retrieve 
# the plaintext again 
def decode(alphabet,ciphertext,key): 

    dic={} 
    for i in range(0,len(key)): 
     dic[key[i]]=alphabet[i] 

    plaintext="" 
    for l in ciphertext: 
     if l in dic: 
      l=dic[l] 
     plaintext+=l 

    return plaintext 

# Example useage 
plaintext="the cat sat on the mat" 
ciphertext,key=substitution(alphabet,plaintext) 
print "Key: ", key 
print "Plaintext:", plaintext 
print "Cipertext:", ciphertext 
print "Decoded :", decode(alphabet,ciphertext,key) 

當我運行這段代碼時,它返回一個"IndexError: String index out of range"錯誤。有人能幫我解決問題,我看不出問題所在。程序返回一個「IndexError:字符串索引超出範圍」。

 
Traceback (most recent call last): 
    File"/Users/Devlin/Desktop/Dev/Python/Substitution Cypher.py", line 57, in 
    print "Decoded :", decode(alphabet,ciphertext,key) 
    File "/Users/Devlin/Desktop/Dev/Python/Substitution Cypher.py", line 41, in decode 
    dic[key[i]]=alphabet[i] IndexError: string index out of range 
+0

後確切的錯誤信息,其中包括行號和完整回溯。 – BrenBarn

+0

'code'回溯(最近一次調用最後一次): print「Decoded:」,decode(alphabet,ciphertext,鍵) 解碼的文件「/ Users/Devlin/Desktop/Dev/Python/Substitute Cypher.py」,行41,解碼 dic [key [i]] = alphabet [i] IndexError:string index out of range'code ' – spoonless

+0

也很高興突出語法,如果您已經必須使用pastebin ... – wasyl

回答

0

的問題就在這裏:

def decode(alphabet,ciphertext,key): 
    dic={} 
    for i in range(0,len(key)): 
     dic[key[i]]=alphabet[i] # this line fails 

key在這一點上始終是31個字符,這是len('Zebra') + len(alphabet)。由於len(alphabet)總是26,alphabet[i]在i> 25時失敗。

我相信你在這裏誤解了key代表的內容。 substitution應該產生一個隨機密鑰,它不是任何密碼或鹽。實際上,如果您查看the original article you got this code from,您會看到key=""substitution,而不是一些隨機值。

0
​​

這裏,len(key) == len(alphabet) + 5。因此,i(遍歷range(0, len(key)))比字母表的實際長度更進一步。這是由於部分

key="Zebra" #HERE, YOU START WITH ZEBRA 

#Create our substitution dictionary 
dic={} 
for i in range(0,len(alphabet)): 
    key+=alphabet[randarray[i]] #AND HERE, YOU ADD TO ZEBRA 
    dic[alphabet[i]]=alphabet[randarray[i]] 

所以,同樣,你在keyalphabet得到了更多的字符,這會導致錯誤。

的解決辦法是改變線

key="Zebra" 

key="" 

爲什麼你希望它是 「斑馬」 呢?

* P.S range(x)相同range(0, x),所以你平時應該只寫range(len(key))等.. *

相關問題