Dr. Neal, WKU
MATH 307
Cryptography
with Matrices
Goals
: (i) To take a message and encrypt it into a string of numbers via matrix
multiplication. (ii) Then to take the string of numbers and decrypt it back to a written
message. As an
initial example, We shall use the following message:
"HI THERE BUDDY"
Alpha

Numeric Substitution
First, we must assign each letter a numeric equivalent. Here, we shall reserve the
number 0 to represent a space and use the numbers 1
–
26 to represent
the 26 letters.
Rather than just making a direct substitution in order, we can permute the digits in
some way. For example:
A
B
C
D
E
F
G
H
I
J
K
L
M
2
1
4
3
6
5
8
7
10
9
12
11
14
N
O
P
Q
R
S
T
U
V
W
X
Y
Z
13
16
15
18
17
20
19
22
21
24
23
26
25
Coding Matrix
Next, we choose an
invertible
€
n
×
€
n
matrix
A
, for some size
€
n
, which will be used to
encryp
t the message. Here we shall use
A
=
3
2
3
−
6
−
5
−
4
9
8
7
.
Encrypting The Message
We first convert the message into the numeric script, using the alpha

numeric
substitution above (H =
7, I = 10, Blank = 0, etc).
7 10 0 19 7 6 17 6 0 1 22 3 3 26
Then we convert this script into a
€
m
×
3 matrix (3 columns because our
A
is 3
×
3).
The number of rows
€
m
depends on the length of the message. We fill out the last row
with more 0's as necessary:
B
=
7
10
0
19
7
6
17
6
0
1
22
3
3
26
0
.
Here,
B
is 5
×
3. Now we multiply:
€
C
=
BA
.
Dr. Neal, WKU
C
=
BA
=
7
10
0
19
7
6
17
6
0
1
22
3
3
26
0
×
3
2
3
−
6
−
5
−
4
9
8
7
=
−
39
−
36
−
19
69
51
71
15
4
27
−
102
−
84
−
64
−
147
−
124
−
95
.
Finally, we convert matrix
€
C
into a string that becomes the encrypted message:
–
39
–
36
–
19
69
51
71
15
4
27
–
102
–
84
–
64
–
147
–
124
–
95
H
I
T
H
E
R
E
B
U
D
D
Y
Notice that “H” has been sent to two different values. The first H has been
encrypted as
–
39, a
nd the second H has been encrypted as 51. Likewise the three blanks
are assigned different values, as are the two “E”s and the two “D”s.
Decrypting the message
The second party receives only the encrypted message:
–
39
–
36
–
19
69
51
71
15
4
27
–
102
–
84
–
64
–
147
–
124
–
95
However the receiver knows the alpha

numeric code and the coding matrix
A
.
Thus, the encrypted script is converted back into an
€
m
×
3 matrix
€
C
and then
multiplied by
A
−
1
to undo the original matrix multiplication. Because
€
C
=
BA
, we
obtain the original matrix
B
by
€
B
CA
−
1
, and then we convert the string back to the
alphabet.
ToCharacterCode
Command
When using a software package to encode messages, we can use the built

in features
that will automatically convert alpha
betic symbols into numeric equivalents. With
Mathematica
, we can use the
ToCharacterCode
command.
For example, consider the following message:
Dear colleague, what is 4% of 120?
We now need alpha

numeric equivalents not only for the 26 le
tters, but also for the
symbols % and ?, and for numbers. Rather than trying to define our own alpha

numeric substitution, we can use the built

in
Mathematica
command that is also case

