Having trouble encrypting and decrypting messages
18 次查看(过去 30 天)
显示 更早的评论
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 个评论
Walter Roberson
2023-11-25
Unfortunately MATLAB does not have any facility for executing pictures of code, so we cannot test your code.
回答(1 个)
Voss
2023-11-25
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",' ')
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(' '))
8 个评论
Walter Roberson
2023-11-26
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.
Voss
2023-11-26
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));
% 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
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
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!