Converting strings to/from hex for use with AES
Math 202 Cryptography
September 24, 2018

T. Ratliff, Wheaton College
ratliff_thomas@wheatoncollege.edu

Defining the functions

stringTohexstring[str] will convert a 16-character string x to a 32-digit hexadecimal string representing the ASCII encoding of x
hexstringTostring[str] performs the opposite conversion

stringTohexstring[str_]:= Module[ {total,i,list},
If[StringLength[str]≠16, Return["** ERROR ** String must be exactly 16 characters long"]];
Return[IntegerString[Total[Flatten[Table[ToCharacterCode[StringTake[str,{i+1}]]*16^(2*(15-i)),{i,0,15}]]],16]
]];

hexstringTostring[h_]:=Module[{i},
Return[FromCharacterCode[Table[FromDigits[StringTake[h,{2i+1,2i+2}],16],{i,0,15}]]]
]

The stringTohexstring[ ] function will complain if your string is not the right length

stringTohexstring["This string is too long since it has more than 16 characters"]

stringTohexstring["Too short"]

stringTohexstring["Just right      "]

The hexstringTostring[ ] will convert a string of hex values to the corresponding ASCII characters

hexstringTostring["4a757374207269676874202020202020"]

In-class work
Use AES emulator http://www.cryptogrium.com/aes-encryption-online-ecb.html

Use a key of k=616e6420696e20393837363534333231 for today.

1. Decrypt the ciphertext y=0f471d1d16840863f482c9c07fd3dca2
	What is the message?

2. Pick your own message that is 16 characters long, convert your message to hex, encrypt it using AES, and post your result to the onCourse forum for today. 

3. Decrypt some of the messages posted by others.