Sage has a type for cryptosystems (created by David Kohel, who also wrote the examples below), implementing classical cryptosystems. The general interface is as follows:
sage: S = AlphabeticStrings() sage: S Free alphabetic string monoid on A-Z sage: E = SubstitutionCryptosystem(S) sage: E Substitution cryptosystem on Free alphabetic string monoid on A-Z sage: K = S([ 25-i for i in range(26) ]) sage: e = E(K) sage: m = S("THECATINTHEHAT") sage: e(m) GSVXZGRMGSVSZG
Here's another example:
sage: S = AlphabeticStrings() sage: E = TranspositionCryptosystem(S,15); sage: m = S("THECATANDTHEHAT") sage: G = E.key_space() sage: G Symmetric group of order 15! as a permutation group sage: g = G([ 3, 2, 1, 6, 5, 4, 9, 8, 7, 12, 11, 10, 15, 14, 13 ]) sage: e = E(g) sage: e(m) EHTTACDNAEHTTAH
The idea is that a cryptosystem is a map
Hom
Set
where
,
, and
are the key space, plaintext (or message) space,
and ciphertext space, respectively.
is presumed to be injective, so
e.key()
returns the pre-image key.
See About this document... for information on suggesting changes.