> | restart; |
> | with(StringTools): |
> | Alphabet:=cat(op(select( IsPrintable, [seq( convert([i],bytes), i=1..255)]))); |
![]() ![]() ![]() |
(1) |
> | length(Alphabet); |
![]() |
(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)); |
![]() |
(3) |
> | cat(AffineEncode("this is a string",0,1)); |
![]() |
(4) |
> | cat(AffineEncode("this is a string",5,95)); |
![]() |
(5) |
> | cat(AffineEncode("abcdefghijkl",4,19)); |
![]() |
(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); |
![]() |
(7) |
> | AffineEncode("test",4,96); |
![]() |
(8) |
> | AffineEncode("test",0,96); |
![]() |
(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); |
![]() |
(10) |
> | AffineDecode(aux,6,7); |
![]() |
(11) |
> |
> |
> |
> |
> |