User-defined function to decrypt/encrypt messages (matlab)

In summary, the author is trying to solve a problem in a PDF file that is attached. They are trying to understand what the equation is saying in (b) and are having difficulty because they don't understand what is being asked. They are working on a solution, but it is not working.
  • #1
gfd43tg
Gold Member
950
50

Homework Statement


I am working on Problem #2 in the attached PDF


Homework Equations





The Attempt at a Solution


I am trying to understand what they are saying in (b) as far as creating the double array. Does that mean I concatenate strIn with keyL? Also, how do I remove the last few characters in keyL so that numel(keyL) == numel(strIn). I'm having a hard time getting started because I don't really understand what they are saying in the question as far as what to do.

Here is what I have so far, it's far from complete but I don't know if I should use a while statement.

Code:
function strOut = Convert(strIn,key,mode)
keyL = [key key];
if mode == 1
    while (numel(keyL) >= numel(strIn)) == 1
        numel(keyL) == numel(strIn)
        for i = 1:numel(keyL)
 

Attachments

  • FCNassignment.pdf
    128.5 KB · Views: 776
Physics news on Phys.org
  • #2
Double is a variable type http://en.wikipedia.org/wiki/Double-precision_floating-point_format
The issue with the encoding scheme that they have defined comes down to the fact that chars are generally an 8 bit value so they only give you 0-256 (for unsigned)
So when you add 2 char's values together they can wrap around, While this should still work regardless, generally converting the variable to a type that has the proper range is preferable.

Remember that char's while they repersent characters are at a base level still just numbers :)
So what they want you do to for example say the first letter of the array to encode is 'A' that is ascii 65 if then the key's first letter is 'm' (ascii 109) you'd add them together and get 174 which you'd plop into the double array.

as far as how you get kelL to be the same length as strIn there are a number of ways to do this which you should figure out on your own :)
But let me give you a couple hints.
You're right that you'll need a loop of some fashion. For loops generally I go by:
If you know exactly how many times the loop needs to run use a for loop
if you know the loop needs to run at least 1 or more times usually a do while loop
if you know the loop needs to run 0 or more times usually a while loop.
So which case would this fall under??

Also loop up exactly what the mod function does it can be quite helpful in cycling through things :)
 
  • #3
Here is what I have so far
Code:
function strOut = Convert(strIn,key,mode)
key = 'key';
strIn = 'abcdefgh';
keyL = repmat(key,1,ceil(numel(strin)/numel(key)));
keyL = keyL(1:length(strIn));
doubletrouble = double(keyL) + double(strIn);
if nargin == 2
    for i = 1:length(strIn)
        doubletrouble(i) = doubletrouble(i) - mod(i,10);
    end
end
if nargin == 3
    if strcmpi(mode,'encrypt')
        for i = 1:length(strIn)
            doubletrouble(i) = doubletrouble(i) + mod(i,10);
        end
        strOut = char(doubletrouble);
    end
end
strOut = char(doubletrouble);
if strcmpi(doubletrouble,'decrypt')
    for i = 1:length(strIn)
        doubletrouble(i) = doubletrouble(i) - mod(i,10);
    end
    strOut = char(doubletrouble);
end
end

It still isn't working though.
 
  • #4
solved, disregard
 
  • #5

strOut(i) = strIn(i) - keyL(i);
end
end
elseif mode == 2
while (numel(keyL) >= numel(strIn)) == 1
numel(keyL) == numel(strIn)
for i = 1:numel(keyL)
strOut(i) = strIn(i) + keyL(i);
end
end
end

As a scientist, it is important to ensure that code is clear and well-documented, especially when working with encryption and decryption functions. From the provided code, it appears that the user is attempting to create a function that can encrypt or decrypt a given string (strIn) using a given key and mode. The keyL array is created by concatenating the key with itself, which creates a longer key that can be used to encrypt or decrypt the entire string.

To remove the extra characters in keyL, the user could use the 'end' keyword in MATLAB. For example, if the key is 'abc' and the string has a length of 10, keyL will have a length of 6. To make them equal, the user can use keyL = [key key(end-3:end)] to create a keyL array with the same length as the string. This can be adjusted depending on the length of the key and string.

In terms of the while loop, it may be more efficient to use a for loop instead, as it will only run for the necessary number of iterations. Additionally, the if statement could be simplified to just check if mode is equal to 1 or 2, rather than checking for specific values.

It may also be helpful to add comments throughout the code to explain what each section is doing and why. This can help others (or even the user) understand the code better and make it easier to troubleshoot any issues that may arise.
 

1. What is a user-defined function for encrypting/decrypting messages in MATLAB?

A user-defined function in MATLAB is a custom function that has been created by the user to perform a specific task, in this case, encrypting or decrypting messages. It allows the user to customize the process of encryption/decryption according to their specific needs.

2. How do I create a user-defined function for encrypting/decrypting messages in MATLAB?

To create a user-defined function for encrypting/decrypting messages in MATLAB, you can use the "function" keyword followed by the name of the function and the input and output arguments. You can then write the code to perform the encryption/decryption process within the function body.

3. Can I use any encryption/decryption algorithm in a user-defined function in MATLAB?

Yes, you can use any encryption/decryption algorithm of your choice in a user-defined function in MATLAB. MATLAB has a wide range of built-in functions and capabilities that allow you to implement various encryption/decryption techniques.

4. Is a user-defined function for encrypting/decrypting messages in MATLAB secure?

The security of a user-defined function for encrypting/decrypting messages in MATLAB depends on the algorithm and key used for encryption/decryption. It is recommended to use strong and proven encryption techniques to ensure the security of your messages.

5. Can I use a user-defined function for encrypting/decrypting messages on different platforms?

Yes, you can use a user-defined function for encrypting/decrypting messages on different platforms as long as the platforms support MATLAB and have the necessary libraries and functions required for the function to run properly.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
9
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
805
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
935
Back
Top