Cryptography Reference
In-Depth Information
length corresponding to some key which, in this case, must be an integer in the
0
..
25 range:
> modulus := Length(alphabet):
shift := proc(key);
i -> (i+key) mod modulus;
end:
We are now ready for the encryption and decryption functions corresponding
to the Caesar cipher. To encrypt a text string we first convert it into a list formed
by elements of
Z
26
, then we shift these elements by using the key and, finally, we
transform the resulting list into the corresponding text string, which is the ciphertext.
To convert a string into a list of its individual characters we use the function
Explode
from the package
StringTools
and we use its inverse
Implode
to convert back
to a string. Another very useful Maple function that we are using here is
map
,
which is used to apply a procedure to the operands of an expression and that we
often use—as happens here—to apply a function to all the elements of a list. Thus,
for example,
map(code,Explode(message))
in the function
CaesarEnc
below, first builds a list with the individual characters of the message and then applies
the function
code
to the elements in this list, obtaining a list of elements of
Z
26
that represents the message. Then we map the function
shift(key)
to this list
to obtain another list of elements of
Z
26
that represents the ciphertext. Finally, we
apply the function
char
to this list to obtain the list of the corresponding alphabetic
characters and we apply
Implode
to convert this list to a text string which is the
ciphertext returned by the function.
As an aside, let us mention that in recent versions of Maple, the use of the function
map
can often be replaced by the use of the element-wise operators obtained by
appending the symbol '~' to a function name, which act element-wise on a list (or
another data structure such as Array, Vector, Matrix, etc.), just as if one had used
map
. In later chapters we often use this symbol because it allows for shorter code but
in the present chapter we stick to using
map
. We advise readers to become familiar
with these functions as they are most useful and are used very often in this topic.
In the following we will be using many Maple built-in functions like these ones
without further explanation. We refer to Maple's help for details and also to [100] for
an introduction to Maple and its programming language. On the other hand, we think
that a good way to explore and to learn how the schemes and algorithms studied here
work is to go through their implementations step by step, running their components
in order and looking at the intermediate results.
The encryption and decryption functions for the Caesar cipher are then the fol-
lowing, where decryption is obtained from encryption just by replacing the key by
its negative value.
> CaesarEnc := (key,message) ->
Implode(map(char,map(shift(key),map(code,Explode(message))))):
CaesarDec := (key,message) -> CaesarEnc(-key,message):
Example 1.2
Suppose that we want to encrypt the sentence “The die is cast” with key
k
=
19. Since our alphabet does not contain spaces or upper case letters (additional