Basic Shift Cipher Decryption Algorithm HELP!

18 ビュー (過去 30 日間)
Jason
Jason 2015 年 1 月 11 日
回答済み: Rahul Gulia 2019 年 7 月 22 日
Hello guys, I'm using matlab to make a function that basically decrypts a shift cipher by taking in the ciphertext string and key integer as parameters and returning the plaintext.
here is the code..
function [ plainText ] = ccdt( c, k )
s = double(c);
for i = 1:numel(s)
s(i) = s(i)-k;
end
plainText = char(s);
return
end
This works fine when the letters don't necessarily need to loop back around to the beginning of the alphabet. For example, if I decrypt the letter 'b' with key = 3, it should give me back 'y', but it's just returning whatever ascii code for 'b' minus 3 which isn't 'y'.
How can I fix this problem? also, how can i modify the code so that lower/ upper case letters don't really matter?
Thanks for your time!
  1 件のコメント
barjas abu matar
barjas abu matar 2017 年 11 月 3 日
i need code basic shift cipher in decryption

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

採用された回答

Mohammad Abouali
Mohammad Abouali 2015 年 1 月 11 日
編集済み: Mohammad Abouali 2015 年 1 月 11 日
Use mode or reminder to loop over certain range of numbers. Something like this:
% A --> 65
% Z --> 90
% a --> 97
% z --> 122
shift=-3;
inputChar=char( [ (65:90)'; (97:122)'] );
for i=1:numel(inputChar)
numericChar=double(inputChar(i));
if ( numericChar>=65 && numericChar<=90 )
numericChar=mod(numericChar-65+shift,26);
outputChar(i,1)=char(numericChar+65);
elseif ( numericChar>=97 && numericChar<=122 )
numericChar=mod(numericChar-97+shift,26);
outputChar(i,1)=char(numericChar+97);
else
error('just alphabetic chars are accepted')
end
end
You can use both negative shift or positive shift, depending on the direction.
  2 件のコメント
Jason
Jason 2015 年 1 月 11 日
ah thanks for this. so basically, the only way to guarantee that the function shifts in between the ascii codes for the lower/upper case alphabet characters is to use multiple if/else to check that they are in between 65:90 and 97:122?
Mohammad Abouali
Mohammad Abouali 2015 年 1 月 11 日
Not really. There are other approaches too.
You can prebuilt a lookup table. Or you can change everything to upper case and cycle there and then restore the caps (lower or upper).
I am sure we can find couple of other methods to get the same output.

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

その他の回答 (2 件)

Aman Gupta
Aman Gupta 2019 年 6 月 27 日
function coded = caesar(x,n)
a = double(x);
a = a+n;
for i = 1:length(a)
while (a(i)>126 || a(i)<32)
if a(i)>126
a(i) = 31 + (a(i) - 126);
elseif a(i)<32
a(i) = 127 - (32 - a(i));
end
end
end
coded = char(a);

Rahul Gulia
Rahul Gulia 2019 年 7 月 22 日
function coded = caesar(str,n)
num1 = double(str); %Converting string to double to make the defined shifts
for i = 1 : length(num1)
if num1(i) + n > 126 % If ASCII value goes beyond 126
m = num1(i)-126+n;
p = 31+m;
num1(i) = p;
elseif num1(i)+n < 32 % If ASCII value goes below 32
m = 32 - num1(i) + n;
p = 126 - m;
num1(i) = p;
else m = num1(i) + n; % In a normal condition
num1(i) = m;
end
code(i) = num1(i);
end
coded = char(code);
% I have written this code. Can anyone please expain as what is wrong in here? I know i have made a mistake. But i am not able to figure it out.
% Thanks in advance.

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by