Caesar cipher matlab code

17 ビュー (過去 30 日間)
Md. Atiqur Rahman
Md. Atiqur Rahman 2022 年 8 月 25 日
コメント済み: Walter Roberson 2022 年 8 月 31 日
Can anyone write a very basic code for caesar cipher that shifts input characters by 5 place?
  1 件のコメント
James Tursa
James Tursa 2022 年 8 月 25 日
What have you done so far? What specific problems are you having with your code?

サインインしてコメントする。

採用された回答

Md. Atiqur Rahman
Md. Atiqur Rahman 2022 年 8 月 27 日
編集済み: Walter Roberson 2022 年 8 月 28 日
%% 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 件のコメント
Md. Atiqur Rahman
Md. Atiqur Rahman 2022 年 8 月 31 日
I think I've done it better 😎
Walter Roberson
Walter Roberson 2022 年 8 月 31 日
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?

サインインしてコメントする。

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2022 年 8 月 25 日
cc5('HELLO')
ans = 'MJQQT'
cc5('ABCXYZ')
ans = 'FGHCDE'
function out = cc5(in)
TT('A':'Z') = ['F':'Z', 'A':'E'];
out = TT(in);
end
  2 件のコメント
Md. Atiqur Rahman
Md. Atiqur Rahman 2022 年 8 月 27 日
編集済み: Walter Roberson 2022 年 8 月 27 日
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 2022 年 8 月 27 日
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeEncryption / Cryptography についてさらに検索

タグ

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by