Cryptography Reference
In-Depth Information
l := nops(m)
else
error "Unrecognized action"
end if;
k := stringposint(key);
blist := BBSByteGen(k, l, ':-bbslength' = bbslength);
m := zip(BitXor, m, blist);
if action = 'encrypt' then
bytestohexstring(m)
else
convert(m, bytes)
end if
end proc:
For example, given the following 128-bit hex key and the following message:
> k := "cf59d8aa3278f823bfea7e31546b6ccb":
m := "This is a message to test the implementation of the Pseudo-Random
One-Time Pad":
we may use
POTP
to encrypt the message and to recover it from the ciphertext as
follows:
> c := POTP(k, m);
"761bece82e6ac923faa546fa84241469caf5988b1a09a4a40798a816b02709102941f95e84ae9bde7\
41fad908331aaf81546a80feb3742c1ec2bf91322bc3c356b034d585e26d4cd32f9f2853902"
> POTP(k, c, decrypt);
"This is a message to test the implementation of the Pseudo-Random One-Time Pad"
The function
POTP
deals only with messages given as text strings but we can
easily modify it to deal with files by using the procedures
filetobytes
and
bytestofile
defined in Appendix A. The function is actually simpler because in
this case we do not have to worry about the different formats that the message may
have: we will treat it always as a binary file and the resulting ciphertext/plaintext will
also be written to a file. Because of this, the operations of encryption and decryption
are exactly the same, so we do not have to tell the function whether we are encrypting
or decrypting.
> POTPF := proc(key::{posint, string}, message::string, filename::string,
filecheck::truefalse, {bbslength::{512, 768, 1024} := 1024})
local k, l, m, blist;
k := stringposint(key);
m := filetobytes(message);
l := nops(m);
blist := BBSByteGen(k, l, ':-bbslength' = bbslength);
bytestofile(zip(BitXor, m, blist), filename)
end proc:
Example 3.5
We create a file named
testfile
in the current directory, we
encrypt it with
POTPF
using the previously defined key
k
and call the ciphertext
ctestfile
. Then we decrypt it to a file named
dtestfile
and we read this file
to check that it contains the original message: