MATLAB project to decrypt messages

  • Context: MATLAB 
  • Thread starter Thread starter G'mo
  • Start date Start date
  • Tags Tags
    Matlab Project
Click For Summary
SUMMARY

The forum discussion focuses on a MATLAB project aimed at decrypting messages composed of uppercase characters and spaces. The primary issue identified is that spaces are incorrectly converted to the ASCII value '7' during decryption. The solution proposed involves restructuring the conditional statements to ensure that spaces remain unchanged, specifically by using an outermost IF statement to handle spaces separately. This adjustment prevents the conversion of space characters into other values, thereby correcting the output.

PREREQUISITES
  • Understanding of MATLAB programming language
  • Familiarity with ASCII character encoding
  • Knowledge of conditional statements in programming
  • Basic concepts of string manipulation in MATLAB
NEXT STEPS
  • Review MATLAB conditional statements and control flow
  • Learn about ASCII values and their manipulation in MATLAB
  • Explore string handling functions in MATLAB
  • Investigate debugging techniques for MATLAB code
USEFUL FOR

This discussion is beneficial for MATLAB developers, students learning programming, and anyone interested in cryptography and message decryption techniques.

G'mo
Messages
3
Reaction score
0
Hi everyone. I have a MATLAB project to decrypt messages using only uppercase characters and spaces(i.e no punctuation). The problem is that my program is working but does not print spaces, instead of a space it prints 7. If anyone can help, I'll be very greatful.Here is my program:

a1 = input('Please enter a sentence: ','s');
p = upper(a1); % Change the text to uppercase
double(p); % String to ASCII codes

for i = 1:numel(p) % Iterate as long as there are characters
% in the string
if (p(i) ~= 32) & (p(i) > 67)
p(i) = p(i) - 3;
elseif (p(i) <= 67)
p(i) = p(i) + 23;
else
* (p(i) == 32)
end
end
disp(p)

* This part seems to be useless.
If the user inputs: khoor pb iulhqg
output: HELLO7MY7FRIEND
 
Physics news on Phys.org
Just replace any occurrence of '7' in p with ' ' before disp(p):

for i=1:numel(p)
if(p(i)=='7')
p(i)=' ';
end
 
Your problem appears to be this part:

Code:
elseif (p(i) <= 67)
  p(i) = p(i) + 23;

...it converts 32 (' ') into 32 + 23 = 55 ('7').

If you don't want to convert spaces into anything then use an outermost IF, something like this:

Code:
if (p(i) ~= 32)
  if (p(i) > 67)
    p(i) = p(i) - 3;
  else
    p(i) = p(i) + 23;
  end
end

(I'm unsure of the syntax but you should get the idea.)
 

Similar threads

  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
1
Views
6K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 7 ·
Replies
7
Views
11K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
8K
  • · Replies 3 ·
Replies
3
Views
2K