Cryptography Reference
In-Depth Information
> bytestoblocks := proc(lis, length)
local l, m;
l:=[];
m := lis;
while length <= nops(m) do
l := [op(l), [op(1 .. length, m)]];
m := [op(length+1 .. nops(m), m)]
end do;
if m <> [] then
l := [op(l), m]
end if;
map(x -> ListTools:-DotProduct(x, [seq(256ˆi, i=0..nops(x)-1)]), l)
end proc:
We will also need a function that computes the integer logarithm (in other words,
the floor of the logarithm) of a positive integer to a given integer base. Maple has the
function ilog for this purpose, but this function uses real logarithms and sometimes
round-off errors appear when computing the integer logarithmof a large integer which
is close to a power of the base. Although it will be very unlikely that this gives an
erroneous result for the logarithm of a randomly chosen RSA modulus, it is also
straightforward to use the following function that computes the integer logarithm of
an integer by what essentially amounts to doing a base change. To use this function
to compute the integer logarithm of n in base 256 one just calls intlog[256](n) .
> intlog := proc(n::posint)
local k, i, b;
if type(procname, 'indexed') then
b := op(procname)
else
error "logarithm base not specified"
end if;
if not type(b, 'posint') then
error "the base must be a positive integer"
elif b = 1 then
error "the base must be greater than 1"
end if;
k:=n;
i:=0;
while b <= k do
k := iquo(k, b);
i:=i+1
end do;
i
end proc:
Next we give the plain RSA encryption function for text strings. There are two
required input parameters: publickey , for the RSA public key in the format given
by the function RSAKeyGen , and plaintext , for the plaintext given as a text
string. The final keyword parameter outputformat specifies in which format the
ciphertext will be written. The default value is hex , which gives a more compact way
of representing the ciphertext and, if another value is specified, the ciphertext will
be given either as a list of several decimal numbers or as a unique decimal number
if the plaintext is encrypted in a unique block.
> RSAEncrypt := proc(publickey::list, plaintext::string, outputformat::name := hex)
local pk, blocks, blocksize, ciphertext, hexsize;
if type(publickey[1], string) then
pk := convert (publickey, decimal, hex)
else
Search WWH ::




Custom Search