Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please use Matlab One of the most important tasks modern computers perform is cr

ID: 2080711 • Letter: P

Question

Please use Matlab

One of the most important tasks modern computers perform is cryptography. Strong encryption lets us keep our network passwords private, our commercial and financial transactions on the Internet secure, and our personal information safe from prying eyes. Computers are well-suited to the complex, repetitive mathematical operations that make modern cryptosystems effective. In fact, the immediate ancestors of modern computers were invented by the Allies in World War II to crack the codes used by the Axis powers. Those codes, like the German Enigma, were implemented with mechanical encryption systems, like gears and rotors; they were quickly broken by the new generation of digital electronic machines. But methods of encrypting and obscuring text have been in use for thousands of years. One of the oldest is called the Caesar cipher, after Julius Caesar, who used it to communicate with his political and military allies. The cipher is very simple: choose a secret (integer) number (called a key). Write out your message in all capital letters, with no spaces or punctuation. Then, shift" each character through the alphabet by the same number of spots as your secret key (rightward if the key is positive, leftward if the key is negative). To decrypt the message, shift each character leftward by the key number. If, while going in either direction, you reach the end of the alphabet, wrap around and continue at the other end So, for example, if we were using the Caesar cipher a key of 10, we would change the letters of the alphabet like so: Plaintext: A B C DE F G H I J K L M N 0 P Q R ST U V W X Y Z Cipher text: K L M N 0 P Q R S T U V W X Y Z ABC D E F G H I J (In cryptography, plaintext is the term for the original message; ciphertext is the term for the encrypted message.) By using this cipher, the plaintext message HAILCAESAR would become RKSVMKOCKB once we had shifted each character right by 10 places This mechanism shifting the letters and wrapping around the ends of the list is analogous to the mathematical operation modulo division, which divides one integer by another and returns the remainder. We can use the modulo operation to implement the Caesar cipher by imagining that each letter is actually a number, starting at 1 for A and ending with 26 for Z. Then, the operation can be implemented in MATLAB by using the command on two variables containing scalar integers: plaintext R 18 'R' is the eighteenth Letter of the alphabet key 10 cipher R modCplaintextR key, 26) This code applies the Caesar cipher to the letter R to get that Bis the corresponding encoded letter (since 18+10 28 and the remainder from doing 28+26 is 2). In other words, the modulo division by 26 takes care of wrapping the sum of the letter and shift (see the information about the function for mod an important note about mapping results of the function to the 26 letters of the alphabet).

Explanation / Answer

The matlab code for the afore said problem statement is as below. Please feel free to reach us for any more clarifications.

Note: The statement followed by '%' symbol is a commemt for better understanding that discribes the respective line of the code. For better view of the below code, copy the entire code and paste it on a matlab editor and compile it.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%Main Function%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function main

    msg = input('Enter message to encrypt:', 's'); %Taking the data to be encrypted as input, which should contain E as the most frequent letter according to the problem statement

    key = input('Enter key:');                     %Taking numerical key as input

    %%validating Key%%

    %Checking if key is a Scalar

    if isscalar(key)                               %isscalar(key) determines if key is a scalar and retruns 1 if it is true

    else

        display('Key is not a scalar');

        return;                                    %Terminates the code

    end

    %Checking if key is an integer

    if round(key)==key                             %round(key) rounds the given key to its nearest integer

    else

        display('Key is not an integer');

        return;                                    %Terminates the code

    end

    %%Displaying the encrypted messgae

    encryptedMsg = encrypt(msg,key);              %Calling the encrypt function by passing message and key as arguments

    disp('Encrypted message:');

    disp(encryptedMsg);                           %Displays encrypted message

    %%Decryption

    encryptedMsg = input('Enter message to encrypt:', 's'); %Taking the data to be decrypted as input

    decryptedMsg = decrypt(encryptedMsg);         %Calling the decrypt function by passing encrypted message as argument

    display('Decrypted message:');

    display(decryptedMsg);                        %Displays encrypted message

end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%Encrypt Function%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function data = encrypt(msg,key)                  %encrypt function declaration and definition

msgUp = upper(msg);                               %Converting message to upper case   

L = length(msg);                                  %gets lenght of the message to a variable 'L'

for i = 1:L

    CH = double(msgUp(i));                        %gets ASCII value of the each upper case alphabet of the message to a variable 'CH'

    CH = CH-64;                                   %Substracting 64 from ASCII value of the each upper case alphabet gives actual number of the alphabet

    cipherMsg = mod(CH+key, 26);                  %Applying the cypher encryption logic. The variable cipherMsg gets the number of the encrypted character

    if cipherMsg==0                               %Condition when mod operation in the above step returns 0 as described in the problem statement

        cipherMsg = 26;

    end

    cipherMessage(i)=[char(cipherMsg+64)];        %cipherMsg+64 gives the ASCII value of the encrypeted character. Where as [char(cipherMsg+64)] returns the corresponding upper case letter

end

data = cipherMessage;                             %gets the entire encrypted message to a variable 'data'

end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%Decrypt Function%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function data = decrypt(msg)                      %decrypt function declaration and definition

msgUp = upper(msg);                               %Converting message to upper case

L = length(msg);                                  %gets lenght of the message to a variable 'L'

for i = 1:L

   CH(i) = double(msgUp(i));                      %gets ASCII value of the each upper case alphabet of the message to a variable 'CH'

end

tbl = tabulate(CH);                               %tabulate(CH) gets the frequency of occurence of each alphabet in the encrypted message to a variable vector 'tbl'

[~,a] = max(tbl(:,2));                            %Gets the ASCII value of most frequent variable to a variable 'a'

key = a-69;                                       %Finding key by substracting the ASCII value of letter E from most frequent ASCII value as suggested in the problem statement

%Calling encrypt function by passing the message to be decrypted and negative of the key as arguments. Negative of the key as input to encrypt function performs decryption

data = encrypt(msgUp,-key);                       %gets the entire decrypted message to a variable 'data'

end