Caesarts Cipher encryption algorithm assistance

5 visualizaciones (últimos 30 días)
Andrew Marttini
Andrew Marttini el 14 de Jul. de 2019
Comentada: Yifan He el 28 de Jul. de 2022
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 comentarios
evan muas
evan muas el 29 de Dic. de 2019
A link to my answer on stackexchange
https://stackoverflow.com/a/59522032/10810614
Mayank Joshi
Mayank Joshi el 5 de Ag. de 2021
coded=char(mod((double(v)+n-32),95)+32); Use this

Iniciar sesión para comentar.

Respuestas (4)

Akhil Thomas
Akhil Thomas el 16 de Mayo de 2020
function
y = caesar2(ch, key)
v = ' ' : '~';
[~, loc] = ismember(ch, v);
v2 = circshift(v, -key);
y = v2(loc);
end

Akhil Thomas
Akhil Thomas el 16 de Mayo de 2020
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 el 27 de Jul. de 2020
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 el 28 de Jul. de 2022
Editada: Walter Roberson el 28 de Jul. de 2022
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 comentarios
Walter Roberson
Walter Roberson el 28 de Jul. de 2022
What if the requested shift is more than +/- 95 ?
Yifan He
Yifan He el 28 de Jul. de 2022
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)

Iniciar sesión para comentar.

Categorías

Más información sobre Encryption / Cryptography en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by