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

  • Thread starter Thread starter gfd43tg
  • Start date Start date
  • Tags Tags
    Function Matlab
AI Thread Summary
The discussion revolves around creating a user-defined function in MATLAB for encrypting and decrypting messages. The main challenge is understanding how to manipulate the key length to match the input string and how to handle character encoding properly. Participants emphasize the importance of converting characters to their ASCII values for arithmetic operations and suggest using loops appropriately based on the conditions of the task. There is also a mention of using the mod function to cycle through indices effectively. The thread concludes with one participant indicating that their issue has been resolved.
gfd43tg
Gold Member
Messages
947
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

Back
Top