Cryptography Reference
In-Depth Information
The following Maple functions—which will be called from the higher-level func-
tions that carry out encryption and decryption of text strings—perform encryption
and decryption in
l n , i.e., when messages and keys are regarded as lists of elements
Z
of
Z n . The third parameter, language is used to allow the functions to deal with
different languages and different alphabets.
> evig := proc(keylist, textlist, language)
local l, alphabet, modulus;
l := nops(keylist);
alphabet := alph(language);
modulus := StringTools:-Length(alphabet);
[seq((textlist[i] + keylist[(i-1) mod l + 1]) mod modulus, i=1..nops(textlist))];
end proc:
dvig := proc(keylist, textlist, language)
evig(-keylist, textlist, language)
end proc:
We can now give the encryption and decryption algorithms and, since they are
very similar, we will use only one function for both tasks in order to save space. In
the following chapters we will see how such a function can be easily modified to
make it able to encrypt/decrypt messages given as files but for nowwe limit ourselves
to messages given as text strings. The function Vigenere has three required input
parameters, namely key (for the key given as aword over the alphabet), plaintext
(for the plaintext string) and action for the action to be performed. The last para-
meter accepts only two possible values which are either enc if encryption is to be
performed or dec for decryption (note that in this function, as will often be the case
with other functions, we include type declarations to specify the type that the argu-
ment values must have). There is also an optional keyword parameter (see Maple's
help for a description of these parameters) language , which is used to specify the
plaintext language (since now we are not using global variables as we did before).
The default value for this parameter is en (corresponding to the English language),
so that no argument needs to be passed to this parameter when dealing with English
text.
> Vigenere := proc(key::string, message::string, action::identical(enc, dec),
{language := en})
local alphabet, keycodes, messagecodes, act, resultcodes;
alphabet := alph(language);
keycodes := map(x -> SearchText(x,alphabet)-1, StringTools:-Explode(key));
messagecodes := map(x -> SearchText(x,alphabet)-1, StringTools:-Explode(message));
if action = enc then
act := evig
elif action = dec then
act := dvig
else "Incorrect action"
end if;
resultcodes := act(keycodes, messagecodes, language);
StringTools:-Implode(map(x -> alphabet[x+1], resultcodes))
end proc:
Example 1.5 Let us consider the plaintext and the key used in Example 1.4. Then
the ciphertext is obtained as follows:
> Vigenere("word", "vigenereexample", enc);
"rwxhjsihalrplzv"
Search WWH ::




Custom Search