Write a Matlab program that converts a string (text) message up to 140 characters long into an encrypted message, and then decrypts the message to return the same string. I dont know where I went wrong, any help?

3 views (last 30 days)
%variables
C='abcdefghijklomnpqrstuvwxyz 1234567890.,!?$%';
%Input
num=0;
while num==0
A=input('input your message to be encrypted, Must be 140 characters or less ','s');
if size(A)<=140;
num=1;
end
end
%display
disp('Your message')
disp(A)
%Encode
for I=0:size(A)
for D=1:size(C)
if A(D)==C(D)
A(I)=D;
break;
end
end
end
E=randi(10,10,140);
E=E*inv(E);
encoded=A*E;
reshape(A,10,140);
%display
disp('Encoded message',encoded)
%Decode
F=inv(E);
decoded=encoded*F;
for J=1:size(decoded)
for(K=1:26)
if decoded(K)==B(K)
decoded(K)=B(K);
break;
end
end
end
disp('Your message has been decoded ',decoded)
  3 Comments
Maryam AlRabee
Maryam AlRabee on 3 Aug 2015
yes! An error that says "Error using reshape To RESHAPE the number of elements must not change.
Error in Encryption (line 26) reshape(A,140,10);"

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 23 Jan 2023
for I=0:size(A)
It is a mistake to use single-parameter size() as a bound for a for loop. When you use size() with only a single parameter, the return value is always a vector with at least two elements -- a list of all of the dimensions. Column vectors do not show up as a single dimension: they show up as something-by-1 .
When you use the : operator with an non-scalar, the : operator uses the first element of the array. So that statement is equivalent to
for I=0:size(A,1)
which is probably not what you want.
if size(A)<=140;
again, size(A) is going to return a vector. You are testing each element of the vector to be sure that it is <= 140. if will consider the result to be true only if all of the test results are non-zero (true). For example if A were an array that is 119 by 83 then the test would succeed as [119, 83]<=140 is [true, true]. Consider using numel() instead.
for I=0:size(A)
for D=1:size(C)
if A(D)==C(D)
A(I)=D;
You are starting I at 0, and using I as an index into A . But 0 is not a permitted index in MATLAB.
The only case that you assign into A is if A(D)==C(D) and then you assign to A(I) . Can that extend A ? You are using a single subscript, which triggers "linear indexing". Assigning to A(I) would extend A to have I elements in the situation that I exceeds the number of elements in A . But I=0:size(A) so I will be at most as large as the number of elements already in A so that assignment will never extend A.
We see then that after the for I loop, that A will be the same size it already was. But it was generally smaller than 140 character.
reshape(A,10,140);
You compute the reshape of A and then you discard it, so the only point of that line is to
  1. Take up some time; and
  2. to give an error message if A does not happen to already have 10*140 = 1400 elements in it. But you would not have accepted A if it had 1400 elements in it; you only accept size(A)<=140

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!