2016-08-26 2 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 문자열을 교체 작동하지만 암호화가 끝나면 공백이 생기고 암호화 된 문자열은 원본과 다른 크기가됩니다.

또는

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 
> 
관련 문제