GS 2001/Math - Collins
Lab 3
Cipher 1 - Modular Arithmetic


Start MATLAB (from Start Menu).
Arrange your windows so you can see MATLAB and this webpage.

Mod Arithmetic

In MATLAB you can do mod-arithmetic by using the mod command.
Type  help mod to learn more about this command.

Suppose you want to compute various things mod-27, then you would compute

    4928   as mod(4928,27)
    22+19  as mod(22+19,27)
    4*16   as mod(4*16,27)
    86-123 as mod(86-123,27)
    -12    as mod(-12,27)

Mod Inverses

To compute additive inverses, there are two obvious methods.  First,
since 0=27 (mod 27), the additive inverse of a number x is
the number y such that x + y = 27.  Thus y = 27 - x.
The second way is to use mod(-x,27).  The second way works
even if x is not in the range 0-26.

Computing multiplicative inverses is more challenging.  There is an algorithm
that can be used to find the multiplicative inverse, but it is pretty
complicated (It is based on the Euclid's Algorithm, which is a way
of finding the GCD of 2 numbers).  The easiest way (in MATLAB) to find
mult. inverses is by exhaustion.  Try the following:
    x = 4   the number you want to find the inverse of
    num = [1:26]   all the numbers from 1 to 26
    res = mod(x*num,27)   result of x*(1,...,26) mod 27
    find(res==1)  this finds which value gives an answer of 1
           find will either produce a number (the inverse) or will 
           result in  "ans = []" meaning there is no inverse.

Alpha -> Numeric -> Alpha

To convert from letters to numbers and back we need to use 3 different
commands upper, real and char.  Try the following:

     P = 'your name'
     P = upper(P)
     cP = real(P)

Note how the result is a list of numbers between 65 and 90, with 32s for
the spaces.  If you then subtract 64, the result is only numbers in the
range 1-26, plus negative numbers for the spaces (and punctuation).
Try the following:

     cP = cP - 64
     ind = find(cP<0)  this finds where non-letters were
     cP(ind) = 0 and replaces them with 0

Now to convert back, try the following:

     xP = [3 8 21 3 11 0 3 15 12 12 9 14 19];
     ind = find(xP==0);
     xP = xP + 64;
     xP(ind) = 32;   replace all noncharacters with spaces

Now convert the coded version of your name (in cP) back to text.

Automating the Process

You can put all the commands to go from text to numbers and from
numbers back to text into a file (called an M-file) and then run
it from MATLAB.  Go to the "File" menu and choose "New -> M-file".
This will pop-up a new window.  Starting with "P = upper(P)", put
all the commands you typed above to convert from text to numbers
in this file. (You can copy and paste if you want).  Now save
this file as "code1.m".

When you run this program it will use whatever value you have set
for P.

In the original MATLAB window (command window), type "code1" and
see what happens.  If you get some error message, try to
figure out what correction you need to make.

Change P to another string of characters and type "code", like
     P = 'I like math more than TV'

Repeat this process with the commands to convert from numbers back
to text and call this file "decode1.m"

Try converting messages back and forth from text to numbers and back.

If you get really frustrated, look at the end of this file.

Put It All Together

Use what you have done here to do the homework.  

   to keep the clutter down you should put semi-colons at the end of each
   you can create a new M-file which calls code1 and/or decode1
   these M-files are called scripts, you can also make functions, use
      help function or see Lab 2 for more information

The coded messages are:




To use these, select all the lines in green, copy them, and paste them
into the M-file editor window.  Save and run.

SCRIPT 1  code1.m

% code1.m  by Dr. Collins 6/19/01 for GSS:Math

P = upper(P);
cP = real(P);
cP = cP - 64;
ind = find(cP<0);
cp(ind) = 0;

SCRIPT 2  decode1.m

% decode1.m  by Dr. Collins 6/19/01 for GSS:Math

ind = find(cP==0);
cP = cP + 64;
cP(ind) = 32;