Question

In: Computer Science

Create a Columnar Transposition Cipher in Python that encrypts/decrypts the word "Crypt".

Create a Columnar Transposition Cipher in Python that encrypts/decrypts the word "Crypt".

Solutions

Expert Solution

Code:

import math

key = "HACK"

# Encryption

def encryptMessage(msg):

    cipher = ""

# track key indices

    k_indx = 0

msg_len = float(len(msg))

    msg_lst = list(msg)

    key_lst = sorted(list(key))

# calculate column of the matrix

    col = len(key)

# calculate maximum row of the matrix

    row = int(math.ceil(msg_len / col))

# add the padding character '_' in empty

    # the empty cell of the matix

    fill_null = int((row * col) - msg_len)

    msg_lst.extend('_' * fill_null)

    # create Matrix and insert message and

    # padding characters row-wise

    matrix = [msg_lst[i: i + col]

              for i in range(0, len(msg_lst), col)]

    # read matrix column-wise using key

    for _ in range(col):

        curr_idx = key.index(key_lst[k_indx])

        cipher += ''.join([row[curr_idx]

                          for row in matrix])

        k_indx += 1

    return cipher

# Decryption

def decryptMessage(cipher):

    msg = ""

    # track key indices

    k_indx = 0

    # track msg indices

    msg_indx = 0

    msg_len = float(len(cipher))

    msg_lst = list(cipher)

    # calculate column of the matrix

    col = len(key)

     

    # calculate maximum row of the matrix

    row = int(math.ceil(msg_len / col))

    # convert key into list and sort

    # alphabetically so we can access

    # each character by its alphabetical position.

    key_lst = sorted(list(key))

    # create an empty matrix to

    # store deciphered message

    dec_cipher = []

    for _ in range(row):

        dec_cipher += [[None] * col]

    # Arrange the matrix column wise according

    # to permutation order by adding into new matrix

    for _ in range(col):

        curr_idx = key.index(key_lst[k_indx])

        for j in range(row):

            dec_cipher[j][curr_idx] = msg_lst[msg_indx]

            msg_indx += 1

        k_indx += 1

    # convert decrypted msg matrix into a string

    try:

        msg = ''.join(sum(dec_cipher, []))

    except TypeError:

        raise TypeError("This program cannot",

                        "handle repeating words.")

    null_count = msg.count('_')

    if null_count > 0:

        return msg[: -null_count]

    return msg

# Driver Code

msg = "Geeks for Geeks"

cipher = encryptMessage(msg)

print("Encrypted Message: {}".

               format(cipher))

print("Decryped Message: {}".

       format(decryptMessage(cipher)))

Code and Output Snapshot:


Related Solutions

write a Java method that decrypts a keyword columnar transposition cipher with a given key
write a Java method that decrypts a keyword columnar transposition cipher with a given key
You are writing a program that encrypts or decrypts messages using a simple substitution cipher. Your...
You are writing a program that encrypts or decrypts messages using a simple substitution cipher. Your program will use two constant strings. One will represent the code for encryption: going from the original message (called the plaintext) to the encrypted version of the message. The other will be “abcdefghijklmnopqrstuvwxyz” (the lowercase alphabet. Your program will ask the user whether they want to 1) encrypt a message, 2) decrypt a message, or 3) quit. If they choose encrypt or decrypt, you...
PYTHON Write a python program that encrypts and decrypts the user input. Note – Your input...
PYTHON Write a python program that encrypts and decrypts the user input. Note – Your input should be only lowercase characters with no spaces. Your program should have a secret distance given by the user that will be used for encryption/decryption. Each character of the user’s input should be offset by the distance value given by the user For Encryption Process: Take the string and reverse the string. Encrypt the reverse string with each character replaced with distance value (x)...
1 Introduction A cipher is an algorithm which encrypts a message into cipher text so that...
1 Introduction A cipher is an algorithm which encrypts a message into cipher text so that it can be safely transmitted without an eavesdropper being able to (easily) read it. For the purposes of this assignment the message and ciphertext will be stored as strings of ASCII characters. Cipher algorithms always perform two tasks: encryption and decryption. The encryption process takes a “message” and “key” as inputs and produces cipher text. The decryption process performs the reverse: it turns cipher...
Write a program that encrypts and decrypts the user input. Note – Your input should be...
Write a program that encrypts and decrypts the user input. Note – Your input should be only lowercase characters with no spaces. Your program should have a secret distance given by the user that will be used for encryption/decryption. Each character of the user’s input should be offset by the distance value given by the user For Encryption Process: Take the string and reverse the string. Encrypt the reverse string with each character replaced with distance value (x) given by...
Desc: Encrypts or decrypts a file. Input: The user supplies the character '1' to encrypt, or...
Desc: Encrypts or decrypts a file. Input: The user supplies the character '1' to encrypt, or '2' to decrypt via the keyboard.   The user also supplies the name of the source file via the keyboard.   Output: If the user wants to encrypt, the text in input file is encrypted and the encrypted text is stored in "encrypted.txt". The original file is not changed. If the user wants to decrypt, the text in input file is decrypted and the decrypted text...
Programing Language: Java The Problem: You are writing a program that encrypts or decrypts messages using...
Programing Language: Java The Problem: You are writing a program that encrypts or decrypts messages using a simple substitution cipher. Your program will use two constant strings. One will represent the code for encryption: going from the original message (called the plaintext) to the encrypted version of the message. The other will be “abcdefghijklmnopqrstuvwxyz” (the lowercase alphabet. Your program will ask the user whether they want to 1) encrypt a message, 2) decrypt a message, or 3) quit. If they...
A Caesar cipher encrypts a message by shifting letters in the alphabet. For example, a shift...
A Caesar cipher encrypts a message by shifting letters in the alphabet. For example, a shift of 4 maps 'a' to 'e' and maps 'p' to 't' Here is a famous line from Shakespeare encrypted with a shift of 4: 'vq dg qt pqv vq dg: vjcv ku vjg swguvkqp.' (a) Write a program that takes as input a string to be encrypted and an integer encrpytion shift (such as 4 mentioned earlier), and prints the encrypted string. [Hint: zip()...
Cryptography: Using columnar cipher, find the plaintext and the key that generated this ciphertext: ykccjosaiawiekhriogrrlrni Keep...
Cryptography: Using columnar cipher, find the plaintext and the key that generated this ciphertext: ykccjosaiawiekhriogrrlrni Keep in mind that only letter j was used for padding. (Show your detailed work)
Based on Rectangle transposition techniques, decrypt the following cipher text “LTHBPEEMOSRAIAESIGCVDENTUUBWEFSONES”. Then use the same key...
Based on Rectangle transposition techniques, decrypt the following cipher text “LTHBPEEMOSRAIAESIGCVDENTUUBWEFSONES”. Then use the same key to encrypt the following plain text “the automorphism group is more difficult”.           there is no given key
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT