Caesarts Cipher encryption algorithm assistance

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, it substract the same value from the characters. Write a function called caesar that accepts two arguments: the first is the character vector to be encrypted, while the second is the shift amount. The function returns the output argument coded, the encrypted text. The function needs to work with all the visible ASCII characters from space to ~. The ASCII codes of these are 32 through 126. If the shifted code goes outside of this range, it should wrap around. For example, if we shift ~ by 1, the result should be space. If we shift space by -1, the result should be ~.
Here are a few things you may want to try with MATLAB before starting on this assignment:
double(' ')
ans =
32
double('~')
ans =
126
char([65 66 67])
ans =
'ABC'
' ' : '~'
ans =
' !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~'
And here are a few example runs:
caesar('ABCD',1)
ans =
'BCDE'
caesar('xyz ~',1)
ans =
'yz{! '
caesar('xyz ~',-1)
ans =
'wxy~}'
Code to Call your Function:
coded = caesar('ABCD', 3)
decoded = caesar(coded, -3)
wrap = caesar('1234', 96)
back = caesar(wrap, -96)
Hi there, I am having trouble with the caesars cypher question for week 8 homework assignments and I was wondering if I could get some help with the code that I have so far. Here is what I have, any help would be appreciated.
function coded = caesar(v,n)
v = char(32:126)
secret = v + 2
mod(v,126)
coded = char(mod(v,126))
end
ps. I feel embarrased posting this I am trying to figure it out but it is very difficult for me.

20 件のコメント

KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 7 月 14 日
May be your teacher, forgot to ask the question.
Andrew Marttini
Andrew Marttini 2019 年 7 月 14 日
Im sorry I not sure what you want me to do with this text?
dpb
dpb 2019 年 7 月 14 日
"I'm sorry I not sure what you want me to do with this text?"
He's pointing out you tried to post the full question in the Question title instead of it in the question text box for that purpose and so it got truncated and the full question is not shown...
Andrew Marttini
Andrew Marttini 2019 年 7 月 14 日
oh sorry.
Andrew Marttini
Andrew Marttini 2019 年 7 月 14 日
I reposted up above to include the full question. Sorry for the inconvenience.
dpb
dpb 2019 年 7 月 14 日
編集済み: dpb 2019 年 7 月 14 日
So, now finish the cleanup you started...Take the question text out of the title and make a short, appropriate title for the question (write a headline, iow)
Then, format the code to be legible...(select the code then use the "CODE" button or press CTRL-e)
Andrew Marttini
Andrew Marttini 2019 年 7 月 14 日
Hope this is better.
Walter Roberson
Walter Roberson 2019 年 7 月 14 日
Your code ignores the two inputs.
Andrew Marttini
Andrew Marttini 2019 年 7 月 15 日
I defined V as the character vector to be encrypted: char(32:126), I know n is the shift amount. are you saying when I put coded = char(mod(v,126)) I am not addressing v or n?
dpb
dpb 2019 年 7 月 15 日
What Walter's pointing out is that you pass in v but immediately overwrite it and never use n
PS. Thanks for cleaning up the question--much easier to read...
Andrew Marttini
Andrew Marttini 2019 年 7 月 15 日
Hmm maybe this is a little better? Or atleast a little closer to addressing v and n in the function
function coded = caesar(v,n)
v = char(32:126)
coded = v + n
Walter Roberson
Walter Roberson 2019 年 7 月 15 日
編集済み: Walter Roberson 2019 年 7 月 16 日
That code still ignores the input v.
Walter Roberson
Walter Roberson 2019 年 7 月 15 日
Suppose you were restricting to the range 5, 6, 7, 8 . Then if you were to take the input minus 5, the result would be 0, 1, 2, 3 . If you were to make an arithmetic change to that, and take the result mod 4 then the result would still be in the range 0, 1, 2, 3. If you were then to add 5 to that, the result would be 5, 6, 7, or 8.
It should be trivial for you to extend this logic to the range you are using.
Andrew Marttini
Andrew Marttini 2019 年 7 月 17 日
Hey there Walter,
So I tried a new function but I am still getting errors. I tried implementing mod in my function but I think I am going about this the wrong way. I tried to implement your logic on the situation but I am still not quite understanding.
function coded = caesar (v,n)
v_shift = double(v)
v_new = v_shift + n
mod(32:126,32)
coded = char(v_new)
end
Andrew Marttini
Andrew Marttini 2019 年 7 月 17 日
I was thinking use mod to restrict between a certain range, that being 32:126 and 32 to wrap around it passes 126.
Walter Roberson
Walter Roberson 2019 年 7 月 17 日
mod(32:126,32) does a calculation on some constants and then displays the results. The line does not make any change to any variables.
Andrew Marttini
Andrew Marttini 2019 年 7 月 17 日
How can I apply mod to the function? Do I need to include v_new into the mod function? If I define v I override v
Walter Roberson
Walter Roberson 2019 年 7 月 17 日
Yes you need to include v new in the mod function.
The only place 32:126 should occur is in a comment.
evan muas
evan muas 2019 年 12 月 29 日
A link to my answer on stackexchange
https://stackoverflow.com/a/59522032/10810614
Mayank Joshi
Mayank Joshi 2021 年 8 月 5 日
coded=char(mod((double(v)+n-32),95)+32); Use this

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

回答 (4 件)

Akhil Thomas
Akhil Thomas 2020 年 5 月 16 日

1 投票

function
y = caesar2(ch, key)
v = ' ' : '~';
[~, loc] = ismember(ch, v);
v2 = circshift(v, -key);
y = v2(loc);
end
Akhil Thomas
Akhil Thomas 2020 年 5 月 16 日

0 投票

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
Shaun Olisagu
Shaun Olisagu 2020 年 7 月 27 日

0 投票

function coded = caesar(text, amount)
new_text = double(text)
text_shift = new_text + amount
coded = char(mod(text_shift,(32:126,32)))
end
Pls help. I've been on this for quite a long time now
Yifan He
Yifan He 2022 年 7 月 28 日
編集済み: Walter Roberson 2022 年 7 月 28 日

0 投票

function coded = caesar(v,s)
x = double(v);
y = x + s;
if y >= 32
n = fix((y-32)/95);
else
n = fix((y-126)/95);
end
z = y - 95*n;
coded = char(z);

2 件のコメント

Walter Roberson
Walter Roberson 2022 年 7 月 28 日
What if the requested shift is more than +/- 95 ?
Yifan He
Yifan He 2022 年 7 月 28 日
It still make sense. This problem is a step function in mathematics, actually. z=f(y)=y-95n (32+95n ≤ y ≤ 126+95n,n∈Z)

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

カテゴリ

ヘルプ センター および File ExchangeProgramming についてさらに検索

質問済み:

2019 年 7 月 14 日

コメント済み:

2022 年 7 月 28 日

Community Treasure Hunt

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

Start Hunting!

Translated by