08-04-22.mw

> restart;
 

> with(StringTools):
 

> Alphabet:=cat(op(select( IsPrintable, [seq( convert([i],bytes), i=1..255)])));
 

 !?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~" align="center" border="0">
 !?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~" align="center" border="0">
 !?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~" align="center" border="0">
(1)
 

> length(Alphabet);
 

95 (2)
 

> StringToList := proc(text::string)
 global Alphabet;
 return(
  [seq(  SearchText(text[i], Alphabet)   ,i=1..length(text))] );
end:
 

> ListToString := proc( numlist :: list(posint))
 global Alphabet;
 cat(seq( Alphabet[numlist[i]], i=1..nops(numlist) ));
end:
 

>
 

> AffineEncode := proc( plain::string, shift::integer, a::integer)
 local nums,crypt, i;
 global Alphabet;
 nums := StringToList(plain);
 crypt := ListToString(   [ seq(
                              modp(a*(nums[i]-1) + shift, length(Alphabet)) +1,
                              i=1..nops(nums)) ] );
 return(crypt);
end:
 

> cat(AffineEncode("this is a string",5,3));
 

c?B`%B`%*%`c]BQ< (3)
 

> cat(AffineEncode("this is a string",0,1));
 

this is a string (4)
 

> cat(AffineEncode("this is a string",5,95));
 

%%%%%%%%%%%%%%%% (5)
 

> cat(AffineEncode("abcdefghijkl",4,19));
 

$7J]p$7J]p$7 (6)
 

> AffineEncode := proc( plain::string, shift::integer, a::integer)
 local nums,crypt, i;
 global Alphabet;
 if(gcd(a,length(Alphabet))>1) then
    error("The parameter %1 must be coprime with the length of the Alphabet %2",a, length(Alphabet));fi;
 nums := StringToList(plain);
 crypt := ListToString(   [ seq(
                              modp(a*(nums[i]-1) + shift, length(Alphabet)) +1,
                              i=1..nops(nums)) ] );
 return(crypt);
end:
 

> AffineEncode("test",5,4);
 

X{TX (7)
 

> AffineEncode("test",4,96);
 

xiwx (8)
 

> AffineEncode("test",0,96);
 

test (9)
 

> AffineDecode := proc( plain::string, shift::integer, a::integer)
 local nums,crypt, i, A, B;
 global Alphabet;
 A:= modp(1/a,length(Alphabet));
 B:= (-shift)*A;
 AffineEncode(plain,B,A);
end:
 

> aux:= AffineEncode("this is another test", 6,7);
 

8CJ1&J1&qmt8C.*&8.18 (10)
 

> AffineDecode(aux,6,7);
 

this is another test (11)
 

>
 

>
 

>
 

>
 

>