Made a text encryption program in school, it works for short words but when I fill in complete sentences (or really long words) something goes wrong

So this is the code, each letter will make an array entry of 9 digits, say, I have a word of 5 letters it will make an array with 5 entries that all contain 9 digits, then I make a string of all the digits and if I divide the length of the string by 9, it should give an integer. For short words it actually does, but when I enter longer words something goes wrong. Can someone help me?
clc
a = 815; A = 534;
b = 906; B = 695;
c = 127; C = 317;
d = 913; D = 950;
e = 632; E = 439;
f = 198; F = 382;
g = 278; G = 766;
h = 547; H = 795;
i = 958; I = 187;
j = 965; J = 490;
k = 158; K = 446;
l = 971; L = 646;
m = 485; M = 709;
n = 800; N = 755;
o = 142; O = 276;
p = 422; P = 680;
q = 916; Q = 655;
r = 792; R = 163;
s = 656; S = 119;
t = 849; T = 498;
u = 758; U = 340;
v = 392; V = 585;
w = 171; W = 224;
x = 706; X = 751;
y = 277; Y = 255;
z = 823; Z = 506;
space = 109;
dot = 254;
comma = 736;
exlm = 825;
quest = 235;
at = 199;
dblpnt = 501;
prompt = 'Enter text to encrypt: ';
INPUT = input(prompt,'s');
END=size(INPUT,2);
ARRAY=zeros(1,END);
ARRAY2=zeros(1,END);
for II = 1:END
if INPUT(II) == ' '
LET=space;
elseif INPUT(II) == ','
LET=comma;
elseif INPUT(II) == '.'
LET=dot;
elseif INPUT(II) == '@'
LET=at;
elseif INPUT(II) == ':'
LET=dblpnt;
elseif INPUT(II) == '!'
LET=exlm;
elseif INPUT(II) == '?'
LET=quest;
else
LET=eval(INPUT(II));
end
ARRAY(1,II)=LET;
end
for JJ = 1:END
ARRAY(1,JJ)=ARRAY(1,JJ)+100000000+261785000+JJ*12*23;
end
for KK = 1:END
STR=num2str(ARRAY(1,KK));
if mod(KK,2)==0
EVENSTR=STR([6 2 3 8 1 9 5 4 7]);
ARRAY2(1,KK)=str2num(EVENSTR);
else
ODDSTR=STR([3 5 4 8 1 7 9 6 2]);
ARRAY2(1,KK)=str2num(ODDSTR);
end
end
Encrypted_text='';
FARRAY=flip(ARRAY2);
CIRCD=circshift(FARRAY,[1 floor(END/2)]);
for LL = 1:END
Encrypted_text=([Encrypted_text,num2str(CIRCD(LL))]);
end
clc
fprintf('Your encrypted text:%s\n')
fprintf(2,' %s\n', Encrypted_text)
clear all

1 Comment

Figure out how many letters it can handle before it goes wrong.
I suspect that "num2str" is falling over when "E" or "+" becomes involved.

Sign in to comment.

 Accepted Answer

Thomas - your code (which is neat!) does a number of conversions from numbers to strings and back again to numbers. As you mention in your question, each input letter (to be encrypted) makes up an array entry of nine digits. The code then does some re-ordering of the digits (among other things), and this re-ordering seems to be occurring here
for KK = 1:END
STR=num2str(ARRAY(1,KK));
if mod(KK,2)==0
EVENSTR=STR([6 2 3 8 1 9 5 4 7]);
ARRAY2(1,KK)=str2num(EVENSTR);
else
ODDSTR=STR([3 5 4 8 1 7 9 6 2]);
ARRAY2(1,KK)=str2num(ODDSTR);
end
end
Note how the re-ordering uses the string equivalent of the number, and then str2num is used to create the numeric entry. Now what happens if the new order of the digits is such that a zero is moved to the "front" (left most digit), and we get something like
ODDSTR = '012345678';
When we convert this to a number, we lose the zero
str2num(ODDSTR)
ans =
12345678
and now our number which was originally nine digits long is now eight. So you will need to pad these numbers before creating the encrypted text
for LL = 1:END
numAsText = num2str(CIRCD(LL));
lenText = length(numAsText);
if lenText<9
% length of string is less than nine so prefix with zeros
numAsText = [repmat('0',1,9-lenText) numAsText];
end
Encrypted_text=([Encrypted_text, numAsText]);
end

2 Comments

Thanks alot! This fixed the code I used back then, I am currently working on a way better one, I'll share it somewere on the mathworks site when im finished!
Thanks again! Kind regards,
Thomas
An alternative to using str2num would be to convert the string to a numeric vector of digits:
>> ODDSTR = '012345678';
>> ODDSTR - '0'
ans = [0,1,2,3,4,5,6,7,8]
This will always include leading zeros (even if all digits are zero), without any special-case handling. Of course this would require a few other changes to the code (for example ARRAY and ARRAY2 as matrices), but is worth considering...

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Products

Asked:

on 8 Oct 2014

Commented:

on 9 Oct 2014

Community Treasure Hunt

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

Start Hunting!