Decrypting a message without knowing the key?

19 views (last 30 days)
I need to repeat my decrypted code 25 times, and stop it once ive correctly decrypted the message. Heres my decryted code:
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)
key=mod(key,26)
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) < 65
number_message(k) = number_message(k)+26
if number_message(k)>=90
number_message(k)=number_message(k)+26
end
end
elseif number_message(k)>=97 && number_message(k)<=122
number_message(k)=number_message(k)-key
if number_message(k) < 97
number_message(k)=number_message(k)+26
if number_message(k)>=122
number_message(k)=number_message(k)+26
end
end
end
end
fprintf('The decrypted message is %s \n',number_message)
I need to decrypt a message without knowing the key though. My code above is when the key is known.

Answers (1)

Geoff Hayes
Geoff Hayes on 30 Oct 2015
Brian - the above code is near identical to that posted at http://www.mathworks.com/matlabcentral/answers/251932-decrypting-a-message-in-matlab by a user who is not named "Brian Tiffman". So when you say my code what do you really mean? ;)
As an aside, the second if block used in the above two "cases" (if the character is lower case or if the character is upper case) is not needed and will cause incorrect results. (Why and when?)
As for decrypting when you don't know the key, since there are only 26 possible keys, then use a for loop to iterate over each one to see all the possible messages. It should be clear which key correctly decrypts the message (the other 25 results will probably be non-sensical).
  16 Comments
Geoff Hayes
Geoff Hayes on 3 Nov 2015
Where is the example? Did you even read my previous comment?
Brian, it really seems that you want or expect the answer to be given to you without having to put any of your own effort into solving the problem. You have taken someone else's code and claimed it as your own and have still not tried to step through it and determine where the problem lies.
Brian Tiffman
Brian Tiffman on 3 Nov 2015
I inputed SGZRGH which should become MATLAB, and this is the example output:
number_message =
77 65 84 76 65 82
The decrypted message is MATLAR
Thats the closest answer I got. Im looking over my if statments again. I dont expect the answer given to me, I am more than happy to work through it. Having the answer handed to me would be taking the easy way out, I thought you wanted a example describing what was going on. Sorry.

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!