While none of the methods we have covered yet are secure enough to foil a
good cryptanalyst, the OneTimePad
procedure of
§5.2 is good enough for household use (unless you live
with cryptanalysts!). Should we want to really use it, however, we would
find it extremely tedious to type in our message each time, and then copy
the encrypted text from the worksheet.
Fortunately, maple is able to read and write data from a file, using
readline
and writeline
. These read (or write) a single line
of data from a specified file.3.16.
When reading from a file, readline
returns either a line of data, or
0 when the end of the file is reached.
Although it is not strictly necessary, it is a good idea to explicitly
open
the file before dealing with it, and close
it when you
are done. In addition to being good practice (like explicitly declaring
variables), it also allows you to specify certain options, like whether you
intend to read or write to the file. In fact, I recommend using
fopen
, which also allows you to specify whether the file should be
treated as text or binary.3.17
Below is a small example of a procedure which encrypts a file one line at a
time using a procedure EncryptLine
. We have never given such a
procedure, but it should be clear how to modify it to use your
favorite encryption routine instead.
> Cryptfile := proc(plainfilename,cryptfilename,key)
local line, cline, file, cryptf;
file :=fopen(plainfilename,READ,TEXT);
cryptf:=fopen(cryptfilename,WRITE,TEXT);
line :=readline(file);
while (line <> 0) do
writeline(cryptf,EncryptLine(line,key));
line :=readline(file);
od;
close(file);
close(cryptf);
end: