You are on page 1of 4

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 2 N 13 B 1 O 16 C 4 P 15 D 3 Q 18 E 6 R 17 F 5 S 20 G 8 T 19 H 7 U 22 I 10 V 21 J 9 W 24 K 12 X 23 L 11 Y 26 M 14 Z 25

Coding Matrix Next, we choose an invertible n n matrix A , for some size n , which will be used to encrypt the message. Here we shall use

3 2 3 A= . 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: 7 10 0 19 7 6 B = 17 6 0 . 1 22 3 3 26 0 Here, B is 5 3. Now we multiply: C = BA .

Dr. Neal, WKU

7 10 0 39 36 19 19 7 6 3 69 51 71 2 3 15 4 27 . C = BA = 17 6 0 = 6 5 4 1 22 3 9 8 7 102 84 64 3 26 0 147 124 95


Finally, we convert matrix C into a string that becomes the encrypted message: 39 H 36 I 19 69 51 71 E 15 R 4 E 27 102 B 84 U 64 D 147 D 124 Y 95

T H

Notice that H has been sent to two different values. The first H has been encrypted as 39, and the second H has been encrypted as 51. Likewise the three blanks are assigned different values, as are the two Es and the two Ds. 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 1 multiplied by A to undo the original matrix multiplication. Because C = BA , we obtain the original matrix B by B = CA1, 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 alphabetic 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 letters, but also for the symbols % and ?, and for numbers. Rather than trying to define our own alphanumeric 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 will 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,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} 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, one 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 character 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] Dear colleague, what is 4% of 120? Assignment 1. Pick a partner. Together decide on a square invertible coding matrix A . Adjust the programs accordingly 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 'partner'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 received 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.

You might also like