Question

In: Computer Science

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".

Solutions

Expert Solution

def decryptRailFence(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(encryptRailFence("attack at once", 2))

    print(encryptRailFence("GeeksforGeeks ", 3))

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

     

    # Now decryption of the

    # same cipher-text

    print(decryptRailFence("GsGsekfrek eoe", 3))

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

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


Related Solutions

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".
write a program for the decryption of 2-rail fence cipher. For example, the input "Cmhmtmrooeoeoorw" should...
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". in python
Problem 2: Caesar Cipher Decryption] Write a python method that takes two parameters: A parameter of...
Problem 2: Caesar Cipher Decryption] Write a python method that takes two parameters: A parameter of type str and a parameter of type int. The first parameter is the plaintext message, and the second parameter is the encryption key. The method strictly does the following tasks: a. Reverse the operations performed by the encryption method to obtain the plaintext message. The method’s header is as follows: def casesardecryption(s, key):
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 :)
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!
Write a complete and syntactically correct Python program to solve the following problem: Write a program...
Write a complete and syntactically correct Python program to solve the following problem: Write a program for your professor that allows him to keep a record of the students’ average grade in his class. The program must be written in accordance with the following specs: 1. The input must be interactive from the keyboard. You will take input for 12 students. 2. You will input the students’ name and an average grade. The student cannot enter an average below zero...
IN PYTHON Write a program to do the following: Solve the a set of equations as...
IN PYTHON Write a program to do the following: Solve the a set of equations as mentioned in "Zybook 5.20 LAB: Brute force equation solver". Instead of the arithmetic operators use your own function defined in a module named calc. You must provide two files (calc.py, brute_force_solver.py) :- 1) calc.py:- Add function named 'add' instead of using operator '+' [10pts] Add function named 'difference' instead of using operator '-' [10pts] Add function named 'product' instead of using operator '*' [10pts]...
In the caeser cipher encryption and decryption program below, what do the two lines if(ch >...
In the caeser cipher encryption and decryption program below, what do the two lines if(ch > 'z'){ ch = ch - 'z' + 'a' - 1; } if(ch < 'a'){ ch = ch + 'z' - 'a' + 1; } mean??? I understand that it has something to do with ASCII characters and makes sure that if the encryption/decryption character is more than "z", then it would loop back to "a" instead of outputting a charcter like "{" . I...
Write a complete and syntactically correct Python program to solve the following problem: You are the...
Write a complete and syntactically correct Python program to solve the following problem: You are the payroll manager for SoftwarePirates Inc. You have been charged with writing a package that calculates the monthly paycheck for the salespeople. Salespeople at SoftwarePirates get paid a base salary of $2000 per month. Beyond the base salary, each salesperson earns commission on the following scale: Sales Commission Rate Bonus <$10000 0% 0 $10000 – $100,000 2% 0 $100,001 - $500,000 15% $1000 $500,001 -...
Write a Python program in a file called consonants.py, to solve the following problem using a...
Write a Python program in a file called consonants.py, to solve the following problem using a nested loop. For each input word, replace each consonant in the word with a question mark (?). Your program should print the original word and a count of the number of consonants replaced. Assume that the number of words to be processed is not known, hence a sentinel value (maybe "zzz") should be used. Sample input/output: Please enter a word or zzz to quit:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT