필터 지우기
필터 지우기

Caesar's cypher is the simplest encryption algorithm. It adds a fixed value to the ASCII (unicode) value of each character of a text. In other words, it shifts the characters. Decrypting a text is simply shifting it back by the same amount, that is,

조회 수: 46 (최근 30일)
function coded = caesar(a,b)
coded = char(a+b);
c = length(coded);
for ii = 1:c
if coded(ii) > 126;
d = coded(ii) - 126;
coded(ii) = (32+d);
elseif coded(ii)<32;
d = coded(ii) - 32;
coded(ii) = char(126-d);
end
end
end
  댓글 수: 3
David Hill
David Hill 2020년 4월 13일
What is a? Isn't 'a' already a character array like: 'my name is Dave'? Is b the shift number?
muhammad usman
muhammad usman 2020년 4월 13일
a is the vector input and b is shift number.
i have modified function but still not accurate
function coded = caesar(a,b)
while b>126 % b must be 126 or less so that only 1 cycle for each number
b= b-1;
end
code = a+b;
c = length(code);
for ii = 1:c
if code(ii) > 126;
d = code(ii) - 126;
code(ii) = (31+d);
elseif code(ii)<32;
d = code(ii) - 32;
code(ii) = (127+d);
end
end
coded = char(code);
end

댓글을 달려면 로그인하십시오.

답변 (5개)

Walter Roberson
Walter Roberson 2020년 4월 13일
You start by doing
coded = char(a+b);
Suppose b is negative enough that a+b would be less than zero. Because of the char() that would be char() of a negative value and because char cannot be negative that would be changed to 0.
You need to do your work in signed integer or in floating-point and convert to char afterwards
  댓글 수: 2
muhammad usman
muhammad usman 2020년 4월 13일
i have modified function and included your sugestion. but still not working
function coded = caesar(a,b)
while b>126 % b must be 126 or less so that only 1 cycle for each number
b= b-1;
end
code = a+b;
c = length(code);
for ii = 1:c
if code(ii) > 126;
d = code(ii) - 126;
code(ii) = (31+d);
elseif code(ii)<32;
d = code(ii) - 32;
code(ii) = (127+d);
end
end
coded = char(code);
end
Walter Roberson
Walter Roberson 2020년 4월 13일
if b were 256 then that should probably be the same outcome as if it were 0, but you treat it as 126. You also do not raise negatives into the positive range at that stage.

댓글을 달려면 로그인하십시오.


David Hill
David Hill 2020년 4월 13일
Are you using the full range from 0-255 for ascii? or do you want the output to have only letters (a-zA-Z) and no special characters?
function coded = caesar(a,b)
coded=char(mod(unicode2native(a)+b,256));
end

KaMATLAB
KaMATLAB 2020년 9월 2일
function txt = caesar(txt,key)
txt = double(txt) + key;
first = double(' ');
last = double('~');
% use mod to shift the characters - notice the + 1
% this is a common error and results in shifts
% being off by 1
txt = char(mod(txt - first,last - first + 1) + first);
end

Prerona Dey
Prerona Dey 2020년 11월 21일
function y = caesar2(ch, key)
v = ' ' : '~';
[~, loc] = ismember(ch, v);
v2 = circshift(v, -key);
y = v2(loc);
end
Can u please explain me this soultion I found.

Zia Ur Rehman
Zia Ur Rehman 2022년 8월 28일
Hi folks,
I write this code, this is working fine with the problem.
Need further improvement if any from seniors as I'm very novice in coding and MATLAB.
function coded = caesar(a,b)
% removing the ';' so you can see how it works in output
c = double(a) % to convert the given char(string) into double(numeric)
d=c+b % adding the shift smount to encrypt
l= length(d) % measuring the length as we need to traverse every element to check if it lies in the limit(32:126)
for e = 1:l % applying loop to check each element if it lies in the limit
while d(e) > 126 % using while as if we use 'if' statement it will only execute once but we need execution untill the value comes in the limit
d(e) = d(e)-95 % if number is greater than 126 so wrap around by adding (126-32+1=95) we use +1 as we need next number not the same number
end
while d(e) < 32 % using while as if we use if statement it will only execute once but we need execution untill the value comes in the limit
d(e) = d(e) + 95 % if number is less than 32 so wrap around by subtracting (126-32+1=95) we use +1 as we need next number not the same number
end
end
coded=char(d) % d is now updated according to the limit
%coded = char(double(a) + b)
end
  댓글 수: 2
John D'Errico
John D'Errico 2022년 8월 28일
This is not an answer to the question, instead a request for some coding adivce on how to improve your code.
Does your code work? Is there a problem with the code?
Walter Roberson
Walter Roberson 2022년 8월 28일
Let us test:
in = 'To whom it may concern'
in = 'To whom it may concern'
O1 = caesar(in, 5)
O1 = 'Yt%|mtr%ny%rf~%htshjws'
O2 = caesar(O1, -5)
O2 = 'To whom it may concern'
O3 = caesar(in, 5120)
O3 = 'Jeum^ecu_jucWouYedY[hd'
O4 = caesar(O3, -5120)
O4 = 'To whom it may concern'
function coded = caesar(a,b)
% removing the ';' so you can see how it works in output
c = double(a); % to convert the given char(string) into double(numeric)
d=c+b; % adding the shift smount to encrypt
l= length(d); % measuring the length as we need to traverse every element to check if it lies in the limit(32:126)
for e = 1:l % applying loop to check each element if it lies in the limit
while d(e) > 126 % using while as if we use 'if' statement it will only execute once but we need execution untill the value comes in the limit
d(e) = d(e)-95; % if number is greater than 126 so wrap around by adding (126-32+1=95) we use +1 as we need next number not the same number
end
while d(e) < 32 % using while as if we use if statement it will only execute once but we need execution untill the value comes in the limit
d(e) = d(e) + 95; % if number is less than 32 so wrap around by subtracting (126-32+1=95) we use +1 as we need next number not the same number
end
end
coded=char(d); % d is now updated according to the limit
%coded = char(double(a) + b)
end
That seems to work.
Notices, though, that I added semi-colons everywhere, to prevent it from repeatedly outputing results as it goes.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

제품


릴리스

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by