Question

In: Computer Science

The prompt is using Python:  Write a 3 rail transposition encryption algorithm, and a corresponding decryption algorithm....

The prompt is using Python:  Write a 3 rail transposition encryption algorithm, and a corresponding decryption algorithm. Implement these two algorithms in their own function. Now write a testing function that demonstrates your algorithms work for all interesting cases!

Solutions

Expert Solution

# 3Rail Cipher Encryption

# and Decryption

  

# function to encrypt a message

def encryptRail(text, key):

  

    # create the matrix to cipher

    # plain text key = rows ,

    # length(text) = columns

    # filling the rail matrix

    # to distinguish filled

    # spaces from blank ones

    rail = [['\n' for i in range(len(text))]

                  for j in range(key)]

      

    # to find the direction

    dir_down = False

    row, col = 0, 0

      

    for i in range(len(text)):

          

        # check the direction of flow

        # reverse the direction if we've just

        # filled the top or bottom rail

        if (row == 0) or (row == key - 1):

            dir_down = not dir_down

          

        # fill the corresponding alphabet

        rail[row][col] = text[i]

        col += 1

          

        # find the next row using

        # direction flag

        if dir_down:

            row += 1

        else:

            row -= 1

    # now we can construct the cipher

    # using the rail matrix

    result = []

    for i in range(key):

        for j in range(len(text)):

            if rail[i][j] != '\n':

                result.append(rail[i][j])

    return("" . join(result))

      

# This function receives cipher-text

# and key and returns the original

# text after decryption

def decryptRail(cipher, key):

  

    # create the matrix to cipher

    # plain text key = rows ,

    # length(text) = columns

    # filling the rail matrix to

    # distinguish filled spaces

    # from blank ones

    rail = [['\n' for i in range(len(cipher))]

                  for j in range(key)]

      

    # to find the direction

    dir_down = None

    row, col = 0, 0

      

    # mark the places with '*'

    for i in range(len(cipher)):

        if row == 0:

            dir_down = True

        if row == key - 1:

            dir_down = False

          

        # place the marker

        rail[row][col] = '*'

        col += 1

          

        # find the next row

        # using direction flag

        if dir_down:

            row += 1

        else:

            row -= 1

              

    # now we can construct the

    # fill the rail matrix

    index = 0

    for i in range(key):

        for j in range(len(cipher)):

            if ((rail[i][j] == '*') and

               (index < len(cipher))):

                rail[i][j] = cipher[index]

                index += 1

          

    # now read the matrix in

    # zig-zag manner to construct

    # the resultant text

    result = []

    row, col = 0, 0

    for i in range(len(cipher)):

          

        # check the direction of flow

        if row == 0:

            dir_down = True

        if row == key-1:

            dir_down = False

              

        # place the marker

        if (rail[row][col] != '*'):

            result.append(rail[row][col])

            col += 1

              

        # find the next row using

        # direction flag

        if dir_down:

            row += 1

        else:

            row -= 1

    return("".join(result))

  

# Driver code

if __name__ == "__main__":

    print(encryptRail("attack at once, 2))

    print(encryptRail("defend the east wall",3))

      

    # Now decryption of the

    # same cipher-text

    print(decryptRail("dnhaweedtees alf tl", 3))

    print(decryptRail("atc toctaka ne", 2))

#output:-

#Encryption output:-

atc toctaka ne
dnhaweedtees alf  tl
#Decryption output:-
attack at once
​​​​​
delendfthe east wal

Related Solutions

Alice is sending message “HIDE” to Bob. Perform encryption and decryption using RSA algorithm, with the...
Alice is sending message “HIDE” to Bob. Perform encryption and decryption using RSA algorithm, with the following information: parameters p=11,q=5, e=7 Present all the information that you will need to encrypt and decrypt only the first letter from text
please solve the following in python: write a program for the decryption of 2-rail fence cipher....
please solve the following in python: write a program for the decryption of 2-rail fence cipher. For example, the input "Cmhmtmrooeoeoorw" should return "Comehometomorrow", and input "topaesw lyr" should return "two players".
please solve the following in python: write a program for the decryption of 2-rail fence cipher....
please solve the following in python: write a program for the decryption of 2-rail fence cipher. For example, the input "Cmhmtmrooeoeoorw" should return "Comehometomorrow", and input "topaesw lyr" should return "two players".
The mathematical expression of the encryption and decryption process of a Caesar cipher algorithm is expressed...
The mathematical expression of the encryption and decryption process of a Caesar cipher algorithm is expressed respectively as: c=Ep, k=p+k%26                                                                                                                         (1) p=Dc,k=c-k%26                                                                                                                         (2) Please do the following: Write at least two paragraphs to explain the principle of operation of the algorithm. For a full credit, your explanation must show the architectural diagram of the encryption and decryption process. Write a program to implement the Caesar algorithm Code must have two functions; encryption and decryption Test your codes with p as...
write a C program which performs encryption and decryption of a message
write a C program which performs encryption and decryption of a message
In C++, write a program to implement the Caesar Cipher for both encryption and decryption. The...
In C++, write a program to implement the Caesar Cipher for both encryption and decryption. The program should be able to handle different keys by deciding the key at run time. Thank you :)
My question below Write a complete program that performs encryption/decryption of a text file. Encryption means...
My question below Write a complete program that performs encryption/decryption of a text file. Encryption means you need to scramble the words of a file to become secure. Decryption means you need to apply some rules to an encrypted file in order to find the original file with its original content. An example of encryption is to replace each letter in a file with its consecutive letter in the alphabet. Therefore, ‘a’ is replaced by ‘b’, ‘b’ is replaced by...
Write a program in c++ that can perform encryption/decryption. In the following let the alphabet A...
Write a program in c++ that can perform encryption/decryption. In the following let the alphabet A be A={A, a, B, b, . . ., “ ”, “.”,“’ ”}. The encoding is A→0, a→1, B→2, b→4, . . ., Z→50, z→51, “ ”→52, “.”→53 and “’”→54.
Write a program in java that can perform encryption/decryption. In the following let the alphabet A...
Write a program in java that can perform encryption/decryption. In the following let the alphabet A be A={A, a, B, b, . . ., “ ”, “.”,“’ ”}. The encoding is A→0, a→1, B→2, b→4, . . ., Z→50, z→51, “ ”→52, “.”→53 and “’”→54.
Your task is to write a C program which performs encryption and decryption of a message...
Your task is to write a C program which performs encryption and decryption of a message using the substitution cipher algorithm. Write a C program which performs encryption and decryption using the substitution cipher algorithm. Your program must be fully automated (ie: it does not take any interactive user input) and either encrypt or decrypt depending on the files which exist in the program’s directory. If message.txt exists your program should read that file, encrypt the contents, and write the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT