Encryption & Decryption

To encrypt a message we need a method of turning a string of text into a number. We begin by ordering the alphabetic and the other key-board symbols, with a  followed by b , b  by c , etc:

>    `crypt/alphabet` :=
   `abcdefghijklmnopqrstuvwxyz`
   ||`ABCDEFGHIJKLMNOPQRSTUVWXYZ`
   ||```1234567890-=~!@#$%^&*()_+`
   ||` ,./<>?;':"[]{}|    `:

The symbols || means concatenate in Maple Version 6 and later versions. I am endebted to Professor John Cosgrave for the following function  (and its inverse) that turns a string of text into a number. (See Professor Cosgrave's home page at: www.spd.dcu.ie/johnbcos/.)

>    to_number := proc(st, string)
local ll, nn, ss, ii;  
ll := length(st);
if ll = 0 then RETURN(0) fi;
nn := 1;
for ii to ll do
ss := SearchText(substring(st, ii .. ii),
                 `crypt/alphabet`);
nn := 100*nn + ss
od;
nn - 10^(2*ll)
end:

Next we do some examples, where the text needs to be included within backward quotes.

>    N:=to_number(`I came I saw I conquered.`);

N := 35790301130579357919012379357903151417210518050481

The initial 35 represents the letter `I', the  79 is the blank, the 03 is the letter `c', and 81 is the period at the end.  Next we need a program to recover a string of text from the number.

>    from_number := proc(nn, integer)
local ss, mm, ll, pp, ii, ans; mm := nn;
ll := floor(1/2*trunc(evalf(log10(mm))))+1;
ans := ``; for ii to ll do mm := mm/100;
pp := 100*frac(mm);
ss := substring(`crypt/alphabet`, pp..pp);
ans := cat(ss, ans); mm := trunc(mm)
od; ans end:

>    from_number(N);

`I came I saw I conquered.`

It should be noted that if we use the above function on a randomly chosen number we will get gobbledegook .

>    R:=rand(10^85)();

R := 1321110693270343633073697474256143563558458718976746753830538032062222085722974121768

>    from_number(%);

`aFkj#F$HJGgJ UP3nI02>5%

Next we generate public and private keys for our friends Bob and Alice who wish to communicate privately. First for Alice and then Bob.

>    p[a]:=nextprime(rand(10^60)());

p[a] := 745580037409259811952655310075487163797179490457039169594271

>    q[a]:=nextprime(rand(10^70)());

q[a] := 8430571674960498834085812920457916453747019461644031395307920624947429

>    n[a]:=p[a]*q[a]; # n[a]  is public but not the p[a]  and q[a] .

n[a] := 6285665944798494872603873316836884759607840180663917600572231099828615995603171520804206836221325481322102449896176916554634579259
n[a] := 6285665944798494872603873316836884759607840180663917600572231099828615995603171520804206836221325481322102449896176916554634579259

>    length(%);

130

>    e[a]:=nextprime(12345678987654321);# e[a]  is also public.

e[a] := 12345678987654373

>    phi_na:=(p[a]-1)*(q[a]-1); # This number   phi(n[a])  is kept secret.

phi_na := 6285665944798494872603873316836884759607840180663917600572222669256940289524300025731474425649561658815477008685291151594840037560
phi_na := 6285665944798494872603873316836884759607840180663917600572222669256940289524300025731474425649561658815477008685291151594840037560

>    igcd(e[a],phi_na);

1

>    d[a]:=e[a]^(-1) mod phi_na; # d[a]  is very, very private.

d[a] := 5768489995375390499902787581243594928087609756325935364916262082951362588325033962209331678057868187444393091375042959415342663797
d[a] := 5768489995375390499902787581243594928087609756325935364916262082951362588325033962209331678057868187444393091375042959415342663797

>    e[a]*d[a] mod phi_na;

1

>    p[b]:=nextprime(rand(10^58)());

p[b] := 1053530086146486307198155590763466429392673709525428511059

>    q[b]:=nextprime(rand(10^68)());

q[b] := 608981219760099374675982933766845473509473676470788342281338779533

>    n[b]:=p[b]*q[b];

n[b] := 641580036915449803606619939060629944682665994575099264085432592878419186611445591558672040495653618294575088108180453355447
n[b] := 641580036915449803606619939060629944682665994575099264085432592878419186611445591558672040495653618294575088108180453355447

>    length(%);

123

>    e[b]:=nextprime(987654321234567);

e[b] := 987654321234587

>    phi_nb:=(p[b]-1)*(q[b]-1);

phi_nb := 641580036915449803606619939060629944682665994575099264084823611657605557150623122317707039431380678188711626056373686064856
phi_nb := 641580036915449803606619939060629944682665994575099264084823611657605557150623122317707039431380678188711626056373686064856

>    d[b]:=e[b]^(-1) mod phi_nb;

d[b] := 457148273991491641998132212087983297952051836384399517592475904715652044164128229513218802330002070511453446604428380797851
d[b] := 457148273991491641998132212087983297952051836384399517592475904715652044164128229513218802330002070511453446604428380797851

Now we are ready for Bob to send a message to Alice. He first uses his secret exponent d[b]  to sign his message before using Alice's public exponent e[a]  to encrypt the message. First the message M .

>    M:=to_number(`Meet me at Fog City at 8:00pm. I am madly in love with you.`);

M := 3905052079130579012079321507792909202579012079618863631613817935790113791301041225790914791215220579230920087925152181
M := 3905052079130579012079321507792909202579012079618863631613817935790113791301041225790914791215220579230920087925152181

>    S:=M&^d[b] mod n[b];

S := 20956626685658682364317327239855652731439182643230914063022153955236619772199770294131346304505821150199808072949016950658
S := 20956626685658682364317327239855652731439182643230914063022153955236619772199770294131346304505821150199808072949016950658

>    C:=S&^e[a] mod n[a];

C := 530887571056149976302519883558961999631419551671988686873087418861743895696401885116066249122201805962784326343702466977376905013
C := 530887571056149976302519883558961999631419551671988686873087418861743895696401885116066249122201805962784326343702466977376905013

Alice receives the message C  from Bob and uses her secret exponent d[a]   to decrypt. Then when she realizes that the message does not make sense and that the message is supposed to come from Bob, she uses Bob's public exponent e[b]   to retrieve the message M .

>    C&^d[a] mod n[a];

20956626685658682364317327239855652731439182643230914063022153955236619772199770294131346304505821150199808072949016950658
20956626685658682364317327239855652731439182643230914063022153955236619772199770294131346304505821150199808072949016950658

>    from_number(%);

`t ~z@35@w-E&Aw 2=AEQ]/-FD]N0bu\` ZJ8 ^s $COEH0dX5uoa,,^|[p f5`

>    %%&^e[b] mod n[b];

3905052079130579012079321507792909202579012079618863631613817935790113791301041225790914791215220579230920087925152181
3905052079130579012079321507792909202579012079618863631613817935790113791301041225790914791215220579230920087925152181

>    from_number(%);

`Meet me at Fog City at 8:00pm. I am madly in love with you.`

>   

So Alice is now sure this message comes from Bob. But, will she meet him???