Homework-Permutation Cipher A permutation cipher is an encryption that uses any permutation of the alphabet to encrypt a message. It resists brute-force attacks (trying all possible keys) because...

1 answer below »
i have uploaded the question file would it be possible it can be completed?,it needs to be in matlab code


Homework-Permutation Cipher A permutation cipher is an encryption that uses any permutation of the alphabet to encrypt a message. It resists brute-force attacks (trying all possible keys) because there are 26! keys. This is rather a lot: 26! = 403291461126605635584000000 The weakness of permutation ciphers is that they encrypt each letter individually, and uniformly. This means we can use patterns in natural language to guess what the key is. We will first build a class to store keys, and the functions we will use with them. A key is any permutation of the alphabet. We will store a key internally as a permutation of the numbers 1 to 26, in a row vector of length 26, but make the display function such that it shows the corresponding letters. The identity permutation, the alphabet, is stored as the vector 1:26, and you can generate a random permutation with randperm(26). Build a class PermutationKey that stores and handles permutation keys. It should have the following: Part 1 Properties: • A property perm that stores a key as a permutation on the numbers 1 to 26. It should not store it as a string of letters. Methods: · A constructor method that takes a permutation p of the numbers 1 to 26 and creates a PermutationKey storing that permutation as its perm property. · A display method that shows a PermutationKey as a permutation of the alphabet. · A method mtimes(l,m) that takes two keys and gives their composition: (l ◦ m) (i) = l (m (i)) . Using the name mtimes means you can write l * m for l ◦ m . • · A method invertion(K) that gives the inverse of the key K. For every number x between 1 and 26, if K.perm(x) = y then K.invertion.perm(y) = x. · • A method encryption(k,m) that encrypts a message (a character vector) m with the key k. First, turn the message into all uppercase. Then apply the key to each uppercase letter in the message, leaving other characters (spaces, punctuation, underscores, etc.) as is. Make sure to convert from the ASCII indices (65–90) of the alphabet to the permutation indices (1–26) and back. · A method decryption(k,m) that decrypts a message m with the key k, by encrypting with the inverse key. · All these methods should be implemented in the same file PermutationKey.m · Make sure you think about what happens when a user tries to pass in the wrong data type. Part 2 A frequency attack works as follows. Suppose our ciphertext has these letter counts: Sorting this by occurrence count (from small to large), we get: We can compare this against the frequency of letters in English (from rare to common): This tells us that a likely key used to encrypt the ciphertext message is, starting from the most frequent letter, the one taking E → J, T → X, A → O, etc. To help you implement this, you are provided on Moodle with a function permutation that, given the letter counts (the first line above), returns the matching permutation of 1 to 26 (second line above). We will build a class to try and decrypt a permutation-cipher encoded message. Write a class Attack with the following properties and methods. Properties: • A property ciphertext that stores the encrypted message. • A property key that stores a PermutationKey object. Methods: • A constructor method that takes an encrypted message as input, stores it as the ciphertext property, and initializes the key with the identity permutation 1:26. • A display method that shows the stored key and the first 300 characters of the message, decrypted with the current key. The message is probably too long to display in its entirety (short messages are hard to decode), and displaying it (partially) decrypted shows the progress made thus far in breaking the cipher. Format it nicely, indicating the key and the message (you can use char(13) to create a line break). • A method lettercount that counts the occurrences of each letter of the alphabet in the ciphertext, and returns them as a 1×26 array. Make sure to pre-allocate the return array. You should not use built-in counting functions such as find or histcount. • A method attack that carries out a frequency attack: 1. allocate an array with English letter frequencies (see above); 2. use lettercount to retrieve the letter frequency in the cyphertext; 3. use the permutation function to sort the alphabet by (reverse) letter count; 4. combine 1. and 3. in the way described above to guess a key. Your method should return the new Attack object, with the same ciphertext but the new key. You can approach 4. above in two ways: by directly computing the new key (easiest), or by using the invertion and composition (*) methods of the PermutationKey class (slightly harder but very satisfying). All methods should be implemented in the same file Attack.m. Note: it is permitted, but not recommended, to make Attack a handle class. If you do, your code may behave differently from the examples at the end of this file. You should still make sure that all your functions give a return value (except the display method). Part 3: As was to be expected, the letter frequency of our ciphertext is not exactly that of English in general. The key generated by our frequency attack is off, and the message is still very much illegible. But at the same time, several letters are spot on: in particular the word “all” is complete. We will finish the job by creating a method sample to inspect a random sample of the current decoding in search of familiar words, and a method swap to swap two letters in the key. · Add a method sample to your Attack class that displays a random piece of the ciphertext, decrypted with the current key, of 300 characters in length. Make sure that the sample text is not cut short by reaching the end of the ciphertext. For efficiency, first select the sample and then decrypt it, instead of the other way around (which would decrypt the whole ciphertext). Use the Matlab function randi(n) to generate a random integer between 1 and n (inclusive). • Add a method swap to your PermutationKey class to swap two letters in the key. The function should take a key and two characters as input, and swap the two characters on the input side of the key. That is because we’re using the key to decrypt the message, and we want to swap two letters in the decrypted ciphertext to create recognizable words. So, if k is the key given by the first permutation below, swapping H and P, swap(k,’H’,’P’), should give the second key below. You may compute the resulting key directly, or by giving the permutation that swaps the two given letters and then composing with the original key using (*). · Add a corresponding method swap to your Attack class, that swaps two letters in the key property by calling the swap method of the PermutationKey class. Part 4: To wrap up, we will polish our functions and add some convenient functionality. One thing we’ll add is undo functionality for the swap function, using a list to store previous swaps. A List class is included with the files you were given. • Edit the constructor for the PermutationKey class so that, in addition to the current functionality, it accepts a character array as input, and calling PermutationKey without arguments creates a random permutation key. • Add a property past to your Attack class, and let the constructor initialize it with the empty list. • Edit the swap method of the Attack class so that it stores appropriate “undo” information in the past property. Edit the attack method so that it erases any “undo” information. • Add an undo method that reverts the previous swap, or if there is no undo information, writes an appropriate message and does nothing. • Use the tools you have built to decrypt the secret message in ciphertext.m. Put the key you’ve found in a script solution.m as the variable theKey. It should be an object of the PermutationKey class, and it should be the only variable declared in the script. The list class Matlab Running code: your code when completed should work as follows
Answered Same DayMar 22, 2021

Answer To: Homework-Permutation Cipher A permutation cipher is an encryption that uses any permutation of the...

Abr Writing answered on Mar 28 2021
150 Votes
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here