Teacher tools for Classical cryptosystems\302\251Mike May, S. J., 2007, Saint Louis University, maymk@slu.eduA worksheet that collects tools used in the worksheets on classical systems so a teacher would have them near at hand for creating problems, test questions, and answer keys.Type conversionrestart:with(StringTools):charToBinByte := proc(character)
local binChar:
binChar := convert(op(convert(character, bytes))+256,binary):
substring(convert(binChar,string),2..9):
end:
binToHexByte := binString ->
substring(convert(convert(parse(binString),decimal,binary)+256,
hex),2..3):
hexToBinByte := hexString ->
substring(convert(convert(convert(hexString, decimal,hex)+256,
binary), string),2..9):
binByteToChar := binString ->
convert([convert(binString,decimal,binary)],bytes):
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 := array(1..numWords,
[seq( cat(seq(temp[bsize*i + j], j = 1..bsize)),
i=0..numWords-1)]):
convert(hexblock,list):
end:
unblocker := (hexblock,bsize) ->
map(hexString -> seq(substring(hexString,2*j-1..2*j),
j = 1..bsize), hexblock):stringToHexWordList := (string, blocksize) ->
blocker(map(binToHexByte, map(charToBinByte, Explode(string))), blocksize):mess1 := "AZaz Good morning Mr. Phelps. Your job today, should you choose
to accept it, is to learn to use one time pad stream ciphers. AZaz";
hexWordList := stringToHexWordList(mess1,5);hexWordListToString := (hexList, blocksize) ->
Implode(map(binByteToChar, map(hexToBinByte,
unblocker(hexwordlist1, blocksize)))):mess2 := hexWordListToString(hexWordList, 5);Vigenererestart:encodevigenere := proc(message, key, klength)
#This procedure uses the vigenere cipher to encode a string
#key should be a list of integers from 0 to 25
#message is an ASCII string. klength is the length of the key
local temp1, messageLength, position, counter:
#first convert the message to numerical equivalents
temp1 := convert(convert(message, bytes),array):
#then uses vigenere to scrable the letters.
# c1 is the number of characters in the message.
messageLength:= linalg[vectdim](temp1):
# counter is the number of letters converted
counter := 0:
for position from 1 to messageLength do
if (temp1[position] > 64) and (temp1[position] < 91) then
counter := (counter mod klength) + 1:
temp1[position] := 65 + ((temp1[position] - 65 + key[counter]) mod 26):
fi:
if (temp1[position] > 96) and (temp1[position] < 123) then
counter := (counter mod klength) + 1:
temp1[position] := 97 + ((temp1[position] - 97 + key[counter]) mod 26):
fi:
od:
#then convert back to the ASCII code
convert(convert(temp1,list), bytes):
end:wordtoekey := x -> map(a -> a-97, convert(x,bytes)):
wordtodkey := x -> map(a -> 123-a,convert(x,bytes)):vigenerekey := wordtoekey("zsedc");
keylength := nops(vigenerekey);message :=
"There are two kinds of cryptography in this world: cryptography that will stop your kid sister from reading your files, and cryptography that will stop major govenrmanets from reading your files. This book is about the latter.
If I take a letter, lock it in a safe, hide the safe somewhere in New York, then tell you to read the letter, that's not security. That's obscurity. On the other hand, if I take a letter and lock it in a safe, and then give you the safe along with the design specifications of the safe and a hundred identical safes with their combinations so that you and the world's best safecrackers can study the locking mechanism - and you still can't open the safe and read the letter - that's security";ciphertext1 := encodevigenere(message, vigenerekey, keylength);decryptkey := wordtodkey("zsedc");
encodevigenere(ciphertext1, decryptkey, keylength);LUklbXJvd0c2Iy9JK21vZHVsZW5hbWVHNiJJLFR5cGVzZXR0aW5nR0koX3N5c2xpYkdGJzYjLUkjbWlHRiQ2I1EhRic=Legal Notice: The copyright for this application is owned by the author(s). Neither Maplesoft nor the author are responsible for any errors contained within and are not liable for any damages resulting from the use of this material. This application is intended for non-commercial, non-profit use only. Contact the author for permission if you wish to use this application in for-profit activities.