Decrypting a message without knowing the key?
Show older comments
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
on 30 Oct 2015
1 vote
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
Brian Tiffman
on 31 Oct 2015
Walter Roberson
on 31 Oct 2015
Functions. Learn how to use them.
Brian Tiffman
on 2 Nov 2015
Geoff Hayes
on 2 Nov 2015
Brian - if the key is unknown, then how can it be an input to the decrypt function? Or are you passing in a different key each time you call decrypt? If the latter is true, then I don't understand why you are using keylength. Please describe how you are calling this function and what the inputs are (in particular key).
Brian Tiffman
on 2 Nov 2015
Geoff Hayes
on 2 Nov 2015
Brian - I would go with your second idea: loop over every possible key as 1,2,3,...,26 rather than trying to randomly create a key.
Brian Tiffman
on 2 Nov 2015
Geoff Hayes
on 2 Nov 2015
Just create a for loop and iterate over each possible key. Something like
for key=1:26
% call you decrypt function with key
end
Brian Tiffman
on 2 Nov 2015
Geoff Hayes
on 3 Nov 2015
Brian - please clarify what you mean by a little off using an example. Also, as I indicated in a previous comment, there is no need for the if statements that check to see if the decrypted character is greater than or equal to 90 or 122. Not only are they unnecessary but will add 26 to 'z' or 'Z' and so give you an incorrect answer.
Brian Tiffman
on 3 Nov 2015
Edited: Brian Tiffman
on 3 Nov 2015
Geoff Hayes
on 3 Nov 2015
Brian - please show this with an example. Include the message and the encrypted text. And, have you removed the code that checks to see if the characters are greater than or equal to 90 or 122?
Geoff Hayes
on 3 Nov 2015
Also, step through the code with the debugger. Because if you do so, you will notice that on each iteration of the for loop, your message to decrypt is never the same as the previous iteration...as you are always overriding it with the encrypted message. You need to keep the decrypted message separate from the encrypted (input) message.
Brian Tiffman
on 3 Nov 2015
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
on 3 Nov 2015
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!