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

  • Thread starter Thread starter gfd43tg
  • Start date Start date
  • Tags Tags
    Function Matlab
Click For Summary

Discussion Overview

The discussion revolves around a homework problem related to creating a user-defined function in MATLAB for encrypting and decrypting messages. Participants explore the implementation details, particularly focusing on handling character arrays and the manipulation of key lengths.

Discussion Character

  • Homework-related
  • Technical explanation
  • Mathematical reasoning

Main Points Raised

  • One participant expresses confusion about the instructions for creating a double array and how to adjust the length of the key to match the input string.
  • Another participant discusses the implications of using character values in encryption, noting that characters are represented as 8-bit values and can wrap around when added together.
  • Hints are provided regarding the use of loops, suggesting different types based on the conditions of the problem.
  • A participant shares their current implementation of the function, detailing how they are attempting to handle the encryption and decryption processes but indicates that it is not functioning correctly.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the implementation details, and there are multiple approaches and uncertainties expressed regarding the correct handling of the key length and character encoding.

Contextual Notes

There are unresolved issues regarding the proper handling of character values and the specifics of the encryption and decryption logic, including the use of the mod function and the conditions for loop execution.

gfd43tg
Gold Member
Messages
949
Reaction score
48

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

Physics news on Phys.org
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 :)
 
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.
 
solved, disregard
 

Similar threads

  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
7K
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K