Can anyone write a very basic code for caesar cipher that shifts input characters by 5 place?

1 Kommentar

James Tursa
James Tursa am 25 Aug. 2022
What have you done so far? What specific problems are you having with your code?

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Md. Atiqur Rahman
Md. Atiqur Rahman am 27 Aug. 2022
Bearbeitet: Walter Roberson am 28 Aug. 2022

0 Stimmen

%% To whom it may concern
Plaintext= input('Plaintext: ','s')
Shift= input('Shift: ')
Output = Plaintext+Shift;
r=Output-26;
for i = 1:length(Plaintext)
if Output(i) <= 90
Output(i) = char(Output(i));
else
Output(i) = char(r(i));
end
end
fprintf('Ciphertext: ','s')
disp(char(Output))

10 Kommentare

Let's see:
Plaintext = 'To whom it may concern';
O1 = CC(Plaintext, 5);
[Plaintext;
O1;
char(Plaintext+5)]
ans = 3×22 char array
'To whom it may concern' 'YZ%bSZX%T_%XLd%NZYNP]Y' 'Yt%|mtr%ny%rf~%htshjws'
CC(O1, -5)
ans = 'TU CNUS OZ SGE IUTIKXT'
CC(Plaintext, 96)
ans = 'µf½®µ³f¯ºf³§¿f©µ´©«¸´'
CC(Plaintext, 5120)
ans = 'ᐺᑕᐆᑝᑎᑕᑓᐆᑏᑚᐆᑓᑇᑟᐆᑉᑕᑔᑉᑋᑘᑔ'
function Output = CC(Plaintext, Shift)
Output = Plaintext+Shift;
r=Output-26;
for i = 1:length(Plaintext)
if Output(i) <= 90
Output(i) = char(Output(i));
else
Output(i) = char(r(i));
end
end
Output = char(Output);
end
With a shift of 5, you move 'T' to 'Y' and you move 'o' to 'Z'. But 'Y' and 'Z' are adjacent to each other, so in order for them to appear together as output, the two input characters would have had to be adjacent to each other in the alphabet.
You can see that when you shift your first output back, you do not get the original input.
You can see that you are not handling large shifts properly.
Md. Atiqur Rahman
Md. Atiqur Rahman am 28 Aug. 2022
According to my code U appears as Z and T appears as Y for a shift of 5 places in both cases. Besides, caesar cipher limited upto 25 place shift. I think this works fine for any shift (<25) if you try again. Now, if I need to shift small letters then i can do this by inserting one more 'if' statement. Hope this explanation helps you.
"To whom it may concern" is the input suggested by your own comment. Considering there is no documentation in your code that would limit it to upper case, it would be expected that the code should work for your suggested input.
Notice that with the shift of +5, that letters at the end of the upper-case range are not reversed with a shift of -5
Plaintext = upper('To whom it may concern')
Plaintext = 'TO WHOM IT MAY CONCERN'
O1 = CC(Plaintext, 5);
[Plaintext;
O1;
char(Plaintext+5)]
ans = 3×22 char array
'TO WHOM IT MAY CONCERN' 'YT%BMTR%NY%RFD%HTSHJWS' 'YT%\MTR%NY%RF^%HTSHJWS'
CC(O1, -5)
ans = 'TO =HOM IT MA? CONCERN'
O2 = CC(Plaintext, -24);
[Plaintext;
O2;
char(Plaintext-24)]
ans = 3×22 char array
'TO WHOM IT MAY CONCERN' '<?0715)+76+-:6' '<?0715)+76+-:6'
CC(O2, 24)
ans = 'TO WHOM IT MAY CONCERN'
function Output = CC(Plaintext, Shift)
Output = Plaintext+Shift;
r=Output-26;
for i = 1:length(Plaintext)
if Output(i) <= 90
Output(i) = char(Output(i));
else
Output(i) = char(r(i));
end
end
Output = char(Output);
end
Md. Atiqur Rahman
Md. Atiqur Rahman am 28 Aug. 2022
Bearbeitet: Walter Roberson am 28 Aug. 2022
%%Decryption
Ciphertext= input('Ciphertext: ','s')
Shift= input('Shift: ')
Output = Ciphertext-Shift
r=Output+26;
for i = 1:length(Ciphertext)
if Output(i) >= 65
Output(i) = char(Output(i));
else
Output(i) = char(r(i));
end
end
fprintf('Plaintext: ','s')
disp(char(Output))
No, notice how the spaces do not map back to spaces when you reverse the shift.
Plaintext = ('To whom it may concern')
Plaintext = 'To whom it may concern'
O1 = CC(Plaintext, 5);
[Plaintext;
O1;
char(Plaintext-5)]
ans = 3×22 char array
'To whom it may concern' 'Oj5rcjh5do5h\t5^ji^`mi' 'Ojrcjhdoh\t^ji^`mi'
CC(O1, -5)
ans = 'ToTwhomTitTmayTconcern'
O2 = CC(Plaintext, -24);
[Plaintext;
O2;
char(Plaintext+24)]
ans = 3×22 char array
'To whom it may concern' 'lRRRyR{{}' 'l888y8{{}'
CC(O2, 24)
ans = 'ToTwhomTitTmayTconcern'
function Output = CC(Ciphertext, Shift)
Output = Ciphertext-Shift;
r=Output+26;
for i = 1:length(Ciphertext)
if Output(i) >= 65
Output(i) = char(Output(i));
else
Output(i) = char(r(i));
end
end
Output = char(Output);
end
Md. Atiqur Rahman
Md. Atiqur Rahman am 28 Aug. 2022
Spaces are not my concern as long message can be extracted. If you have a better solution post it here. which must work upto 25 shifts.
Thanks for constructive criticism.
Md. Atiqur Rahman
Md. Atiqur Rahman am 31 Aug. 2022
% Caesar cipher Encryption
Plaintext= input('Plaintext: ','s');
Shift= input('Shift: ');
Output = Plaintext+Shift;
r=Output-26;
for i = 1:length(Plaintext)
if Output(i) <= 90
Output(i) = char(Output(i));
Output(Output<=58)=32;
else
Output(i) = char(r(i));
end
end
fprintf('Ciphertext: ','s');
disp(char(Output))
%Caesar cipher Decryption
Ciphertext= input('Ciphertext: ','s');
Shift= input('Shift: ');
Output = Ciphertext-Shift;
r=Output+26;
for i = 1:length(Ciphertext)
if Output(i) >= 65
Output(i) = char(Output(i));
else
Output(i) = char(r(i));
Output(Output<=58)=32;
end
end
fprintf('Plaintext: ','s');
disp(char(Output))
Md. Atiqur Rahman
Md. Atiqur Rahman am 31 Aug. 2022
I think I've done it better 😎
Output(i) = char(Output(i));
Why are you doing that redundant operation? Yes, you convert to char but you store it in the same array so it would convert back to the same data type.
Output(Output<=58)=32;
Why are you mixing logical indexing of the entire array inside a loop?

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Walter Roberson
Walter Roberson am 25 Aug. 2022

0 Stimmen

cc5('HELLO')
ans = 'MJQQT'
cc5('ABCXYZ')
ans = 'FGHCDE'
function out = cc5(in)
TT('A':'Z') = ['F':'Z', 'A':'E'];
out = TT(in);
end

2 Kommentare

Md. Atiqur Rahman
Md. Atiqur Rahman am 27 Aug. 2022
Bearbeitet: Walter Roberson am 27 Aug. 2022
Plaintext= input('Plaintext: ','s')
Shift= input('Shift: ')
if Plaintext+Shift <= 90
Ciphertext = (char(Plaintext+Shift))
else
Ciphertext = (char(Plaintext+Shift-26))
end
%Can you fix this?
Walter Roberson
Walter Roberson am 27 Aug. 2022
Your Plaintext is a vector of characters. Plaintext+Shift <= 90 would be a vector. When you if a vector, the result is considered true only if all of the values in the vector are non-zero. So your Plaintext+Shift <= 90 test would be true only if all of the characters where no more than 'U'
You should investigate logical indexing.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Encryption / Cryptography finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by