How Can I Optimize My MATLAB Cipher Program?

  • Thread starter G'mo
  • Start date
  • Tags
    Program
In summary: So, if the question is "In summary, the code provided is a ciphering program that takes a sentence as input and converts it to uppercase before passing it through a function to remove unwanted characters. It then converts the string to ASCII numbers and uses a loop to iterate through each character, replacing it with a corresponding letter 3 places along in the alphabet (with some exceptions for certain characters). The code is not very efficient and can potentially be shortened by using a vectorized approach. However, the current mapping used may make it difficult to decipher the encrypted message.
  • #1
G'mo
3
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
  • #2
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.
 
  • #3
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:
  • #4
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.
 
  • #5
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.
 

1. What is a ciphering program?

A ciphering program is a software or computer program that is used to encode or decode messages using a specific set of rules or algorithms. It is commonly used to protect sensitive information and ensure secure communication.

2. How does a ciphering program work?

A ciphering program works by taking a plain text message and converting it into a coded message using a specific encryption algorithm. The recipient of the message then uses the same program to decode the message and retrieve the original message.

3. Can you recommend a good ciphering program?

There are many good ciphering programs available, and the best one for you will depend on your specific needs and preferences. Some popular options include VeraCrypt, GnuPG, and AES Crypt. It is important to research and compare different programs to find one that suits your needs.

4. Is ciphering the same as encryption?

Ciphering and encryption are often used interchangeably, but there is a subtle difference between the two. Ciphering is the process of converting a message into a secret code, while encryption is the process of scrambling a message using a specific algorithm to make it unreadable to unauthorized parties.

5. Are there any drawbacks to using a ciphering program?

While ciphering programs are a useful tool for protecting sensitive information, they are not foolproof. Some potential drawbacks include the possibility of the cipher being cracked by someone with enough computing power and the risk of forgetting the password or losing access to the program, resulting in permanently inaccessible data.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
Replies
3
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
2
Replies
36
Views
2K
  • General Discussion
Replies
18
Views
11K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
7K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • General Discussion
Replies
9
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
2K
Back
Top