Having trouble encrypting and decrypting messages

Here's the homework problem I have, and here is the code I put in for it. I keep getting this question wrong. What could I be doing wrong?

2 commentaires

Unfortunately MATLAB does not have any facility for executing pictures of code, so we cannot test your code.
Okay, for sure. Here is the code:
% Testing the encryption function
key = 7;
message = 'My Bday is Nov 28';
encrypted_message = encrypt(message, key);
disp(['Encrypted Message: ', encrypted_message]);
% Decrypting the given message
encrypted_msg = 'VJCUJK Ena 1912j';
for possible_key = 4:9
decrypted_message = encrypt(encrypted_msg, -possible_key);
disp(['Key: ', num2str(possible_key), ', Decrypted Message: ', decrypted_message]);
end
function encrypted_message = encrypt(message, key)
% Function to encrypt or decrypt a message based on a key.
% Spaces are not encrypted and the encryption wraps around for letters and numbers.
encrypted_message = "";
for i = 1:length(message)
charr = message(i);
% Check if character is a space
if charr == ' '
encrypted_message = strcat(encrypted_message, char);
continue;
end
% Determine the ASCII value of the character
ascii_value = double(charr);
% Check if character is an uppercase letter
if 'A' <= charr && charr <= 'Z'
% Wrap around within uppercase letters
encrypted_message = strcat(encrypted_message, char(mod(ascii_value - 65 + key, 26) + 65));
% Check if character is a lowercase letter
elseif 'a' <= charr && charr <= 'z'
% Wrap around within lowercase letters
encrypted_message = strcat(encrypted_message, char(mod(ascii_value - 97 + key, 26) + 97));
% Check if character is a digit
elseif '0' <= charr && charr <= '9'
% Wrap around within digits
encrypted_message = strcat(encrypted_message, char(mod(ascii_value - 48 + key, 10) + 48));
else
% If it's not a letter or digit, just add the character as it is
encrypted_message = strcat(encrypted_message, char);
end
end
end

Connectez-vous pour commenter.

Réponses (1)

Notice that a space character should be encrypted as a space character. The input message you are given has spaces, but the encrypted message you generate does not. This indicates a problem with how spaces are encrypted in your code.
The reason is explained in the strcat documentation:
"For character array inputs, strcat removes trailing ASCII whitespace characters: space, tab, vertical tab, newline, carriage return, and form feed. For cell array and string array inputs, strcat does not remove trailing white space."
This means that when you try to strcat a space onto your encrypted string, it doesn't work:
strcat("message",' ')
ans = "message"
There's no space at the end because strcat removed the trailing space from ' ' and concatenated '' (i.e., an empty character array) onto "message".
But if you convert the character ' ' into a string before doing strcat, then you get the space concatenated properly:
strcat("message",string(' '))
ans = "message "

8 commentaires

Nice catch!
Voss
Voss le 26 Nov 2023
Thanks!
I understand, but I'm not 100% sure where to make the changes. Could you tell me which specific lines need adjustments in my code, and how to adjust them? Thank you for your help.
% Check if character is a space
if charr == ' '
encrypted_message = strcat(encrypted_message, string(char));
continue;
end
Okay, I ran the code, and this is what it gave me:
Given this, what should I put in for my homework answers? I will attach the homework page in another message since it will only let me attach one photo per message.
This is what I got for my latest attempt on my homework. My apologies for the excessive zoom on these images.
The first character of msg is 'M'. The 7'th letter after 'M' is 'T' but your output is 'O' -- which is only 2 letters after 'M' not 7.
Sorry, it should've been
encrypted_message = strcat(encrypted_message, string(charr));
instead of
encrypted_message = strcat(encrypted_message, string(char));
Running the whole thing:
% Testing the encryption function
key = 7;
message = 'My Bday is Nov 28';
encrypted_message = encrypt(message, key);
disp(sprintf('Encrypted Message: %s', encrypted_message));
Encrypted Message: Tf Ikhf pz Uvc 95
% Decrypting the given message
encrypted_msg = 'VJCUJK Ena 1912j';
for possible_key = 4:9
decrypted_message = encrypt(encrypted_msg, -possible_key);
disp(sprintf('Key: %d, Decrypted Message: %s', possible_key, decrypted_message));
end
Key: 4, Decrypted Message: RFYQFG Ajw 7578f Key: 5, Decrypted Message: QEXPEF Ziv 6467e Key: 6, Decrypted Message: PDWODE Yhu 5356d Key: 7, Decrypted Message: OCVNCD Xgt 4245c Key: 8, Decrypted Message: NBUMBC Wfs 3134b Key: 9, Decrypted Message: MATLAB Ver 2023a
function encrypted_message = encrypt(message, key)
% Function to encrypt or decrypt a message based on a key.
% Spaces are not encrypted and the encryption wraps around for letters and numbers.
encrypted_message = "";
for i = 1:length(message)
charr = message(i);
% Check if character is a space
if charr == ' '
encrypted_message = strcat(encrypted_message, string(charr));
continue;
end
% Determine the ASCII value of the character
ascii_value = double(charr);
% Check if character is an uppercase letter
if 'A' <= charr && charr <= 'Z'
% Wrap around within uppercase letters
encrypted_message = strcat(encrypted_message, char(mod(ascii_value - 65 + key, 26) + 65));
% Check if character is a lowercase letter
elseif 'a' <= charr && charr <= 'z'
% Wrap around within lowercase letters
encrypted_message = strcat(encrypted_message, char(mod(ascii_value - 97 + key, 26) + 97));
% Check if character is a digit
elseif '0' <= charr && charr <= '9'
% Wrap around within digits
encrypted_message = strcat(encrypted_message, char(mod(ascii_value - 48 + key, 10) + 48));
else
% If it's not a letter or digit, just add the character as it is
encrypted_message = strcat(encrypted_message, char);
end
end
end

Connectez-vous pour commenter.

Catégories

En savoir plus sur Characters and Strings dans Centre d'aide et File Exchange

Produits

Version

R2023a

Question posée :

le 25 Nov 2023

Commenté :

le 26 Nov 2023

Community Treasure Hunt

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

Start Hunting!

Translated by