Page 1 of 1

This is one full question. Please write in Python.

Posted: Tue Apr 12, 2022 10:23 am
by answerhappygod
This is one full question. Please write in Python.
This Is One Full Question Please Write In Python 1
This Is One Full Question Please Write In Python 1 (483.85 KiB) Viewed 42 times
4. The Caesar cipher is an example of a substitution cipher the letters in the plaintext are replaced by other letters to produce the ciphertext. In a transposition cipher, by contrast, we simply rearrange the letters of the plaintext to obtain the ciphertext. Here's an example of a transposition cipher that relies on repeatedly swapping characters in the plaintext. Inputs: plaintext, keyword (both of which contain only capital letters) For each letter c in the keyword: = • Convert c to a numeric value, with A 0, B = 1, C = 2, and so on. • Mod the numeric value from the previous step by the length of the plaintext to get an index i. The mod guarantees that i is a valid index in the plaintext. • Swap the characters at indices i and i +1 in the plaintext. If i refers to the last character of the plaintext, this should swap the last and first characters since index i + 1 would be out of bounds. For example, to encrypt the message SLOTH POWER with a keyword of TOP, we first clean up the plaintext to make it SLOTHPOWER. The length of this plaintext is 10. Then we iterate through each letter of the keyword:

• The numeric value of T is 19. 19 mod 10 is 9, so we swap indices 9 and 0 of SLOTHPOWER to get RLOTHPOWES. • The numeric value of O is 14. 14 mod 10 is 4, so we swap indices 4 and 5 of RLOTHPOWES to get RLOTPHOWES. • The numeric value of P is 15. 15 mod 10 is 5, so we swap indices 5 and 6 of RLOTPHOWES to get the ciphertext, RLOTPOHWES. The swaps above can be followed in reverse to decrypt the ciphertext. (a) (4 points) Within ciphers.py, write a function swap_encrypt (plaintext, keyword) that returns the ciphertext from applying the transposition cipher described above to encrypt plaintext using keyword. You may assume that both plaintext and keyword are non- empty. Clean up both the plaintext and keyword parameters by calling your previously written cleanup_text() function before proceeding. Call your previously written swap() function as needed. Do not call any built-in Python functions except len(), ord(), and range() in your solution. Here are some example arguments for this function and their expected return values: Arguments Return Value ('sloth', 'a') LSOTH' ('sloth', SLOTH (SLOTH POWER', 'TOP') RLOTPOHWES' 'aa') (b) (4 points) Within ciphers.py, write a function swap_decrypt(ciphertext, keyword) that returns the plaintext from applying the transposition cipher described above to de- crypt ciphertext using keyword. You may assume that both ciphertext and keyword are non-empty.

The swaps above can be followed in reverse to decrypt the ciphertext. (a) (4 points) Within ciphers.py, write a function swap_encrypt(plaintext, keyword) that returns the ciphertext from applying the transposition cipher described above to encrypt plaintext using keyword. You may assume that both plaintext and keyword are non- empty. Clean up both the plaintext and keyword parameters by calling your previously written cleanup-text() function before proceeding. Call your previously written swap() function as needed. Do not call any built-in Python functions except len(), ord(), and range() in your solution. Here are some example arguments for this function and their expected return values: Arguments ('sloth', 'a') ('sloth', 'aa') (SLOTH POWER', 'TOP') Return Value 'LSOTH SLOTH' 'RLOTPOHWES' (b) (4 points) Within ciphers.py, write a function swap_decrypt(ciphertext, keyword) that returns the plaintext from applying the transposition cipher described above to de- crypt ciphertext using keyword. You may assume that both ciphertext and keyword are non-empty. Clean up both the ciphertext and keyword parameters by calling your previously written cleanup_text() function before proceeding. Call your previously written swap() function as needed. Do not call any built-in Python functions except len(), ord(), and range() in your solution. Here are some example arguments for this function and their expected return values: Arguments Return Value ‘LSOTH', 'a') SLOTH' ('sloth', 'aa') SLOTH' ('RLOTPOHWES', 'top') SLOTHPOWER'