2016-08-26 48 views
0

這個換位解密代碼爲什麼不能用於某些鍵?轉置解密不適用於某些密鑰?

def transencrypt(word,key): 
    '''Traspositon encryption function. This function is used to encrypt a line 
using the transposition encryption method. To know how transpositon encryption 
works you can visit here https://en.wikipedia.org/wiki/Transposition_cipher.''' 
    count1=0 
    count2=0 
    encrypted='' 
    encryptbox=['']*key 
    while count1<key: 
     count2=count1 
     while count2<len(word): 
      encryptbox[count1]+=word[count2] 
      count2+=key 
     encrypted+=encryptbox[count1] 
     count1+=1 
    return encrypted 

def transdecrypt(word,key): 
    '''This Function is for the decrypting the encrypted strings encrypted by 
transencrypt().This function only requires the encrypted string and the key 
with which it has been decrypted.''' 
    import math 
    count1=0 
    count2=0 
    decrypted='' 
    col=int(math.ceil(len(word)/key)) 
    decryptbox=['']*col 
    while count1<col: 
     count2=count1 
     while count2<len(word): 
      decryptbox[count1]+=word[count2] 
      count2+=col 
     decrypted+=decryptbox[count1] 
     count1+=1 
    return decrypted 

print(transencrypt('hello world',5)) 
print(transdecrypt('h dewlolrol',5)) 

OP's original code source

我試圖與鍵5加密的「hello world」,但解密的時候我得到錯誤的結果。使用其他鍵可以正常工作。

+0

你可以在這裏發佈你的代碼而不是在pastebin上嗎?閱讀起來更容易,而且您的代碼始終與您的問題相關聯。 – MichaelDotKnox

+0

他們編輯它我猜。你現在可以讀嗎? – Star

+0

@Star你期望得到什麼,取而代之的是什麼?你能描述一下你的代碼應該如何運作嗎? –

回答

0

問題是,字符串長度(11)沒有均勻分配到密鑰(5)中,所以字符串"hello world"編碼爲組h d-ew-lo-lr-ol,即"h dewlolrol"。這很好,但解密例程將"h dewlolrol"改爲h d-ewl-olr-ol,並生成錯誤結果"heoo wlldlr"

幾個可能的方式來解決這個:

1)以與陣列和墊更換encryptbox串加密單元分爲偶數寬度段:h d-ew -lo -lr -ol"h dew lo lr ol "

這將允許你的解密程序來工作,但在解密結束時最終會出現空格,並且加密的字符串將與原始大小不同。

OR

2)動態調整您解密邏輯弄清楚,基於剩餘的字符串的長度進行解碼,和預期段,該段必須有多少縮水的剩餘數量。這意味着您的解密例程不能像現在那樣與加密例程相似。但它將允許您處理當前加密例程的輸出,並且加密的字符串可以保持與原始字符長度相同的長度。

下面是沿着上述辦法#2線粗略返工 - 你可以看到它允許加密程序保持簡單,但解密程序有更復雜的,以彌補它:

import math 

def transencrypt(string, key): 
    ''' 
    Transpositon encryption function. This function is used to encrypt a line 
    using the transposition encryption method. To learn how transpositon encryption 
    works you can visit here https://en.wikipedia.org/wiki/Transposition_cipher. 
    ''' 

    encrypted = '' 
    length = len(string) 

    for start in range(key): 
     for offset in range(start, length, key): 
      encrypted += string[offset] 

    return encrypted 

def transdecrypt(string, key): 
    ''' 
    This function is for the decrypting the strings encrypted by 
    transencrypt(). This function only requires the encrypted 
    string and the key with which it was decrypted. 
    ''' 

    decrypted = '' 
    length = len(string) 
    width = int(math.ceil(length/key)) 

    for start in range(width): 
     offset = start 
     remaining_key = key 
     remaining_length = length 
     remaining_width = width 

     while offset < length: 
      decrypted += string[offset] 
      offset += remaining_width 

      remaining_key -= 1 

      if remaining_key > 0: 
       remaining_length -= remaining_width 
       remaining_width = int(math.ceil(remaining_length/remaining_key)) 

    return decrypted[:length] 

if __name__ == '__main__': 
    import sys 

    string = sys.argv[1] 
    key = int(sys.argv[2]) 

    print(transencrypt(string, key)) 
    print(transdecrypt(transencrypt(string, key), key)) 

** OUTPUT *

> python3 test.py "hello world" 5 
h dewlolrol 
hello world 
>