sensitive.
First, type the message enclosed in quotes. Here
we define the message to be the
term
script
. Then apply the
ToCharacterCode
command.
Dr. Neal, WKU
script="Dear colleague, what is 4% of 120?";
numeric=ToCharacterCode[script]
{68,101,97,114,32,99,111,108,108,101,97,103,117,101,44,32,119,
104,97,116,32,105,115,32
,52,37,32,111,102,32,49,50,48,63}
We observe that a “blank” is represented by 32 and that an “l” is assigned 108.
Mathematica Codes
Now we need a short program that will allow us to input our
€
n
×
€
n
coding matrix
A
,
convert
numeric
into an
€
m
×
€
n
matrix
B
and fill out
the last row with “Blanks,”
multiply
B
A
to get
€
C
, and then convert the matrix
€
C
into just a string of numbers.
This string of numbers wil
l be the encrypted message which is sent. The following
commands perform each of these tasks:
script="Dear colleague, what is 4% of 120?";
A={{3,2,3},{

6,

5,

4},{9,8,7}};size=n=3;Det[A]
numeric=ToCharacterCode[script];
While[Mod[Length[numeric],n]!=0,
AppendTo[numeric,32]]
Do[Do[b[i,j]=numeric[[n(i

1)+j]],{j,1,n}],
{i,1,Ceiling[Length[numeric]/n]}];
B=Table[b[i,j],{i,1,Ceiling[Length[numeric]/n]},{j,1,n}];
MatrixForm[B]
Z=B.A;MatrixForm[Z]
encrypt=Flatten[Z]
{471,407,479,1041,860,907,657,546,6
57,648,541,636,141,81,255,
318,301,348,

117,

130,51,

87,

109,79,222,175,232,9,

32,149,
279,232,283,285,222,285}
We see common letters are assigned to different numerical values. One “l” has been
assigned 546 and the other is given 657.
Now the code cannot be broken by studying
the frequency of numeric occurrences. In addition, different letters may be assigned the
same numerical value. The first 657 in
encrypt
came from the letter “o”, while the
second 657 came from “l”. Likewise, o
ne 285 is from “?” and the other 285 is actually a
“blank.”
Decryption
The person receiving the encrypted message must also know the encoding matrix
A
.
The received
string must first be re

converted into a matrix
Y
, that is then multiplied on
the right by
A
–
1
. The resulting product is put back into a string, and then converted
into its charac
ter code giving the plaintext message. We illustrate with the encrypted
message
encrypt
created above.
Dr. Neal, WKU
encrypt={471,407,479,1041,860,907,657,546,657,648,541,636,141,
81,255,318,301,348,

117,

130,51,

87,

109,79,222,175,232,9,

32,149,279,232,283,285,222
,285};
A={{3,2,3},{

6,

5,

4},{9,8,7}};size=n=3;
Do[Do[c[i,j]=encrypt[[n(i

1)+j]],
{j,1,n}],{i,1,Length[encrypt]/n}];
Y=Table[c[i,j],{i,1,Length[encrypt]/n},{j,1,n}];
V=Y.Inverse[A];MatrixForm[V]
decrypt=Flatten[V]
FromCharacterCode[decrypt]
De
ar colleague, what is 4% of 120?
Assignment
1. Pick a partner. Together decide on a square invertible coding matrix
A
. Adjust the
programs accordi
ngly with this matrix
A
.
2. Each student creates an encrypted message to send to the other. Print out the
program and outputs used to create your encrypted message. On it write "Message
from 'your name' to 'par
tner's name'."
3. Email your partner only your encrypted message. (Send the numbers enclosed in
braces and separated by commas.)
4. Check your email to obtain the encrypted message from your partner. Print out this
email.
5. Decrypt your rece
ived message. Print out the codes and outputs used to decrypt.
On it write, "Message from 'partner's name' decoded by 'your name'."
6. Turn in both print

outs along with the received email message.
Note
: If doing this in a computer lab, you can
first create your encrypted message with
Mathematica
. Then highlight and copy the final encrypted message output. Then use
email on the same computer to send it by simply pasting into the body of the message.
Likewise, you can check your email in the
lab, highlight and copy the received message,
then open your
Mathematica
file and paste it into the decryption codes. Otherwise, you
will have to type out the numbers when you send the email, and type them out again
when you decrypt.
Comments 0
Log in to post a comment