Trying to make a function for Caesar Encryption -- need help.

1 visualización (últimos 30 días)
Samir Saidi
Samir Saidi el 26 de Nov. de 2018
Comentada: MANIKANTA THOTA el 15 de Sept. de 2020
I've got a project which involves designing a GUI that can encrypt a string using Caesar encryption. I've been making this function file to use in the GUI .m file, but I've encountered some problems. I think I've got the method properly, I just can't seem to get it right without any errors.
function output = encCsar(word,key)
numWord = uint8(word);
numKey = uint8(lower(key)) - 96;
for i = 1:numel(numWord)
if ( numWord>=65 & numWord<=90 )
numWord=mod(numWord-65+numKey,26);
output(i,1)=char(numWord+65);
elseif ( numWord>=97 & numWord<=122 )
numWord=mod(numWord-97+numKey,26);
output(i,1)=char(numWord+97);
else
error('Please enter a string and a number key.')
end
end
My method is:
  1. Convert "word" (a string) into its respective numerical array in ASCII.
  2. Convert the key (one lowercase letter to determine how much to shift by) to its respective ASCII number.
  3. run a for loop that runs for each element of the numerical array numWord
  4. Apply the Caesar encryption formula (num value of the letter + num value of the key) in mod 26, and I had it run for both uppercase and lowercase letters just in case the string isn't completely lowercase or uppercase.
  5. It then outputs each encrypted element as a new array, and it converts it back to characters.
That's what it's supposed to do in theory, but I've gotten the error I set up ('Please enter a string and a number key.') in case it fails each time. If I try commenting out the else part, the function just doesn't do anything. How should I go about fixing this? Any answers or help would be appreciated.

Respuesta aceptada

Jan
Jan el 27 de Nov. de 2018
Editada: Jan el 27 de Nov. de 2018
I assume, that word is a string or char vector. Then
numWord = uint8(word);
converts it to a vector of type uint8. Now
for i = 1:numel(numWord)
starts a loop over all elements of this vector. But
if ( numWord>=65 & numWord<=90 )
uses the complete vector in the condition. If Matlab's if gets an array as condition, if performs this implicitly:
if all(X(:)) && ~isempty(X)
Most likely you want to use the loop index i:
if numWord(i) >= 65 & numWord(i) <= 90
% ^^^ ^^^
By the way, it is easier to operate on the characters directly:
for i = 1:numel(word)
if word(i) >= 'a' && word(i) < 'z'
output(i) = char(65 + mod(word(i) - 65 + numKey, 26));
...

Más respuestas (1)

Juan Sebastián Hincapié Montes
Juan Sebastián Hincapié Montes el 21 de Ag. de 2020
I've got a solution to this problems... hope this help you!
function [coded] = caesar(v ,sa)
secret = double(v);
code = ones(1, length(v));
for ii=1:length(secret)
if secret(ii)+sa > 126 %greater than 126
remainder=rem(sa,95);
if remainder + secret(ii)>126 %if the double plus the remainder is greater than 32
code(ii)=31+(remainder-(126-secret(ii)));
else
code(ii)=remainder+secret(ii); %if the double plus the remainder isn't greater than 32
end
elseif secret(ii)+sa < 32 %lower than 32
remainder=abs(rem(sa,95));
if secret(ii)-remainder < 32 %if the double plus the remainder is lower than 32
code(ii)=127-(remainder-(secret(ii)-32));
else
code(ii)=secret(ii)-remainder; %if the double plus the remainder isn't lower than 32
end
else
code(ii) = sa + secret(ii); %everything is normal
end
end
coded=char(code);
end

Categorías

Más información sobre Programming en Help Center y File Exchange.

Productos


Versión

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by