clear all
clc
disp('We demonstrate how to use modular arithmetic')
disp('to encrypt text messages.')
disp('This is taken from Cleve Moler''s ''Numerical Computing'' book.')
disp(' ')
pause
disp('The usual ASCII characters we use to communicate')
disp('are associated with the values 32-127.')
disp('We first display these numbers in a compact way')
disp('arranged in a 8x12 matrix. The transposition is needed')
disp('to have the numbers ascend across rows instead of columns.')
pause
disp('>> x = reshape(32:127,12,8)''')
x = reshape(32:127,12,8)'
pause
disp('Here are the corresponding ASCII characters:')
disp('>> c = char(x)')
c = char(x)
pause
disp('As an aside, here are the digits 0-9 (as a string):')
disp('>> d = char(48:57)')
d = char(48:57)
pause
disp('In order to convert this to individual digits we try')
disp('>> str2num(d)')
str2num(d)
pause
disp('That didn''t work (since this is one single number), so we try')
disp('>> double(d)')
double(d)
pause
disp('... and realize that we have to correct')
disp('>> double(d)-''0''')
double(d)-'0'
pause
disp('or')
disp('>> double(d)-48')
double(d)-48
pause
disp('or')
disp('>> double(d)-double(d(1))')
double(d)-double(d(1))
pause
disp(' ')
disp('The encryption will be done via modular arithmetic.')
disp('We pick a prime number equal to the number of characters')
disp('we wish to encode. (There''s a little fudging going on')
disp('here that I don''t want to worry about.)')
disp('>> p = 97;')
p = 97;
pause
disp('We also pick a 2x2 encoding matrix')
disp('with which we will encode 2 characters at a time.')
disp('>> A = [71 2; 2 26]')
A = [71 2; 2 26]
pause
disp('Note that')
disp('>> A^2')
A^2
pause
disp('but')
disp('>> mod(A^2,p)')
mod(A^2,p)
pause
disp('which means that the matrix is its own inverse')
disp('and therefore can be used for both encoding and decoding')
disp('(provided we use modular arithmetic).')
pause
disp('Note that ''mod(x,p)'' gives us the remainder')
disp('after division of x by p. For example')
disp('>> mod(13,5)')
mod(13,5)
pause
disp(' ')
disp('Let''s take some text:')
disp('>> message = ''Math 100''')
message = 'Math 100'
pause
disp('We convert this to numbers in the range of 0-96')
disp('>> x = double(message) - 32')
x = double(message) - 32
pause
disp('and see that this works (no encoding yet).')
disp('>> char(x+32)')
char(x+32)
pause
disp(' ')
disp('Now, let''s encode.')
disp('First we reshape the message (or its vector of numbers)')
disp('so that we have a matrix with 2 rows (we won''t worry')
disp('about even/odd here - our message consists of 8 characters).')
disp('This will allow us to treat all columns (=pairs of 2 characters)')
disp('simultaneously.')
disp('>> X = reshape(x,2,length(x)/2)')
X = reshape(x,2,length(x)/2)
pause
disp('Here''s the encoding step using modular arithmetic:')
disp('>> Y = mod(A*X,p)')
Y = mod(A*X,p)
pause
disp('Now we put all the numbers back into a long vector')
disp('>> y = reshape(Y,1,length(x))')
y = reshape(Y,1,length(x))
pause
disp('and finally convert them back to characters:')
disp('>> char(y+32)')
char(y+32)
pause
disp(' ')
disp('The function ''crypto'' from the book collects all of this')
disp('work, and also allows us to decode again:')
disp('>> crypto(ans)')
crypto(ans)