# Arup Guha
# 9/21/2022
# Program to encrypt using column permutation.

def getPerm(key):

    # list size length of key.
    perm =[0]*len(key)

    idx = 0
    
    # Trying each letter 1 by 1 in order!
    for let in range(ord('A'), ord('Z')+1):

        # Go through the word!
        for i in range(len(key)):

            # We have a matching letter.
            if key[i] == chr(let):
                perm[idx] = i
                idx += 1

    # Return the forward permutation.
    return perm

# Encrypts msg using the permutation stored in numKey.
def encrypt(msg, numKey):

    res = ""

    # Go through perm array.
    for i in range(len(numKey)):

        # This is the column to read down.
        whichcol = numKey[i]

        # Read down this column; skip by length of key.
        for i in range(whichcol, len(msg), len(numKey)):
            res = res + msg[i]

    return res
        
def main():

    # Get the key and plaintext.
    key = input("Please enter the key, all uppercase letters.")
    key = key.strip()
    plain = input("Enter the message all uppercase letters.")
    plain.strip()

    # Convert the key to a permutation.
    numKey = getPerm(key)
    print(numKey)

    # This is the cipher text.
    cipher = encrypt(plain, numKey)
    print(cipher)

main()
