TypeConversions.mws

Conversions of Data Types

İMike May, S.J. 2002, maymk@slu.edu

>    restart:

This worksheet is focused on the preliminary work of conversion of data formats.  As we work with encryption, we will want to put messages in a number of different formats.  We first want to convert back and forth between normal text and strings of numbers.  For reasons that will become apparent later we will also want to be able to convert the numbers between decimal, binary, and hexadecimal formats.  To make ciphertext manageable we want to be able to concatenate the hex numbers into blocks.  Finally, since some codes use finite field arithmetic, we want to be able to convert binary strings to polynomials in alpha over Z2 and then to convert back.

Entering a message

To be able to look at specific examples, we start by creating a standard message that we will work with.

>    message1 :="AZaz Good morning Mr. Phelps.  Your mission for today,
should you choose to accept it, is to encode this message. AZaz";
message1;

message1 :=
message1 :=


(Notice that strings handle carriage returns as the special character "\n".  If we want to include the " symbol we use either a backslash quote \", or a double quote ''.)

The plain text starts and ends with the string AZaz to give easy clues for understanding the conversions.

>   

Conversions 1 - Letters to numbers and back again

For most of the encryption systems we will study this semester, we have to look at a preliminary task of converting the messages into some kind of numerical equivalent that we can apply our equations to.  

With the classical ciphers we have looked at we considered a simple conversion that let a be represented by the number 0, b by the number 1, and so on down to z.  This is easy to do by hand once we make a conversion table.  Unfortunately, that system has the drawback of not dealing with capitol and small letters or spaces and other special characters.  Instead we will used a conversion to ASCII.  ASCII is the standard conversion used for most current computer systems.

With Maple, we convert between alphanumeric strings and strings of integers with the convert command.  The syntax is
     convert(xxx, bytes);

The same command works both ways.

>    temp[1] := convert(message1, bytes);
temp[2] := convert(temp[1], bytes);

temp[1] := [65, 90, 97, 122, 32, 71, 111, 111, 100, 32, 109, 111, 114, 110, 105, 110, 103, 32, 77, 114, 46, 32, 80, 104, 101, 108, 112, 115, 46, 32, 32, 89, 111, 117, 114, 32, 109, 105, 115, 115, 105, ...
temp[1] := [65, 90, 97, 122, 32, 71, 111, 111, 100, 32, 109, 111, 114, 110, 105, 110, 103, 32, 77, 114, 46, 32, 80, 104, 101, 108, 112, 115, 46, 32, 32, 89, 111, 117, 114, 32, 109, 105, 115, 115, 105, ...
temp[1] := [65, 90, 97, 122, 32, 71, 111, 111, 100, 32, 109, 111, 114, 110, 105, 110, 103, 32, 77, 114, 46, 32, 80, 104, 101, 108, 112, 115, 46, 32, 32, 89, 111, 117, 114, 32, 109, 105, 115, 115, 105, ...
temp[1] := [65, 90, 97, 122, 32, 71, 111, 111, 100, 32, 109, 111, 114, 110, 105, 110, 103, 32, 77, 114, 46, 32, 80, 104, 101, 108, 112, 115, 46, 32, 32, 89, 111, 117, 114, 32, 109, 105, 115, 115, 105, ...

temp[2] :=
temp[2] :=

(Maple will convert from either a name (enclosed in back quotes `) or a string (enclosed in double quotes "), but it returns a string.  You must explicitly convert to type name if that is what we want.)

>   

We can see that the ASCII conversion takes A to 65, Z to 90, a to 97, z to 122, and a blank space to 32.  The letters between a and z are converted in order so that B is 66 and b is 98.

The convert command can also be used on an array of integers.

>    convert([65,255, 66, 256, 67, 257, 68], bytes);
convert([65,2,66, 1, 67, 0, 68, -2, 69],bytes);

A few things to notice:

1)  The convert function from integer to ASCII assumes the input is between 1 and 255, inclusive.  If fed an integer outside that range, the conversion routine stops.  In that case, it does not process the rest of the list.

2)  We only need 52 of the 255 places for English letters counting upper and lower case.  That leaves enough room for all the special characters and a lot of extra space as well.

>   

Exercises

1)  Produce a conversion chart for converting the lower case letters in the set {a, c, e, g, h, i, m, r, s, t}  to the ASCII equivalent.

>   

2)  Produce a conversion chart for converting the special characters in the string

`,.;' :"[]{}()!$%?` used for punctuation to ASCII.

>   

3)  Use your table to convert the string "this is a secret message" to ASCII.  Use Maple to check your work.

>   

4)  Type the first full paragraph on page 1 of your book and save it as message2.  Use Maple to convert message2 to the string of numbers corresponding to the ASCII equivalents.

>   

Warning - A bug in the Maple conversion routine

There is a bug in the Maple conversion routine that you should be aware of.  It does not affect the characters we normally use in typing, but it is worth noting anyway.  The bug is that Maple converts both 10 and 13 to carriage return, which is converted back to 13.

>    numstring := [seq(i,i=5..15)];
charstring := convert(numstring,bytes);
numstring2 := convert(charstring, bytes);

numstring := [5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]

charstring :=
charstring :=

numstring2 := [5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]

Conversions 2 - Other number systems (hexadecimal in particular)

For most math classes we do arithmetic in base 10.  The base was chosen by an accident of history.  (Look at your hands and identify the accident.)  When we deal with digital computers we often do binary arithmetic do to an accident of mechanics. (The accident is that transistors have two states, on and off.)  One of the disadvantages of binary is that the numbers quickly become too long.  That has led to the use of base 16, or hexadecimal.

The syntax used to convert a decimal number xxx to binary is

     convert(xxx, binary);

The command to convert to hexadecimal is similar,

     convert(xxx, hex);

There is a catch with these two commands.  We want to use them on an array of decimal numbers.  To do this we use the map command.  The commands to convert an array, XXX, of decimal numbers to binary and hexadecimal are respectively:

     map(convert, XXX, binary);

     map(convert, XXX, hex);

The commands to reverse the processes are:

     map(convert, XXX, decimal,  binary);

     map(convert, XXX, decimal, hex);

>    temp[3] := map(convert, temp[1], binary);
temp[4] := map(convert, temp[1], hex);
temp[5] := map(convert, temp[3], decimal, binary);
convert(temp[5], bytes);
temp[6] := map(convert, temp[4], decimal, hex);
convert(temp[6], bytes);

temp[3] := [1000001, 1011010, 1100001, 1111010, 100000, 1000111, 1101111, 1101111, 1100100, 100000, 1101101, 1101111, 1110010, 1101110, 1101001, 1101110, 1100111, 100000, 1001101, 1110010, 101110, 1000...
temp[3] := [1000001, 1011010, 1100001, 1111010, 100000, 1000111, 1101111, 1101111, 1100100, 100000, 1101101, 1101111, 1110010, 1101110, 1101001, 1101110, 1100111, 100000, 1001101, 1110010, 101110, 1000...
temp[3] := [1000001, 1011010, 1100001, 1111010, 100000, 1000111, 1101111, 1101111, 1100100, 100000, 1101101, 1101111, 1110010, 1101110, 1101001, 1101110, 1100111, 100000, 1001101, 1110010, 101110, 1000...
temp[3] := [1000001, 1011010, 1100001, 1111010, 100000, 1000111, 1101111, 1101111, 1100100, 100000, 1101101, 1101111, 1110010, 1101110, 1101001, 1101110, 1100111, 100000, 1001101, 1110010, 101110, 1000...
temp[3] := [1000001, 1011010, 1100001, 1111010, 100000, 1000111, 1101111, 1101111, 1100100, 100000, 1101101, 1101111, 1110010, 1101110, 1101001, 1101110, 1100111, 100000, 1001101, 1110010, 101110, 1000...
temp[3] := [1000001, 1011010, 1100001, 1111010, 100000, 1000111, 1101111, 1101111, 1100100, 100000, 1101101, 1101111, 1110010, 1101110, 1101001, 1101110, 1100111, 100000, 1001101, 1110010, 101110, 1000...
temp[3] := [1000001, 1011010, 1100001, 1111010, 100000, 1000111, 1101111, 1101111, 1100100, 100000, 1101101, 1101111, 1110010, 1101110, 1101001, 1101110, 1100111, 100000, 1001101, 1110010, 101110, 1000...
temp[3] := [1000001, 1011010, 1100001, 1111010, 100000, 1000111, 1101111, 1101111, 1100100, 100000, 1101101, 1101111, 1110010, 1101110, 1101001, 1101110, 1100111, 100000, 1001101, 1110010, 101110, 1000...

temp[4] := [`41`, `5A`, `61`, `7A`, `20`, `47`, `6F`, `6F`, `64`, `20`, `6D`, `6F`, `72`, `6E`, `69`, `6E`, `67`, `20`, `4D`, `72`, `2E`, `20`, `50`, `68`, `65`, `6C`, `70`, `73`, `2E`, `20`, `20`, `59...
temp[4] := [`41`, `5A`, `61`, `7A`, `20`, `47`, `6F`, `6F`, `64`, `20`, `6D`, `6F`, `72`, `6E`, `69`, `6E`, `67`, `20`, `4D`, `72`, `2E`, `20`, `50`, `68`, `65`, `6C`, `70`, `73`, `2E`, `20`, `20`, `59...
temp[4] := [`41`, `5A`, `61`, `7A`, `20`, `47`, `6F`, `6F`, `64`, `20`, `6D`, `6F`, `72`, `6E`, `69`, `6E`, `67`, `20`, `4D`, `72`, `2E`, `20`, `50`, `68`, `65`, `6C`, `70`, `73`, `2E`, `20`, `20`, `59...
temp[4] := [`41`, `5A`, `61`, `7A`, `20`, `47`, `6F`, `6F`, `64`, `20`, `6D`, `6F`, `72`, `6E`, `69`, `6E`, `67`, `20`, `4D`, `72`, `2E`, `20`, `50`, `68`, `65`, `6C`, `70`, `73`, `2E`, `20`, `20`, `59...

temp[5] := [65, 90, 97, 122, 32, 71, 111, 111, 100, 32, 109, 111, 114, 110, 105, 110, 103, 32, 77, 114, 46, 32, 80, 104, 101, 108, 112, 115, 46, 32, 32, 89, 111, 117, 114, 32, 109, 105, 115, 115, 105, ...
temp[5] := [65, 90, 97, 122, 32, 71, 111, 111, 100, 32, 109, 111, 114, 110, 105, 110, 103, 32, 77, 114, 46, 32, 80, 104, 101, 108, 112, 115, 46, 32, 32, 89, 111, 117, 114, 32, 109, 105, 115, 115, 105, ...
temp[5] := [65, 90, 97, 122, 32, 71, 111, 111, 100, 32, 109, 111, 114, 110, 105, 110, 103, 32, 77, 114, 46, 32, 80, 104, 101, 108, 112, 115, 46, 32, 32, 89, 111, 117, 114, 32, 109, 105, 115, 115, 105, ...
temp[5] := [65, 90, 97, 122, 32, 71, 111, 111, 100, 32, 109, 111, 114, 110, 105, 110, 103, 32, 77, 114, 46, 32, 80, 104, 101, 108, 112, 115, 46, 32, 32, 89, 111, 117, 114, 32, 109, 105, 115, 115, 105, ...


temp[6] := [65, 90, 97, 122, 32, 71, 111, 111, 100, 32, 109, 111, 114, 110, 105, 110, 103, 32, 77, 114, 46, 32, 80, 104, 101, 108, 112, 115, 46, 32, 32, 89, 111, 117, 114, 32, 109, 105, 115, 115, 105, ...
temp[6] := [65, 90, 97, 122, 32, 71, 111, 111, 100, 32, 109, 111, 114, 110, 105, 110, 103, 32, 77, 114, 46, 32, 80, 104, 101, 108, 112, 115, 46, 32, 32, 89, 111, 117, 114, 32, 109, 105, 115, 115, 105, ...
temp[6] := [65, 90, 97, 122, 32, 71, 111, 111, 100, 32, 109, 111, 114, 110, 105, 110, 103, 32, 77, 114, 46, 32, 80, 104, 101, 108, 112, 115, 46, 32, 32, 89, 111, 117, 114, 32, 109, 105, 115, 115, 105, ...
temp[6] := [65, 90, 97, 122, 32, 71, 111, 111, 100, 32, 109, 111, 114, 110, 105, 110, 103, 32, 77, 114, 46, 32, 80, 104, 101, 108, 112, 115, 46, 32, 32, 89, 111, 117, 114, 32, 109, 105, 115, 115, 105, ...


We want to make a small modification to hex.  Most ASCII characters convert to a two character hex string.  The usual convention of number writing drops leading zeroes.  For technical reasons we would like each character to be represented by a string of the same hex length.  Thus we introduce a procedure to include the leading zero if needed.

>    twodigithex := a -> substring(convert(256+a,hex),2..3):
temp[a4] := map(twodigithex, temp[1]);

temp[a4] := [`41`, `5A`, `61`, `7A`, `20`, `47`, `6F`, `6F`, `64`, `20`, `6D`, `6F`, `72`, `6E`, `69`, `6E`, `67`, `20`, `4D`, `72`, `2E`, `20`, `50`, `68`, `65`, `6C`, `70`, `73`, `2E`, `20`, `20`, `5...
temp[a4] := [`41`, `5A`, `61`, `7A`, `20`, `47`, `6F`, `6F`, `64`, `20`, `6D`, `6F`, `72`, `6E`, `69`, `6E`, `67`, `20`, `4D`, `72`, `2E`, `20`, `50`, `68`, `65`, `6C`, `70`, `73`, `2E`, `20`, `20`, `5...
temp[a4] := [`41`, `5A`, `61`, `7A`, `20`, `47`, `6F`, `6F`, `64`, `20`, `6D`, `6F`, `72`, `6E`, `69`, `6E`, `67`, `20`, `4D`, `72`, `2E`, `20`, `50`, `68`, `65`, `6C`, `70`, `73`, `2E`, `20`, `20`, `5...
temp[a4] := [`41`, `5A`, `61`, `7A`, `20`, `47`, `6F`, `6F`, `64`, `20`, `6D`, `6F`, `72`, `6E`, `69`, `6E`, `67`, `20`, `4D`, `72`, `2E`, `20`, `50`, `68`, `65`, `6C`, `70`, `73`, `2E`, `20`, `20`, `5...

Note that the two lists temp[4] and temp[a4] are almost the same, with the D in temp[4][56] replaced by 0D in temp[a4][56].)

Exercises:

5)  Covert message2 to an array of binary numbers.

>   

6)  Convert message2 to an array of two character hexadecimal numbers.

>   

Arranging letters into "words"

Using strings of numbers does give an easy first encryption.  (It is not very secure however.)   One of the problems with strings of numbers is that they are cumbersome to write.  The conversion of every ASCII character to a two character hex string lets us concatenate hex characters into words of uniform length.  We now define procedures for turning a vector of hexadecimal numbers into an array of blocks, with bsize numbers put into each block, and the last block padded out with zeroes.  We also define a procedure for undoing the blocking.

>    blocker := proc(hexvec,bsize)
local numCharacters, paddingNum, numWords, i, j, temp, hexblock;
temp := hexvec;
numCharacters :=linalg[vectdim](hexvec);
numWords := ceil(numCharacters/bsize);
paddingNum := numWords*bsize - numCharacters;
temp := [op(temp),seq(`00`,j = 1..paddingNum)];
hexblock :=  
   [seq( cat(seq(temp[bsize*i + j], j = 1..bsize)), i=0..numWords-1)]:
end:

>    numCharacters :=linalg[vectdim](temp[a4]);
bl4 := blocker(temp[a4],4);
bl7 := blocker(temp[a4],7);
bl11 := blocker(temp[a4],11);

numCharacters := 119

bl4 := [`415A617A`, `20476F6F`, `64206D6F`, `726E696E`, `67204D72`, `2E205068`, `656C7073`, `2E202059`, `6F757220`, `6D697373`, `696F6E20`, `666F7220`, `746F6461`, `792C200A`, `73686F75`, `6C642079`, `...
bl4 := [`415A617A`, `20476F6F`, `64206D6F`, `726E696E`, `67204D72`, `2E205068`, `656C7073`, `2E202059`, `6F757220`, `6D697373`, `696F6E20`, `666F7220`, `746F6461`, `792C200A`, `73686F75`, `6C642079`, `...
bl4 := [`415A617A`, `20476F6F`, `64206D6F`, `726E696E`, `67204D72`, `2E205068`, `656C7073`, `2E202059`, `6F757220`, `6D697373`, `696F6E20`, `666F7220`, `746F6461`, `792C200A`, `73686F75`, `6C642079`, `...

bl7 := [`415A617A20476F`, `6F64206D6F726E`, `696E67204D722E`, `205068656C7073`, `2E2020596F7572`, `206D697373696F`, `6E20666F722074`, `6F6461792C200A`, `73686F756C6420`, `796F752063686F`, `6F736520746F...
bl7 := [`415A617A20476F`, `6F64206D6F726E`, `696E67204D722E`, `205068656C7073`, `2E2020596F7572`, `206D697373696F`, `6E20666F722074`, `6F6461792C200A`, `73686F756C6420`, `796F752063686F`, `6F736520746F...
bl7 := [`415A617A20476F`, `6F64206D6F726E`, `696E67204D722E`, `205068656C7073`, `2E2020596F7572`, `206D697373696F`, `6E20666F722074`, `6F6461792C200A`, `73686F756C6420`, `796F752063686F`, `6F736520746F...

bl11 := [`415A617A20476F6F64206D`, `6F726E696E67204D722E20`, `5068656C70732E2020596F`, `7572206D697373696F6E20`, `666F7220746F6461792C20`, `0A73686F756C6420796F75`, `2063686F6F736520746F20`, `616363657...
bl11 := [`415A617A20476F6F64206D`, `6F726E696E67204D722E20`, `5068656C70732E2020596F`, `7572206D697373696F6E20`, `666F7220746F6461792C20`, `0A73686F756C6420796F75`, `2063686F6F736520746F20`, `616363657...
bl11 := [`415A617A20476F6F64206D`, `6F726E696E67204D722E20`, `5068656C70732E2020596F`, `7572206D697373696F6E20`, `666F7220746F6461792C20`, `0A73686F756C6420796F75`, `2063686F6F736520746F20`, `616363657...

Notice that blocks of size 4 require 1 more character, while blocks of size 7 come out even, and blocks of size 11 need 2 extra characters to make the blocks come out even.

>    unblocker := (hexblock,bsize) ->
   map(hexString -> seq(substring(hexString,2*j-1..2*j), j = 1..bsize),
      hexblock):

>    s4 := unblocker(bl4, 4);
s7 := unblocker(bl7, 7);
s11 := unblocker(bl11, 11);

s4 := [`41`, `5A`, `61`, `7A`, `20`, `47`, `6F`, `6F`, `64`, `20`, `6D`, `6F`, `72`, `6E`, `69`, `6E`, `67`, `20`, `4D`, `72`, `2E`, `20`, `50`, `68`, `65`, `6C`, `70`, `73`, `2E`, `20`, `20`, `59`, `6...
s4 := [`41`, `5A`, `61`, `7A`, `20`, `47`, `6F`, `6F`, `64`, `20`, `6D`, `6F`, `72`, `6E`, `69`, `6E`, `67`, `20`, `4D`, `72`, `2E`, `20`, `50`, `68`, `65`, `6C`, `70`, `73`, `2E`, `20`, `20`, `59`, `6...
s4 := [`41`, `5A`, `61`, `7A`, `20`, `47`, `6F`, `6F`, `64`, `20`, `6D`, `6F`, `72`, `6E`, `69`, `6E`, `67`, `20`, `4D`, `72`, `2E`, `20`, `50`, `68`, `65`, `6C`, `70`, `73`, `2E`, `20`, `20`, `59`, `6...
s4 := [`41`, `5A`, `61`, `7A`, `20`, `47`, `6F`, `6F`, `64`, `20`, `6D`, `6F`, `72`, `6E`, `69`, `6E`, `67`, `20`, `4D`, `72`, `2E`, `20`, `50`, `68`, `65`, `6C`, `70`, `73`, `2E`, `20`, `20`, `59`, `6...

s7 := [`41`, `5A`, `61`, `7A`, `20`, `47`, `6F`, `6F`, `64`, `20`, `6D`, `6F`, `72`, `6E`, `69`, `6E`, `67`, `20`, `4D`, `72`, `2E`, `20`, `50`, `68`, `65`, `6C`, `70`, `73`, `2E`, `20`, `20`, `59`, `6...
s7 := [`41`, `5A`, `61`, `7A`, `20`, `47`, `6F`, `6F`, `64`, `20`, `6D`, `6F`, `72`, `6E`, `69`, `6E`, `67`, `20`, `4D`, `72`, `2E`, `20`, `50`, `68`, `65`, `6C`, `70`, `73`, `2E`, `20`, `20`, `59`, `6...
s7 := [`41`, `5A`, `61`, `7A`, `20`, `47`, `6F`, `6F`, `64`, `20`, `6D`, `6F`, `72`, `6E`, `69`, `6E`, `67`, `20`, `4D`, `72`, `2E`, `20`, `50`, `68`, `65`, `6C`, `70`, `73`, `2E`, `20`, `20`, `59`, `6...
s7 := [`41`, `5A`, `61`, `7A`, `20`, `47`, `6F`, `6F`, `64`, `20`, `6D`, `6F`, `72`, `6E`, `69`, `6E`, `67`, `20`, `4D`, `72`, `2E`, `20`, `50`, `68`, `65`, `6C`, `70`, `73`, `2E`, `20`, `20`, `59`, `6...

s11 := [`41`, `5A`, `61`, `7A`, `20`, `47`, `6F`, `6F`, `64`, `20`, `6D`, `6F`, `72`, `6E`, `69`, `6E`, `67`, `20`, `4D`, `72`, `2E`, `20`, `50`, `68`, `65`, `6C`, `70`, `73`, `2E`, `20`, `20`, `59`, `...
s11 := [`41`, `5A`, `61`, `7A`, `20`, `47`, `6F`, `6F`, `64`, `20`, `6D`, `6F`, `72`, `6E`, `69`, `6E`, `67`, `20`, `4D`, `72`, `2E`, `20`, `50`, `68`, `65`, `6C`, `70`, `73`, `2E`, `20`, `20`, `59`, `...
s11 := [`41`, `5A`, `61`, `7A`, `20`, `47`, `6F`, `6F`, `64`, `20`, `6D`, `6F`, `72`, `6E`, `69`, `6E`, `67`, `20`, `4D`, `72`, `2E`, `20`, `50`, `68`, `65`, `6C`, `70`, `73`, `2E`, `20`, `20`, `59`, `...
s11 := [`41`, `5A`, `61`, `7A`, `20`, `47`, `6F`, `6F`, `64`, `20`, `6D`, `6F`, `72`, `6E`, `69`, `6E`, `67`, `20`, `4D`, `72`, `2E`, `20`, `50`, `68`, `65`, `6C`, `70`, `73`, `2E`, `20`, `20`, `59`, `...

It is worthwhile to convert from blocks all the way back to the original message.

>    s7 := unblocker(bl7, 7);
b7 := map(convert, s7, decimal, hex);
convert(b7, bytes);

s7 := [`41`, `5A`, `61`, `7A`, `20`, `47`, `6F`, `6F`, `64`, `20`, `6D`, `6F`, `72`, `6E`, `69`, `6E`, `67`, `20`, `4D`, `72`, `2E`, `20`, `50`, `68`, `65`, `6C`, `70`, `73`, `2E`, `20`, `20`, `59`, `6...
s7 := [`41`, `5A`, `61`, `7A`, `20`, `47`, `6F`, `6F`, `64`, `20`, `6D`, `6F`, `72`, `6E`, `69`, `6E`, `67`, `20`, `4D`, `72`, `2E`, `20`, `50`, `68`, `65`, `6C`, `70`, `73`, `2E`, `20`, `20`, `59`, `6...
s7 := [`41`, `5A`, `61`, `7A`, `20`, `47`, `6F`, `6F`, `64`, `20`, `6D`, `6F`, `72`, `6E`, `69`, `6E`, `67`, `20`, `4D`, `72`, `2E`, `20`, `50`, `68`, `65`, `6C`, `70`, `73`, `2E`, `20`, `20`, `59`, `6...
s7 := [`41`, `5A`, `61`, `7A`, `20`, `47`, `6F`, `6F`, `64`, `20`, `6D`, `6F`, `72`, `6E`, `69`, `6E`, `67`, `20`, `4D`, `72`, `2E`, `20`, `50`, `68`, `65`, `6C`, `70`, `73`, `2E`, `20`, `20`, `59`, `6...

b7 := [65, 90, 97, 122, 32, 71, 111, 111, 100, 32, 109, 111, 114, 110, 105, 110, 103, 32, 77, 114, 46, 32, 80, 104, 101, 108, 112, 115, 46, 32, 32, 89, 111, 117, 114, 32, 109, 105, 115, 115, 105, 111, ...
b7 := [65, 90, 97, 122, 32, 71, 111, 111, 100, 32, 109, 111, 114, 110, 105, 110, 103, 32, 77, 114, 46, 32, 80, 104, 101, 108, 112, 115, 46, 32, 32, 89, 111, 117, 114, 32, 109, 105, 115, 115, 105, 111, ...
b7 := [65, 90, 97, 122, 32, 71, 111, 111, 100, 32, 109, 111, 114, 110, 105, 110, 103, 32, 77, 114, 46, 32, 80, 104, 101, 108, 112, 115, 46, 32, 32, 89, 111, 117, 114, 32, 109, 105, 115, 115, 105, 111, ...
b7 := [65, 90, 97, 122, 32, 71, 111, 111, 100, 32, 109, 111, 114, 110, 105, 110, 103, 32, 77, 114, 46, 32, 80, 104, 101, 108, 112, 115, 46, 32, 32, 89, 111, 117, 114, 32, 109, 105, 115, 115, 105, 111, ...


It should be noted that all four steps can be done in one line.

>    convert(map(convert, unblocker(bl7,7), decimal, hex), bytes);


Exercises

7)  Convert message2 into hex blocks with 5 characters going into each word.  (The words in your answer should appear to be 10 letters long.)

>   

8)  Convert the message  

[`415A617A20`, `5468652072`, `61696E2069`, `6E20537061`, `696E206661`, `6C6C73206D`, `61696E6C79`, `20696E2074`, `686520706C`, `61696E2E20`, `415A617A00`]

back to plaintext from hex blocks.

>   

>   

Conversions 3 - Binary numbers and finite fields.

A number of commercial codes, including AES, include steps where binary numbers are viewed as numbers in finite fields and combined with the arithmetic in the finite field.  For this worksheet we simply are concerned with the conversion routines.  The conversion is done by considering the digits of the binary number as the coefficients on a polynomial in the variable alpha.  The coefficients can be either 0 or 1 and are listed highest power first.

>    binPoly := proc(binNum)
   local binString, temp, stringLen, i:
   binString := convert(binNum, string):
   temp := 0:
   stringLen := length(binString):
   for i from 1 to stringLen do
      temp := temp +
        alpha^(stringLen-i)*parse(substring(binString,i)); od:
   sort(temp);
end:

>    polya := binPoly(1000110001111);

polya := alpha^12+alpha^8+alpha^7+alpha^3+alpha^2+alpha+1

To convert back, we simply evaluate the polynomial with alpha equaling 2, then convert to a binary representation.

>    polyBin := poly -> convert(subs(alpha=2, poly), binary):

>    polyBin(polya);

1000110001111

Exercise;

9)  Convert the integers 120 -130 to their binary representations and then into their representations as polynomials over Z2.

>   

>