필터 지우기
필터 지우기

Decrypting a message in matlab?

조회 수: 12 (최근 30일)
Nick Haufler
Nick Haufler 2015년 10월 30일
편집: Nick Haufler 2015년 10월 30일
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)

채택된 답변

Geoff Hayes
Geoff Hayes 2015년 10월 30일
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
Nick Haufler
Nick Haufler 2015년 10월 30일
편집: Nick Haufler 2015년 10월 30일
So then wouldnt we want to add 26 to number_message(k) if its less than 65
Geoff Hayes
Geoff Hayes 2015년 10월 30일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

TastyPastry
TastyPastry 2015년 10월 30일
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
Nick Haufler
Nick Haufler 2015년 10월 30일
편집: Nick Haufler 2015년 10월 30일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by