Transpostion decryption is not working with some keys?
<p>Why is this transposition decryption code not working with some keys?</p>

<pre><code>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&lt;key:
        count2=count1
        while count2&lt;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&lt;col:
        count2=count1
        while count2&lt;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))
</code></pre>

<p><a href="http://pastebin.com/dfx2EWE6" rel="nofollow">OP's original code source</a></p>

<p>I have tried encrypting "hello world" with key 5, but at the time of decrypting I am getting the wrong result. Using other keys works fine.</p>

<p>The problem is that the string length (11) doesn't divide evenly into the key (5), so the string <code>"hello world"</code> encodes into the groups <code>h d-ew-lo-lr-ol</code> i.e. <code>"h dewlolrol"</code>.  Which is fine, but the decrypt routine chops <code>"h dewlolrol"</code> into <code>h d-ewl-olr-ol</code> and generates the wrong result, <code>"heoo wlldlr"</code>.</p>

<p>A couple of possible ways to fix this:</p>

<p>1) Replace the <code>encryptbox</code> string with an array and pad the encryption units into even width segments: <code>h d-ew -lo -lr -ol</code> i.e. <code>"h dew lo lr ol "</code></p>

<p>This will allow your decrypt routine to work but you'll end up with spaces at the end of the decryption and the encrypted string will be a different size than the original.</p>

<p><strong>OR</strong></p>

<p>2) Dynamically adjust your decryption logic to figure out, based on the length of the remaining string to decode, and remaining number of expected segments, how much the segments must shrink.  This means your decrypt routine can't be as similar to the encrypt routine as it is now.  But it will allow you to handle the output of the current encrypt routine and the encrypted string can remain the same length as the original.</p>

<p>Below is a rough rework along the lines of approach #2 above -- you can see it allows the encrypt routine to remain simple but the decrypt routine has to be more complex to make up for it:</p>

<pre><code>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 &lt; length:
            decrypted += string[offset]
            offset += remaining_width

            remaining_key -= 1

            if remaining_key &gt; 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))
</code></pre>

<p>**OUTPUT*</p>

<pre><code>&gt; python3 test.py "hello world" 5
h dewlolrol
hello world
&gt;
</code></pre>

python python-3.x encryption