Cryptography With Matrices


An elementary encryption method is a direct letter to number substitution. For example, let 1 denote "space", then a -> 3, b -> 2, c -> 5, d -> 4, e -> 7, f -> 6, and so on. We implement this substitution with two commands. The first sets the code, the second stores the alphabet in the order of the code, with the first element "1" used for the "space".

To use the codes that follow, open a new Mathematica file, then type and enter the commands. You can also highlight and copy the lines from this page and then paste them into an open Mathematica page.

a=3;b=2;c=5;d=4;e=7;f=6;g=9;h=8;i=11;j=10;k=13;l=12; m=15;n=14;o=17;p=16;q=19;r=18;s=21;t=20;u=23;v=22;w=25;x=24;y=27;z=26;

alpha={1,b,a,d,c,f,e,h,g,j,i,l,k,n,m,p,o,r,q,t,s,v,u,x,w,z,y};


Now we shall type the message "Hi there buddy". The words are separated by 1's which represent "space." We must use lower case letters, separated by commas, enclosed in braces. We call the message script.

script={h,i,1,t,h,e,r,e,1,b,u,d,d,y}
{8, 11, 1, 20, 8, 7, 18, 7, 1, 2, 23, 4, 4, 27}


Notice the direct substitution in the output. Both h's became 8, both d's became 4. If many messages are intercepted, then the code can be broken by using the relative frequencies of letter occurrences in the English language.

We shall now use an invertible square matrix A to encode the message. In this case, we simply make up a 3x3 matrix A. We enter the matrix A and its dimension with one command. Then we verify that A is invertible by checking to see that its determinant is non-zero.

A={{3,2,3},{-6,-5,-4},{9,8,7}};size=3;

Det[A]
-6


Next, we must convert the message script into an mx3 matrix. The rows must have 3 columns since that is the size of matrix A. To do this, the message length must be a multiple of 3. We can check whether or not it is.

Length[script]
14


Since the length is not a multiple of 3, we add additional "space" at the end. Then we can check to see if the length is evenly divisible by 3. This quotient will also give the number of rows m in the matrix.

script={h,i,1,t,h,e,r,e,1,b,u,d,d,y,1}
{8, 11, 1, 20, 8, 7, 18, 7, 1, 2, 23, 4, 4, 27,1}

Length[script]/3
5


The next two commands convert script to an mx3 matrix B.

Do[Do[mat[i,j]=script[[3(i-1)+j]],{j,1,size}],{i,1,Length[script]/size}]

B=Table[mat[i,j],{i,1,Length[script]/size},{j,1,size}]
{{8, 11, 1}, {20, 8, 7}, {18, 7, 1}, {2, 23, 4}, {4, 27, 1}}


Now we encode the message into a matrix Z by multiplying B.A .

Z=B.A
{{-33, -31, -13}, {75, 56, 77}, {21, 9, 33}, {-96, -79, -58}, {-141, -119, -89}}


Finally, we convert Z back to a string called encrypt with the next two commands. The string encrypt is the encrypted message that one transmits.

Do[Do[code[3(i-1)+j]=Z[[i]][[j]],{j,1,size}],{i,1,Length[script]/size}]

encrypt=Table[code[i],{i,1,Length[script]}]
{-33, -31, -13, 75, 56, 77, 21, 9, 33, -96, -79, -58, -141,-119, -89}

Notice that the two h's are now assigned different numbers. The first goes to -33, the next goes to 56. One d goes to -58, the other to -141.


Decryption of Message


The receiver of the encrypted message must type it in along with the coding matrix A and its size.

encrypt={-33,-31,-13,75,56,77,21,9,33,-96,-79,-58,-141,-119,-89};

A={{3,2,3},{-6,-5,-4},{9,8,7}};size=3;


Then the encoded message encrypt must be put into an appropriately sized matrix Z.

Do[Do[demat[i,j]=encrypt[[3(i-1) j]],{j,1,size}],{i,1,Length[encrypt]/size}];

Z=Table[demat[i,j],{i,1,Length[encrypt]/size},{j,1,size}]
{{-33, -31, -13}, {75, 56, 77}, {21, 9, 33},
{-96, -79, -58}, {-141, -119, -89}}


Next the matrix Z is initially decoded by multiplying on the right by the inverse of A yielding the matrix Y.

Y=Z.Inverse[A]
{{8, 11, 1}, {20, 8, 7}, {18, 7, 1}, {2, 23, 4}, {4, 27, 1}}


Then the matrix Y is converted back to a string descript.

Do[Do[decode[3(i-1)+j]=Y[[i]][[j]],{j,1,size}], {i,1,Length[encrypt]/size}];

descript=Table[decode[i],{i,1,Length[encrypt]}]
{8, 11, 1, 20, 8, 7, 18, 7, 1, 2, 23, 4, 4, 27, 1}


Next, any numeric values stored to the alphabet symbols are cleared and the alphabet code is entered.

Clear[a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z];

alpha={1,b,a,d,c,f,e,h,g,j,i,l,k,n,m,p,o,r,q,t,s,v,u,x,w,z,y};


Finally, the string descript is converted back to letters yielding the decrypted message decrypt.

Do[x[tag]=alpha[[descript[[tag]]]],{tag,1,Length[descript]}];

decrypt=Table[x[tag],{tag,1,Length[descript]}]
{h, i, 1, t, h, e, r, e, 1, b, u, d, d, y, 1}


Assignment

1. Pick a partner. Together decide on an alphabet code and a square coding matrix A.

2. Both create an encrypted message to send to the other.

3. Print out the work involved in encrypting your message. On it type, "Message from 'your name' to 'partner's name'."

4. Give your encrypted message to your partner on a separate piece of paper.

5. Both decrypt the received message.

6. Print out the work involved in decrypting your received message. On it type, "Message from 'partner's name' decoded by 'your name'."

7. Turn in both printouts along with the encrypted message received on paper.


This material was written by David K. Neal, Department of Mathematics, Western Kentucky University, Bowling Green, KY 42101 : nealdk@wku.edu


Return to WKU Math Department.

Return to Western Online.