MATLAB: Decrypting a message in matlab

decrypt codehomework

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)

Best Answer

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?