Transposition Cipher

A transposition cipher encrypts messages as follows.
  1. For a plaintext (e.g., "ABCDEFGHIJKLMNOPQRSTUVWXYZ"), and a key value k (e.g., 4), you create a matrix whose height is k, and a suitable width so that the matrix can accommodate the whole plaintext.
  2. Fill the plaintext into the matrix in a row-wise order. If necessary, you may pad the matrix with some random characters (e.g., 'A', 'E', 'I', 'O', 'U').
  3. The matrix may look like
    A B C D E F G
    H I J K L M N
    O P Q R S T U
    V W X Y Z A A
  4. Read the characters out in a column-wise order, this gets you the ciphertext "AHOVBIPWCJQXDKRYELSZFMTAGNUA".

Write a program to read a line of plaintext, and a key, and output the encrypted text. The program may run as follows:

Please input the message to encrypt -- ABCDEFGHIJKLMNOPQRSTUVWXYZ

Please input the key -- 4

AHOVBIPWCJQXDKRYELSZFMTAGNUA


Decrypting the message is simple. As long as you know the key value, you can re-construct a matrix with height k, and fill in the ciphertext by column-wise order. Then read out the message in row-wise order, and you will obtain the original message.

Scytale Cipher