Writing secret codes?

10 views (last 30 days)
Tyler Silva
Tyler Silva on 26 Oct 2015
Edited: Guillaume on 27 Oct 2015
Im writing a code that needs to do the following:
  • 1) Prompt the user to enter the message to encrypt and the encryption key that will be used to encrypt the message
  • 2) Convert your message into the ASCII values by changing it from a string into a vector of double values using the double command:
  • 3) Go through each value in the message and do the following:
  • a. If the character is a capital letter (ASCII values 65-90), encrypt it by adding the encryption key value to the ASCII value of the letter, ensuring that the encrypted value remains a capital letter by wrapping around to the beginning of the alphabet (i.e. a Z (ASCII value of 90) would end up as a D (ASCII value of 68) using a key value of 4)
  • b. If the character is a lowercase letter (ASCII values 97-122), encrypt it by adding the encryption key value to the ASCII value of the letter, ensuring that the encrypted value remains a lowercase letter by wrapping around to the beginning of the alphabet (i.e. a z (ASCII value of 122) would end up as a f (ASCII value of 102) using a key value of 6)
  • c. If the character is not a letter (number, punctuation, space, etc.), leave it as is
  • 4) Display the encrypted message using an fprintf statement and the %s placeholder
The first two are input statements which should be written as
original_message=input('What message would you like to encrypt')
key=input('What encryption key will you be using')
Next, I need to insert the doubles command
number_message = double(original_message);
I get confused after this. Should I make a vector with my message, and then write if statements to convert the letters to numbers. Im having trouble converting my message over to the encryption key.
These are the encryption keys for capital and lowercase tables below:
A B C D E F G H I J K L M
65 66 67 68 69 70 71 72 73 74 75 76 77
N O P Q R S T U V W X Y Z
78 79 80 81 82 83 84 85 86 87 88 89 90
a b c d e f g h i j k l m
97 98 99 100 101 102 103 104 105 106 107 108 109
n o p q r s t u v w x y z
110 111 112 113 114 115 116 117 118 119 120 121 122

Answers (2)

Guillaume
Guillaume on 26 Oct 2015
What you're trying to implement is a Vigenere cipher. The way I would implement it:
  1. convert the string to lowercase (keeping the original) with lower
  2. subtract 'a' from the string, so that it is in the range [0-25]
  3. repmat the key so that it is at least as long as the message. ceil, numel and / will get you the number of repetitions.
  4. add the portion of the repmat'ed key that is the same length as the message to the values of step 2. No loop needed, just a plain +.
  5. mod the result of step 4 by 25.
  6. add 'a' and convert back to char
  7. use the logical array returned by isstrprop(message, 'upper') to convert back the lowercase letters that were uppercase to upper.
No loop needed at any point and at most 7 lines of code.
  7 Comments
Stephen23
Stephen23 on 27 Oct 2015
Edited: Stephen23 on 27 Oct 2015
@Tyler Silva: the most important step of coding is to first understand the algorithm. To use any of these functions you need to understand what each step of the algorithm does, and why that function is used. You can then check each function to see that it gives the expected output.
Do not write a big block of code in one go without testing and checking it as you write it. Write your code line-by-line, and test every line to make sure that it does what you need it to be doing. Double-check it against your own hand calculated results. Use lots of paper!
Although beginners all seem to be allergic to reading the documentation you might like to consider actually reading the documentation for the functions that you are using. The documentation describes what the functions do, and how they should be used. This is useful information for both beginners and advanced users alike!
Guillaume
Guillaume on 27 Oct 2015
Edited: Guillaume on 27 Oct 2015
"You don't have to repmat the key". I misread the assignment indeed. I thought it was a Vigenere cipher where the key consists of several character/numbers, but it is a much simpler Caesar Cipher where the key is just one character cipher. You indeed don't need to repmat the key so step 3 is not even needed and step 4 consists of just adding the key.
"And you have to mod by 26" Do'h! I'll blame it on old age.
"And you have to take care NOT to convert non-letters". I forgot about that. I would simply not worry about it and at the end, again using isstrprop just copy over the original non-letters.
@Tyler, with Thorsten's and my answer you should have more than enough to complete your assignment. If not, then you haven't put in the effort. And do follow Stephen's advice. That's the only way you'll learn to write code (regardless of the language).

Sign in to comment.


Thorsten
Thorsten on 27 Oct 2015
Edited: Thorsten on 27 Oct 2015
Let's work it out first for a single letter.
The base idea of this encryption is to just add a constant offset, the 'key' to the letter. For a key of say, 4, A should be coded to E. Do do this, we convert the letter to number using double, add the key, and convert back to character:
om = 'A'; % original message, just one letter to start with
key = 4;
ec = char(double(om) + key); % encrypted message
So far, so good. But what happens if we add the key to letter and end up with a number > 'Z'? We have to 'wrap if around', such that, e.g., a 'Z' would end up as a 'D'.
If the values of the letter were running from 0 to 25, we could use the mod function for this task, which gives the modulus after division. When we divide by the number of elements, i.e., 26, a value of 26 is mapped to 0, a value of 27 is mapped to 1 (27 divided by 26 is 1 modulus 1), and so on. This concept is familiar to an experienced programmer. Unfortunately, our letter do not have values from 0 to 25, but from 65 to 90 (for the upper case letters). How to handle this? We can map it to the desired range by subtracting 65, take the mod, and map it back to the original range adding 65.
So our code
ec = char(double(om) + key);
becomes
newvalue = double(om) + key;
newvalue = mod(newvalue - double('A'), 26) + double('A');
ec = char(newvalue);
We can write it all in one line:
ec = char(mod(double(om) + key - double('A'), 26) + double('A'))
Interestingly, we can get rid of the double and just compute with letters (or characters), Matlab converts them to numbers for us. So the whole encoding can be written in a single line
ec = char(mod(om + key - 'A', 26) + 'A')
Let's test it with a new original message
om = 'ABYZ'
ec = char(mod(om + key - 'A', 26) + 'A')
ec =
EFCD
Here comes another strength of Matlab into play. Note that we now have encoded four letters, without changing the code.
So far we have just treated uppercase letters. For lower case letters, we have a different range, and need to subtract a different value, namely 'a' to do the mod operation.
The operation would be
ec = char(mod(om + key - 'a', 26) + 'a')
For the beginning it is easiest to use a for loop where you check for every character of the original message whether it is 'lower' or 'upper' and run the specific encoding.

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!