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
Show older comments
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
Iain
on 8 Oct 2014
Figure out how many letters it can handle before it goes wrong.
I suspect that "num2str" is falling over when "E" or "+" becomes involved.
Accepted Answer
More Answers (0)
Categories
Find more on Programming in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!