📄 enigma.m
字号:
function enigma(plugboard,rrSettings,message)
% plugboard is the string of plugboard settings
% rrSettings refers to rotor and reflector settings
% message refers to the message the user typed
% all are given as strings
% the following are for handling errors
% show an error message and open up the gui window again
if length(rrSettings) < 4
rotors
figure('Position',[285,200,270,160],'MenuBar','none', 'Name', 'Error!','NumberTitle','off');
uicontrol('Style','text','String',...
'Please select THREE rotors AND ONE reflector',...
'Position',[5 5 260 150],'BackgroundColor','white',...
'HorizontalAlignment', 'left','TooltipString', 'Your encrypted message is displayed here',...
'FontSize', 13)
return
elseif length(message)==0
rotors
figure('Position',[285,200,270,160],'MenuBar','none', 'Name', 'Error!','NumberTitle','off');
uicontrol('Style','text','String',...
'You REALLY need to enter a message for any decoding to occur!',...
'Position',[5 5 260 150],'BackgroundColor','white',...
'HorizontalAlignment', 'left','TooltipString', 'Your encrypted message is displayed here',...
'FontSize', 13)
return
elseif rem((length(plugboard)),2) ~=0
rotors
figure('Position',[285,200,270,160],'MenuBar','none', 'Name', 'Error!','NumberTitle','off');
uicontrol('Style','text','String',...
'The plugboard needs an even number of letters because each letter is matched with its adjacent. Please re-enter your data!',...
'Position',[5 5 260 150],'BackgroundColor','white',...
'HorizontalAlignment', 'left','TooltipString', 'Your encrypted message is displayed here',...
'FontSize', 13)
return
end
%***********************************************************************
% handle backspaces in user's input
newmessage=[];
nummessage=double(message);
for i=1:length(nummessage)
if nummessage(i)~=32 %32 is the ascci code for the spacebar
newmessage=[newmessage, nummessage(i)];
end
end
%***********************************************************************
%below i am defining all the rotors
r1= ['E' 'K' 'M' 'F' 'L' 'G' 'D' 'Q' 'V' 'Z' 'N' 'T' 'O' 'W' 'Y' ...
'H' 'X' 'U' 'S' 'P' 'A' 'I' 'B' 'R' 'C' 'J'];
r2=['A' 'J' 'D' 'K' 'S' 'I' 'R' 'U' 'X' 'B' 'L' 'H' 'W' 'T' 'M'...
'C' 'Q' 'G' 'Z' 'N' 'P' 'Y' 'F' 'V' 'O' 'E'];
r3=['B' 'D' 'F' 'H' 'J' 'L' 'C' 'P' 'R' 'T' 'X' 'V' 'Z' 'N' ...
'Y' 'E' 'I' 'W' 'G' 'A' 'K' 'M' 'U' 'S' 'Q' 'O'];
r4=['E' 'S' 'O' 'V' 'P' 'Z' 'J' 'A' 'Y' 'Q' 'U' 'I' 'R' ...
'H' 'X' 'L' 'N' 'F' 'T' 'G' 'K' 'D' 'C' 'M' 'W' 'B'];
r5=['V' 'Z' 'B' 'R' 'G' 'I' 'T' 'Y' 'U' 'P' 'S' 'D' 'N' 'H' ...
'L' 'X' 'A' 'W' 'M' 'J' 'Q' 'O' 'F' 'E' 'C' 'K'];
r6=['J' 'P' 'G' 'V' 'O' 'U' 'M' 'F' 'Y' 'Q' 'B' 'E' 'N'...
'H' 'Z' 'R' 'D' 'K' 'A' 'S' 'X' 'L' 'I' 'C' 'T' 'W'];
r7=['N' 'Z' 'J' 'H' 'G' 'R' 'C' 'X' 'M' 'Y' 'S' 'W' 'B'...
'O' 'U' 'F' 'A' 'I' 'V' 'L' 'P' 'E' 'K' 'Q' 'D' 'T'];
r8= ['F' 'K' 'Q' 'H' 'T' 'L' 'X' 'O' 'C' 'B' 'J' 'S'...
'P' 'D' 'Z' 'R' 'A' 'M' 'E' 'W' 'N' 'I' 'U' 'Y' 'G' 'V'];
% and the keyboard we will use to index the input given by the user
kbrd=['A' 'B' 'C' 'D' 'E' 'F' 'G' 'H' 'I' 'J' 'K' 'L' 'M' 'N' 'O' 'P' ...
'Q' 'R' 'S' 'T' 'U' 'V' 'W' 'X' 'Y' 'Z'];
%***********************************************************************
% this section handles the reflector chosen by the user
if rrSettings(end)=='b' % settings of reflector b
reflector= ['A' 'Y' 'B' 'R' 'C' 'U' 'D' 'H' 'E' 'Q' 'F' 'S'...
'G' 'L' 'I' 'P' 'J' 'X' 'K' 'N' 'M' 'O' 'T' 'Z' 'V' 'W'];
else % settings of reflector c
reflector= ['A' 'F' 'B' 'V' 'C' 'P' 'D' 'J' 'E' 'I' 'G' 'O' 'H' 'Y' 'K' 'R'...
'L' 'Z' 'M' 'X' 'N' 'W' 'T' 'Q' 'S' 'U'];
end
%***********************************************************************
% this section handles the rotors
rotors=[{r1} {r1} {r1}]; % can assign whatever we want to rotors for now
% we just need 3 places to put the 3 rotors
% compare the radiobutton choice given as a string to the number of rotors
% if the match, we know what rotor the user picked
% put the rotor data in the cell array rotors
% do this three times for all rotor choices.
for i=1:3
if str2num(rrSettings(i))==1
rotors{i}=r1;
elseif str2num(rrSettings(i))==2
rotors{i}=r2;
elseif str2num(rrSettings(i))==3
rotors{i}=r3;
elseif str2num(rrSettings(i))==4
rotors{i}=r4;
elseif str2num(rrSettings(i))==5
rotors{i}=r5;
elseif str2num(rrSettings(i))==6
rotors{i}=r6;
elseif str2num(rrSettings(i))==7
rotors{i}=r7;
else rotors{i}=r8;
end
end
%do the assignment
crotor1=rotors(1);
crotor2=rotors(2);
crotor3=rotors(3);
%turn them into vectors - makes the manipulation easier
rotor1=crotor1{1};
rotor2=crotor2{1};
rotor3=crotor3{1};
% now we have the rotors chosen by the user
%***********************************************************************
% this section applies the plugboard transformation
if length(plugboard)~=0
for k=1:2:length(plugboard)
for i=1:length(newmessage)
if plugboard(k)==newmessage(i)
newmessage(i)=plugboard(k+1);
end
end
end
end
% now the message given by the user has gone through the plugboard and
% has been adjusted accordingly
%***********************************************************************
% we are now ready to apply the rotor and reflector transformations
% the following are the transformations through the rotors and through the reflector
% the second set of transformations when the characters pass through the rotors the
% second time will be taken care of in the next section
% counters below are set to move the rotors
counter1=0;
counter2=0;
counter3=0;
%first convert newmessage into characters and provide space for the final encoding
newmessage=char(newmessage);
semiencoded=[];
for i=1:length(newmessage) % do the stuff that follows for all letters
% apply transformation from rotor 1
for k=1:length(kbrd)
if newmessage(i)==kbrd(k)
rotor1=[rotor1(2:end), rotor1(end-((length(rotor1))-1))]; %shift rotor by one place
semiencoded(i)=rotor1(k);
break
end
end
% apply transformation from rotor 2
for j=1:length(kbrd)
if semiencoded(i)==kbrd(j)
% also check if counter==26, if so move position of rotor2
%and increment counter2 by 1
if counter1==26
counter2=counter2 + 1;
elseif counter2==27
counter2= 1; % initialize it
rotor2=[rotor2(2:end), rotor2(end-((length(rotor2))-1))]; % shift rotor by one place
end
semiencoded(i)=rotor2(j);
break
end
end
% apply transformation from rotor 3
for f=1:length(kbrd)
if semiencoded(i)==kbrd(f) % also check counter 2 and if>26
if counter2==26
counter3=counter3 + 1;
elseif counter2==27
counter3= 1; % initialize it
rotor3=[rotor3(2:end), rotor3(end-((length(rotor3))-1))]; % shift rotor by one place
end
semiencoded(i)=rotor3(f);
break
end
end
% apply transformation from the reflector
for m=1:length(reflector)
if semiencoded(i)==reflector(m) % if we find the letter
even=[2 4 6 8 10 12 14 16 18 20 22 24 26];
if sum(m == even) > 0
% and the letter is in an odd place, then we substitute with the
% letter that comes after it.
index=m-1;
semiencoded(i)=reflector(index);
break
else
% otherwise we substitute with the letter that comes before it
index=m+1;
semiencoded(i)=reflector(index);
break
end
end
end
% apply transformation going back through rotors- rotor3
for k=1:length(rotor3)
if semiencoded(i)==rotor3(k)
semiencoded(i)=kbrd(k);
break
end
end
% apply transformation going back through rotors- rotor2
for j=1:length(rotor2)
if semiencoded(i)==rotor2(j)
semiencoded(i)=kbrd(j);
break
end
end
% apply transformation going back through rotors- rotor1
for f=1:length(rotor1)
if semiencoded(i)==rotor1(f)
semiencoded(i)=kbrd(f);
break
end
end
end
%***********************************************************************
msg=char(semiencoded);
figure('Position',[65,200,700,80],'MenuBar','none', 'Name', 'Enigma: The encoded message','NumberTitle','off');
uicontrol('Style','text','String',msg,...
'Position',[5 5 690 70],'BackgroundColor','white',...
'HorizontalAlignment', 'left','TooltipString', 'Enigma: The ciphertext depending on the input you gave',...
'FontSize', 12)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -