Decrypting a message in matlab?
27 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Nick Haufler
el 30 de Oct. de 2015
Editada: Nick Haufler
el 30 de Oct. de 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)
0 comentarios
Respuesta aceptada
Geoff Hayes
el 30 de Oct. de 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 comentarios
Geoff Hayes
el 30 de Oct. de 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.
Más respuestas (1)
TastyPastry
el 30 de Oct. de 2015
There are 3 things which don't work with your code:
- It doesn't work when the key is >26.
- 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.
- 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 comentario
Nick Haufler
el 30 de Oct. de 2015
Editada: Nick Haufler
el 30 de Oct. de 2015
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!