For the attached document, were supposed to encrypt a message, and I got the numbers to come out, but when I try to convert it back to a message it just displays the original message instead of the secret code. I'll attach my code, I have no idea whats wrong with it, and why it isnt working. Thanks.

 채택된 답변

Geoff Hayes
Geoff Hayes 2015년 10월 28일

0 개 추천

Nick - look closely at your for loop
for k=1:1
k is only ever going to be one. You need to iterate over each element of the number_message array. Hint: use length to do so.
Also, take a look at your code to add the key to the number_message array. Remember that the array is an array of doubles. What is the data type for your key?

댓글 수: 6

Nick Haufler
Nick Haufler 2015년 10월 28일
Should it be k:the length of the original message.
Should the key not be a string. Im pretty sure adding key to the number_message is correct, isnt it?
Geoff Hayes
Geoff Hayes 2015년 10월 28일
Nick - since this is homework, I can only give hints. To determine which array to use length on, ask yourself which array you are iterating over? As for your second question, yes you want to add the key to each element in the number_message array. But you will want to make sure that each is of the same data type. Since you have converted the input string, don't you have to do the same for the input key?
Use the MATLAB debugger to step through the code and verify that all additions (of the key) are what you expect.
Nick Haufler
Nick Haufler 2015년 10월 28일
number_message is being iterated over and over to determine which letter matches up with the number. I thought I did make the key a string in my input statement. Thanks!
Geoff Hayes
Geoff Hayes 2015년 10월 28일
Nick - if you are adding the key to the elements of the number_message array which is of type double, then doesn't your key have to be converted to (or cast as) a double too?
Nick Haufler
Nick Haufler 2015년 10월 29일
편집: Geoff Hayes 2015년 10월 29일
Ahh, so key=double(original_message). I have in my for loops
number_message(k)=number_message(k)+key,
wouldnt I want to multiply the two instead of make them equal? so change the equal sign to multiplication.
Your code asks the user to enter a key. What is the range for this key? Is it one through twenty-six? Because that seems to be the case given the remainder of your code when you add the key and check the conditions
number_message(k)>=90
or
number_message(k)>=122
Now, your code asks the user to enter in the key and you save it to the variable key. But this is a string and so is not necessarily in the range of one through twenty six. You need to convert it to a double so that the (for example) '2' becomes 2 as
key = str2double(key);
Nick, you really need to step through the code using the debugger so that you can understand what is happening with each line of code that you have written.

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

추가 답변 (3개)

Thorsten
Thorsten 2015년 10월 29일
편집: Thorsten 2015년 10월 29일

0 개 추천

If you use input with the 's' argument, it will return a string. For the key you need a number, so use
key=input('What will be the encryption key you are using:')
and of course you have to run the loop for all values of the message:
for k=1:length(original_message)
That's it. Given that your key values are not larger then 26, as has been pointed out by Geoff. If you have larger numbers, you have to use the mod function. I've detailed this in a response to one of your colleagues http://www.mathworks.com/matlabcentral/answers/251101-writing-secret-codes.

댓글 수: 3

Nick Haufler
Nick Haufler 2015년 10월 30일
편집: Nick Haufler 2015년 10월 30일
I get the code to display the message in numbers, but what I cant get is changing the numbers back to a secret message in letters. Is there something wrong in my fprintf.
original_message=input('Please enter the message you want encrypted:', '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
ec = char(mod(original_message + key - number_message, 26) + number_message)
fprintf('The encrypted message is %s \n',number_message)
ahmed ibrahim
ahmed ibrahim 2020년 10월 9일
please any one who know this
Write a computer program to encrypt the message “I Love Gondar” in the least significant bit of a gray scale image converted from your own picture. That is:
1. Take a picture of yourself
2. Convert it to grayscale
3. Apply bit plane slicing
4. Separate the lease significant bit plane
5. Encrypt the above message
6. Put all the bit planes back together
7. Show the picture with the encryption
Rik
Rik 2020년 10월 9일
We generally discourage posting solutions to homework questions. You also have not shown any attempt and are hijacking someone else's question. Please post this as a separate question.
Also note that due to the US law that governs this site most discussions about cryptography are not allowed. It may be illegal to answer your question.

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

John BG
John BG 2015년 10월 29일

0 개 추천

original_message=input('Please enter the message you want encrypted:', 's') % original message
% do not input key as string or you will have to write a program with a vector as key
key=input('What will be the encryption key you are using:')
number_message=double(original_message)
mask1=find(number_message>=65 & number_message<=90)
wrapup1=repmat(key,1,length(mask1))
number_message(mask1)
% number_message(mask1)=number_message(mask1)+key % without wrapup
% with wrapup
number_message(mask1)=...
mod(number_message(mask1)+wrapup1,13)+repmat(65,1,length(mask1))
mask2=find(number_message>=97 & number_message<=122)
wrapup1=repmat(key,1,length(mask1))
number_message(mask1)
number_message(mask1)=...
mod(number_message(mask1)+wrapup1,13)+repmat(97,1,length(mask1))
You finish off the fprintf, ok?
John
John BG
John BG 2015년 10월 29일

0 개 추천

The second mask should not have any reference to the first mask, sorry. Replace the last 5 lines of code with
mask2=find(number_message>=97 & number_message<=122)
wrapup1=repmat(key,1,length(mask2))
number_message(mask2)
number_message(mask2)=...
mod(number_message(mask2)+wrapup1,13)+repmat(97,1,length(mask2))
John

카테고리

도움말 센터File Exchange에서 Programming에 대해 자세히 알아보기

질문:

2015년 10월 28일

댓글:

Rik
2020년 10월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by