Decrypting a message in matlab?

27 views (last 30 days)
Nick Haufler
Nick Haufler on 30 Oct 2015
Edited: Nick Haufler on 30 Oct 2015
Im trying to decrypt a message in matlab. My code can decrypt some shorter messages with a low key, but when I try to decrypt a long message with like a key of 9 it wont work. I thought my code was correct, but I guess I have some flaw in it. Can anyone help? (Using ASCII values)
original_message=input('Please enter the message you want decrypted:', 's') % original message
key=input('What will be the encryption key you are using:')
number_message=double(original_message)
for k=1:length(original_message)
if number_message(k)>=65 && number_message(k)<=90
number_message(k)=number_message(k)-key
if number_message(k)<=90
number_message(k)=number_message(k)+26
end
elseif number_message(k)>=97 && number_message(k)<=122
number_message(k)=number_message(k)-key
if number_message(k)<=122
number_message(k)=number_message(k)+26
end
end
end
fprintf('The decrypted message is %s \n',number_message)

Accepted Answer

Geoff Hayes
Geoff Hayes on 30 Oct 2015
Nick - in the future, please tag your questions as homework. Note your condition
if number_message(k)>=65 && number_message(k)<=90
If the above is true, then your encrypted value is between 65 and 90 and the following code is executed
number_message(k)=number_message(k)-key
So the encrypted value has the key subtracted from it. That means that your new interval of values, which was [65, 90] is now [65-key, 90-key]. What is important is that the decrypted values can now be less than 65. This means that your next check should be for those values
if number_message(k) < 65
number_message(k) = ???
end
So how should the above be adjusted?
  2 Comments
Nick Haufler
Nick Haufler on 30 Oct 2015
Edited: Nick Haufler on 30 Oct 2015
So then wouldnt we want to add 26 to number_message(k) if its less than 65
Geoff Hayes
Geoff Hayes on 30 Oct 2015
Right - so you just need to adjust your "inner" conditions to take into account whether the decrypted value is less than 65 or less than 90.

Sign in to comment.

More Answers (1)

TastyPastry
TastyPastry on 30 Oct 2015
There are 3 things which don't work with your code:
  1. It doesn't work when the key is >26.
  2. Your checks for whether or not the values are letters are wrong. Currently, your code attempts to move the ASCII value back to the proper range 65-90, 97-122 when the letter is already within the range. Eg., if the current letter is b, value 66, it would get changed to \, value 92.
  3. The doesn't change the vector back to a string.
Here are the bits of code I would add/change:
number_message = char(number_message);
key=mod(key,26);
number_message(k)<=96
number_message(k)<=64
I'll leave where to put the replacements as an exercise to you.
Also, there are a lot of examples of Caesar ciphers written in MATLAB online. I would encourage you to look at those, since this is a common problem in most courses.
  1 Comment
Nick Haufler
Nick Haufler on 30 Oct 2015
Edited: Nick Haufler on 30 Oct 2015
Thanks,I realized that i needed the last two included. I've tried adding
number_message=char(number_message)
but it just gives me an error. The code will decrypt some messages , but then some other long message it tries to decrypt will partially make sense like some words are correct and some arent.
Heres what I added:
if number_message(k) < 65
number_message(k)=number_message(k)+26
if number_message(k) < 97
number_message(k)=number_message(k)+26

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!