Decrypting a message without knowing the key?

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)

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

Would i need to create a value for key and change that each time through the loop? Do i need to put all the decryption code in my for loop?
Functions. Learn how to use them.
Is this the basis of the for loop? Am I on the right track? Just outlining it.
function plaintext=decrypt(ciphertext,key)
keylength=length(key)
for i=1:length(ciphertext)
ccode=double(ciphertext(i))
keyshiftpoint=mod(i,keylength)
if (keyshiftpoint==0)
keyshiftpoint=keylength
end
shift=double(key(keyshiftpoint))-double('A')
pcode=ccode-shift
if (pcode<65)
pcode=pcode+26
end
plaincode(i)=pcode
end
plaintext=char(plaincode)
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).
Yea, I totally see what youre saying. That was just an outline I did, I don't think I want to continue with that, i don't it will come out correct. but i'd rather stick with the original code. Could i add a variable key= before the loop, and use a randi function to try an find a key that is between 1 and 25. Or should I set key=1 and use for loops for each case, for example key=1, key=2,key=3....etc. all the way down to key=26.
original_message=input('Please enter the message you want decrypted:', 's') % original message
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) < 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
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.
Okay, yea I was thinking that would be the better way to figure it out. Should i make key a vector of values like key=(1:26) or store the value in a variable like key=1, and continue with the original code. Which do you think would be easiest?
Just create a for loop and iterate over each possible key. Something like
for key=1:26
% call you decrypt function with key
end
Thanks. I added the key in, so I get all 26 outputs in my command window, my messages displayed are a little off though.
original_message=input('Please enter the message you want decrypted:', 's') % original message
number_message=double(original_message)
for key=1: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
end
if 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)
end
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.
Well i know what the message should say, and ill have like 3 letters right, and 2 of them are wrong, which throws the whole message off, but its close. If a message was supposed to output MATLAB, it would come out instead as MsTLed
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?
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.
Okay ill show an example. Yes I have removed it. This is what it looks like now?
original_message=input('Please enter the message you want decrypted:', 's') % original message
number_message=double(original_message)
for key=1: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
end
end
if 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
end
end
end
fprintf('The decrypted message is %s \n',number_message)
end
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.
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

Asked:

on 30 Oct 2015

Commented:

on 3 Nov 2015

Community Treasure Hunt

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

Start Hunting!