2016-08-26 6 views
0

このトランスポーズ復号化コードが一部のキーで機能しないのはなぜですか?私は5キーでの「Hello World」を暗号化しようとしているTranspostionの復号化が一部のキーで機能していませんか?

OP's original code source

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)) 

が、復号時に私が間違った結果を取得しています。他のキーを使用すると正常に動作します。

+0

あなたはここではなく、ペーストビンにあなたのコードを投稿することができますか?読みやすく、コードが常にあなたの質問に関連付けられています。 – 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"

この問題を解決するために可能ないくつかの方法が

:これはにあなたの復号化ルーチンをできるようになりますh d-ew -lo -lr -olすなわち"h dew lo lr ol "

1)にも幅のセグメントに配列し、パッドで暗号化ユニットをencryptbox文字列を交換してください暗号化された文字列はオリジナルとは異なるサイズになります。

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 
> 
関連する問題