Having trouble encrypting and decrypting messages

6 ビュー (過去 30 日間)
Christian
Christian 2023 年 11 月 25 日
コメント済み: Voss 2023 年 11 月 26 日
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
Walter Roberson 2023 年 11 月 25 日
Unfortunately MATLAB does not have any facility for executing pictures of code, so we cannot test your code.
Christian
Christian 2023 年 11 月 26 日
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

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

回答 (1 件)

Voss
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",' ')
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 件のコメント
Walter Roberson
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
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));
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

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

カテゴリ

Help Center および File ExchangeEncryption / Cryptography についてさらに検索

製品


リリース

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by