ASCII PADDING
by Marion Scheepers
This worksheet consists of ASCII conversion procedures like ASCIIPad(..,..) and ASCIIDepad(...).
> with(numtheory):
Warning, the protected name order has been redefined and unprotected
ASCIIPad(..,..)
First argument: A string of characters listed between ` and `. Second argument: A "large" natural number.
> ASCIIPad:=proc(Message,Modulus) local L, K, l, x, p, r, M, j, i, z; L:= length(Modulus); K:=convert(Message,bytes); l:=nops(K); x:=floor(L/4); ## This computes length of a packet. if ( l mod x = 0) then p:= l/x; ## This is the number of packets - Case 1. r:= 0; ## This keeps track of the case. else p:= floor(l/x)+1; ## This is the number of packets - Case 2. r:= 1; fi: if (r = 0) then for j from 1 to p do M:=0; ## This starts packet j's padded ascii for i from 1 to x do if (i < x) then M:= (M + (1000 + K[(j-1)*x+i]))*10000; else M:= (M + (1000 + K[j*x])); fi: od: printf("Packet number %d of the padded ASCII is %d\n",j,M); od: else if ( p = 1) then M:=0; for i from 1 to l do if (i < l) then M:= (M + (1000 + K[i]))*10000; else M:= (M + (1000 + K[i])); fi: od: printf("The padded ASCII version is %d \n",M); else for j from 1 to p do M:=0; if (j < p) then for i from 1 to x do if (i < x) then M:= (M + (1000 + K[(j-1)*x+i]))*10000; else M:= (M + (1000 + K[j*x])); fi: od: printf("Packet number %d of the padded ASCII is %d\n",j,M); else z := l mod x; for i from 1 to z do if (i < z) then M:= (M + (1000 + K[(p-1)*x+i]))*10000; else M:= (M + (1000 + K[(p-1)*x+z])); fi: od: printf("Packet number %d of the padded ASCII is %d\n",p,M); fi: od: fi: fi: end:
Example.
> Modulus:=nextprime(11^53); Message:=`Where have all the flowers gone?`; ASCIIPad(Message,Modulus);
Packet number 1 of the padded ASCII is 10871104110111141101103211041097111811011032109711081108
Packet number 2 of the padded ASCII is 10321116110411011032110211081111111911011114111510321103
Packet number 3 of the padded ASCII is 1111111011011063
>
ASCIIDepad(...)
Given a number, this checks to see if it is padded ASCII, and if so, converts it back to a list of ASCII values.
> ASCIIDepad:=proc(Expression) local l, M, j, N, X; l:=length(Expression); if (l mod 4 > 0) then printf(" This is not a padded ASCII string.\n"); else M:= Expression; for j from 1 to (l/4) do X[j]:=((M mod 10^(l-4*(j-1)) - (M mod 10^(l-4*j)))/(10^(l-4*j)))-1000; if (j = 1) then N:=[X[j]]; else N:=[op(N),X[j]]; fi: if( X[j] > 256 ) then printf(" This is not a padded ASCII string.\n"); break; fi: od: fi: RETURN(N); end:
Example
> X:=10321116110411011032110211081111111911011114111510321103; Z:=ASCIIDepad(X);
> convert(Z,bytes);