Maple routines

Here are some simple Maple routines which I used to support my efforts in this course. These routines are not meant to be "production-grade" software. They should be used carefully. Other high-level languages such as Mathematica would support similar routines.

Letters to numbers Numbers to letters Preparing RSA keys Some bitstream routines Computing the GAG number


Letters to numbers

Several times in the course I wanted a simple way to turn alphabetic strings into numbers. For example, this was used in exercises on Diffie-Hellman and RSA protocols. Here is a routine to do this chore.
A2N:=proc(input)
local zorch,L,ss,j_,base,num;
zorch:=0;
L:=length(input);
base:=1;num:=0;
for j_ from 1 to L do
	ss:=substring(input,L-j_+1..L-j_+1);
	if ss='a' then num:=01 elif
	ss='b' then num:=02 elif
	ss='c' then num:=03 elif
	ss='d' then num:=04 elif
	ss='e' then num:=05 elif
	ss='f' then num:=06 elif
	ss='g' then num:=07 elif
	ss='h' then num:=08 elif
	ss='i' then num:=09 elif
	ss='j' then num:=10 elif
	ss='k' then num:=11 elif
	ss='l' then num:=12 elif
	ss='m' then num:=13 elif
	ss='n' then num:=14 elif
	ss='o' then num:=15 elif
	ss='p' then num:=16 elif
	ss='q' then num:=17 elif
	ss='r' then num:=18 elif
	ss='s' then num:=19 elif
	ss='t' then num:=20 elif
	ss='u' then num:=21 elif
	ss='v' then num:=22 elif
	ss='w' then num:=23 elif
	ss='x' then num:=24 elif
	ss='y' then num:=25 elif
	ss='z' then num:=26 fi;
	zorch:=num*base+zorch;
	base:=100*base
	od;
RETURN(zorch);
end;
And here is how it could be used:
>A2N(zax);
                                260124

>A2N(azx);

                                12624
Note that when the first letter is "a", the output number should begin "01" but leading 0's aren't printed: an amateur's routine.

TOP


Numbers to letters

Here is the routine which changes correctly prepared numbers into strings of letters.
N2A:=proc(input)
local cut_down, ss,zorch,alph;
alph:=`abcdefghijklmnopqrstuvwxyz`;
cut_down:=input;zorch:=``;
while(type(cut_down,positive)) do
	ss:=irem(cut_down,100);
	cut_down:=(cut_down-ss)/100;
	zorch:=cat(substring(alph,ss..ss),zorch)
	od;
RETURN(zorch);
end;
Below are some simple uses of this routine. Note the spurious answers for bad input -- I've definitely not written "bulletproof" computer code!
>N2A(260124);
                                 zax

>N2A(12624);

                                 azx

>N2A(37);



>N2A(237);

                                  b

>N2A(9);

                                  i

TOP


Preparing RSA keys

These routines were used to create the RSA exercises.

TOP


Some bitstream routines

Preparation of some assignments and work in class is made easier with a few simple routines manipulating bitstreams.

TOP


Computing the GAG number

A handout in lecture 23 was designed to illustrate some of the pitfalls and benefits of authentication via hashing. A rather flawed algorithm produced the "GAG number" of a document. The routines below helped me compute the GAG number.