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

Matlab Question In cryptography, a Caesar cipher is one of the simplest and most

ID: 3682701 • Letter: M

Question

Matlab Question

In cryptography, a Caesar cipher is one of the simplest and most widely known encryption techniques. It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. For example, with a right shift of 3, A would be replaced by D, B would become E, and so on. Letters that would get shifted out of the alphabet are wrapped around to the beginning, e.g. Z becomes C. The method is named after Julius Caesar, who used it in his private correspondence.2 A string is an array of characters. Each character is represented by an integer. The number that represents the character "A" is 65, the number for "B" is 66, and so on. You can convert a string to an array of numbers (or a single character to a single number) by using the abs function or by performing an arithmetic operation involving a number, e.g. 'A7 +3=68. You can convert an array of numbers to a string (or a single number to a single character) using the char function. The mod function can be used to wrap numbers around to zero when they become too large Write a script (task3.m) that prompts the user for a string to encrypt and the key (shift value) and outputs the Caesar cipher encryption of the given string using the given key. The key may be positive (right shift) or negative (left shift). Your script should force the input to be uppercase and you should delete spaces and punctuation. You may not use loops.

Explanation / Answer

I have written a simple function in matlab for the same:

prompt='please enter the key value';
key=input(prompt)
inputChar=char( [ (65:90)'; (97:122)'] );
for i=1:numel(inputChar)
numericChar=double(inputChar(i));
if ( numericChar>=65 && numericChar<=90 )
numericChar=mod(numericChar-65+key,26);
output(i,1)=char(numericChar+65);
elseif ( numericChar>=97 && numericChar<=122 )
numericChar=mod(numericChar-97+key,26);
output(i,1)=char(numericChar+97);
else
error('just alphabetic chars are accepted')
end
end