How Can I Optimize My MATLAB Cipher Program?

  • Thread starter Thread starter G'mo
  • Start date Start date
  • Tags Tags
    Program
Click For Summary

Discussion Overview

The discussion revolves around optimizing a MATLAB cipher program, focusing on code efficiency and the correctness of character mapping. Participants explore potential improvements, including vectorization and addressing issues with the current mapping of characters.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant suggests creating an array for destination values to simplify the character mapping process, proposing the use of an index based on ASCII values.
  • Another participant points out that the current mapping is incomplete and highlights potential issues with multiple characters mapping to the same output, which could hinder deciphering.
  • A different participant emphasizes the need for the code to handle randomly generated alphabets and to allow for deciphering, indicating that the current implementation does not meet this requirement.
  • Concerns are raised regarding the comments in the code that imply a different mapping logic than what is actually implemented, suggesting a misunderstanding of the intended functionality.

Areas of Agreement / Disagreement

Participants express disagreement regarding the completeness and correctness of the character mapping in the cipher program. There is no consensus on the best approach to optimize the code or resolve the mapping issues.

Contextual Notes

The discussion reveals limitations in the current implementation, including missing character mappings and the potential for multiple inputs to yield the same output, which complicates deciphering. The scope of the program's functionality is also questioned.

G'mo
Messages
3
Reaction score
0
I have the following ciphering program.

a1 = input('Please enter a sentence: ','s');
x = upper(a1); % Change the text to uppercase
p = CleanStr(x) % Call a function CleanStr to remove
% all unwanted characters
double(p); % String to ASCII numbers

for i = 1:numel(p) % Iterate as long as there are characters
% in the string

if (p(i) == 65) % Find all ASCII codes which are less
% than 88 and not spaces
p(i) = 83; % Replace each letter with the letter
% 3 places along in the alphabet

elseif (p(i) == 66)
p(i) = 81;
elseif p(i) == 67;
p(i) = 85;
elseif p(i) == 68
p(i) = 89;
elseif (p(i) == 69) | (p(i) == 89)
p(i) = 78;
elseif (p(i) == 70) | (p(i) == 75)
p(i) = 87
elseif p(i) == 71
p(i) = 69;
elseif (p(i) == 73 ) | (p(i) == 81) | (p(i) == 83)
p(i) = 72;
elseif p(i) == 74
p(i) = 71;
elseif p(i) == 76
p(i) = 84;
elseif p(i) == 77
p(i) = 68;
elseif p(i) == 78
p(i) = 65;
elseif p(i) == 79
p(i) = 88;
elseif p(i) == 80
p(i) = 70;
elseif p(i) == 84
p(i) = 77;
elseif p(i) == 85
p(i) = 66;
elseif p(i) == 86
p(i) = 89;
elseif p(i) == 87
p(i) = 80;
elseif p(i) == 88
p(i) = 76;
elseif p(i) == 90
p(i) = 73;
else
p(i) = 32;
end
end
disp(['The enciphired message is: ',num2str(p)]);

Is there a way of making this program any shorter? ( Maybe vectorising). The program ciphers as follows: (ABCDEFGHIJKLMNOPQRSTUVWXYZ ----> SQUYNWEZHGWTDAXFHRHMBZPLNI).

Thank you for your help guys.
 
Technology news on Phys.org
Yes, just make an array with the destination values SQUY...LNI and look at the appropriate element in that list. Something like dest(p(i) - 65), hwere dest(0) = 'S', dest(1) = 'Q', etc.
 
You are missing characters in your mapping. Sort this second string (SQUYNWEZHGWTDAXFHRHMBZPLNI) to see a few problems:

AB DEFG HHH I LM NN PQRSTU X WW Y ZZ

EDIT: Well, maybe this is intentional. Code like this:

elseif (p(i) == 73 ) | (p(i) == 81) | (p(i) == 83)
p(i) = 72;

...will map three different characters to the same one: H. It makes complete decyphering impossible, but you may have some reason...
 
Last edited:
The code has to work with any radomly generated alphabets & deciphering should also be possible. So far, it is indeed impossible for me to decipher the messages.
 
I also note some comments in your code that say "Find all ASCII codes which are less than 88 and not spaces. Replace each letter with the letter 3 places along in the alphabet." This means mapping ABC...W to DEF...Z but you are implementing an entirely different mapping.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 18 ·
Replies
18
Views
15K
  • · Replies 36 ·
2
Replies
36
Views
4K
  • · Replies 2 ·
Replies
2
Views
8K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 12 ·
Replies
12
Views
3K