1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

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

  1. Jun 24, 2014 #1

    Maylis

    User Avatar
    Gold Member

    1. The problem statement, all variables and given/known data
    I am working on Problem #2 in the attached PDF


    2. Relevant equations



    3. 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 (Text):
    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)
     

    Attached Files:

  2. jcsd
  3. Jun 24, 2014 #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 :)
     
  4. Jun 24, 2014 #3

    Maylis

    User Avatar
    Gold Member

    Here is what I have so far
    Code (Text):
    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.
     
  5. Jun 24, 2014 #4

    Maylis

    User Avatar
    Gold Member

    solved, disregard
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: User-defined function to decrypt/encrypt messages (matlab)
Loading...