We would like to encrypt a message using a 3x3 matrix we w call our \"cipher 7 2
ID: 3282127 • Letter: W
Question
We would like to encrypt a message using a 3x3 matrix we w call our "cipher 7 2 1 C0 3-1 3 42 Choose a message (string) to encrypt. Be sure the number of characters (not including spaces) in your string is a multiple of 3. HINT: To input a string of characters into MATLAB use single quotes my_string - thisismystring' Ignore all spaces. Keep the same case for each letter in the string. - Let each letter correspond to its numerical representation (e.g. A-1, B-2, Y-25, » Create a matrix of numbers 0-25, D, representing your message and encrypt it by applying the cipher as a transformation to the matrix. DO NOT DO THIS STEP BY HAND! (Pay attention to dimension!) HINT: Create a string reprcsenting all possiblo characters in your alphabet encrypted matrix- cipher *original matrix mod 26 Output the encrypted string to the command window. DO NOT DO THIS STEP BY HAND!Explanation / Answer
clear all;
close all;
%given ciphermatrix
C = [7 2 1;0 3 -1;-3 4 -2];
%string of length multiple of 3
my_string = 'helloguys';
n = size(double(my_string),2)/size(C,1);
%taking a/A = 0,b/B = 1.....ans so on z/Z = 25;
X = double(my_string) - double('a');
A = zeros(3,n);
k=1;
i=1;
%writing the string into matrix form
while i <= numel(X)
j=1;
while(mod(i,3) ~= 0)
A(j,k) = X(i);
i = i+1;
j = j+1;
end
A(j,k) = X(i);
k = k+1;
i = i+1;
end
encrypted_matrix = C*A;
encrypted_matrix = mod(encrypted_matrix,26);
fprintf('The word to be encrypted is: %s: ',my_string);
fprintf('the word after encryption is : ');
%printing the encrypted string
for i = 1:n
for j = 1:3
fprintf(char(encrypted_matrix(j,i) + 'a'));
end
end
fprintf(' ');
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.