Classical Cryptosystems

class sage.crypto.classical.HillCryptosystem(S, m)

Create a Hill cryptosystem defined by the m x m matrix space over \ZZ / N \ZZ, where N is the alphabet size of the string monoid S.

INPUT:

  • S - a string monoid over some alphabet
  • m - integer > 0; the block length of matrices that specify block permutations

OUTPUT:

  • A Hill cryptosystem of block length m over the alphabet S.

EXAMPLES:

sage: S = AlphabeticStrings()
sage: E = HillCryptosystem(S,3)
sage: E
Hill cryptosystem on Free alphabetic string monoid on A-Z of block length 3
sage: R = IntegerModRing(26)
sage: M = MatrixSpace(R,3,3)
sage: A = M([[1,0,1],[0,1,1],[2,2,3]])
sage: A
[1 0 1]
[0 1 1]
[2 2 3]
sage: e = E(A)
sage: e
[1 0 1]
[0 1 1]
[2 2 3]
sage: e(S("LAMAISONBLANCHE"))
JYVKSKQPELAYKPV

TESTS:

sage: S = AlphabeticStrings()
sage: E = HillCryptosystem(S,3)
sage: E == loads(dumps(E))
True
__call__(A)

Create a Hill cipher.

INPUT:

  • A - a matrix which specifies a block permutation

EXAMPLES:

sage: S = AlphabeticStrings()
sage: E = HillCryptosystem(S,3)
sage: E
Hill cryptosystem on Free alphabetic string monoid on A-Z of block length 3
sage: M = E.key_space()
sage: A = M([[1,0,1],[0,1,1],[2,2,3]])
sage: A
[1 0 1]
[0 1 1]
[2 2 3]
sage: e = E(A)
sage: e
[1 0 1]
[0 1 1]
[2 2 3]
sage: m = S("LAMAISONBLANCHE")
sage: e(m)
JYVKSKQPELAYKPV
sage: c = e.inverse()
sage: c(e(m))
LAMAISONBLANCHE
__init__(S, m)

See HillCryptosystem for full documentation.

Create a Hill cryptosystem defined by the m x m matrix space over \ZZ / N \ZZ, where N is the alphabet size of the string monoid S.

INPUT:

  • S - a string monoid over some alphabet
  • m - integer > 0; the block length of matrices that specify block permutations

OUTPUT:

  • A Hill cryptosystem of block length m over the alphabet S.

EXAMPLES:

sage: S = AlphabeticStrings()
sage: E = HillCryptosystem(S,3)
sage: E
Hill cryptosystem on Free alphabetic string monoid on A-Z of block length 3
_repr_()

Return a string representation of self.

EXAMPLES:

sage: A = AlphabeticStrings()
sage: H = HillCryptosystem(A, 3)
sage: H
Hill cryptosystem on Free alphabetic string monoid on A-Z of block length 3
sage: H._repr_()
'Hill cryptosystem on Free alphabetic string monoid on A-Z of block length 3'
block_length()

The row or column dimension of a matrix specifying a block permutation. Encryption and decryption keys of a Hill cipher are square matrices, i.e. the row and column dimensions of an encryption or decryption key are the same. This row/column dimension is referred to as the block length.

OUTPUT:

  • The block length of an encryption/decryption key.

EXAMPLES:

sage: A = AlphabeticStrings()
sage: n = randint(1, A.ngens() - 1)
sage: H = HillCryptosystem(A, n)
sage: H.block_length() == n
True
deciphering(A, C)

Decrypt the ciphertext C using the key A.

INPUT:

  • A - a key within the key space of this Hill cipher
  • C - a string (possibly empty) over the string monoid of this Hill cipher

OUTPUT:

  • The plaintext corresponding to the ciphertext C.

EXAMPLES:

sage: H = HillCryptosystem(AlphabeticStrings(), 3)
sage: K = H.random_key()
sage: M = H.encoding("Good day, mate! How ya going?")
sage: H.deciphering(K, H.enciphering(K, M)) == M
True
enciphering(A, M)

Encrypt the plaintext M using the key A.

INPUT:

  • A - a key within the key space of this Hill cipher
  • M - a string (possibly empty) over the string monoid of this Hill cipher.

OUTPUT:

  • The ciphertext corresponding to the plaintext M.

EXAMPLES:

sage: H = HillCryptosystem(AlphabeticStrings(), 3)
sage: K = H.random_key()
sage: M = H.encoding("Good day, mate! How ya going?")
sage: H.deciphering(K, H.enciphering(K, M)) == M
True
encoding(M)

The encoding of the string M over the string monoid of this Hill cipher. For example, if the string monoid of this Hill cipher is AlphabeticStringMonoid, then the encoding of M would be its upper-case equivalent stripped of all non-alphabetic characters.

INPUT:

  • M - a string, possibly empty

OUTPUT:

  • The encoding of M over the string monoid of this Hill cipher.

EXAMPLES:

sage: M = "The matrix cipher by Lester S. Hill."
sage: A = AlphabeticStrings()
sage: H = HillCryptosystem(A, 7)
sage: H.encoding(M) == A.encoding(M)
True
inverse_key(A)

The inverse key corresponding to the key A.

INPUT:

  • A - an invertible matrix of the key space of this Hill cipher

OUTPUT:

  • The inverse matrix of A.

EXAMPLES:

sage: S = AlphabeticStrings()
sage: E = HillCryptosystem(S,3)
sage: A = E.random_key()
sage: B = E.inverse_key(A)
sage: M = S("LAMAISONBLANCHE")
sage: e = E(A)
sage: c = E(B)
sage: c(e(M))
LAMAISONBLANCHE
random_key()

A random key within the key space of this Hill cipher. That is, generate a random m x m matrix to be used as a block permutation, where m is the block length of this Hill cipher. If n is the size of the cryptosystem alphabet, then there are n^{m^2} possible keys. However the number of valid keys, i.e. invertible m x m square matrices, is smaller than n^{m^2}.

OUTPUT:

  • A random key within the key space of this Hill cipher.

EXAMPLES:

sage: A = AlphabeticStrings()
sage: n = 3
sage: H = HillCryptosystem(A, n)
sage: K = H.random_key()
sage: Ki = H.inverse_key(K)
sage: M = "LAMAISONBLANCHE"
sage: e = H(K)
sage: d = H(Ki)
sage: d(e(A(M))) == A(M)
True
class sage.crypto.classical.SubstitutionCryptosystem(S)

Create a substitution cryptosystem.

INPUT:

  • S - a string monoid over some alphabet

OUTPUT:

  • A substitution cryptosystem over the alphabet S.

EXAMPLES:

sage: M = AlphabeticStrings()
sage: E = SubstitutionCryptosystem(M)
sage: E
Substitution cryptosystem on Free alphabetic string monoid on A-Z
sage: K = M([ 25-i for i in range(26) ])
sage: K
ZYXWVUTSRQPONMLKJIHGFEDCBA
sage: e = E(K)
sage: m = M("THECATINTHEHAT")
sage: e(m)
GSVXZGRMGSVSZG

TESTS:

sage: M = AlphabeticStrings()
sage: E = SubstitutionCryptosystem(M)
sage: E == loads(dumps(E))
True
__call__(K)

Create a substitution cipher.

INPUT:

  • K - a key which is a permutation of the cryptosystem alphabet

EXAMPLES:

sage: M = AlphabeticStrings()
sage: E = SubstitutionCryptosystem(M)
sage: E
Substitution cryptosystem on Free alphabetic string monoid on A-Z
sage: K = M([ 25-i for i in range(26) ])
sage: K
ZYXWVUTSRQPONMLKJIHGFEDCBA
sage: e = E(K)
sage: m = M("THECATINTHEHAT")
sage: e(m)
GSVXZGRMGSVSZG
__init__(S)

See SubstitutionCryptosystem for full documentation.

EXAMPLES:

sage: M = AlphabeticStrings()
sage: E = SubstitutionCryptosystem(M)
sage: E
Substitution cryptosystem on Free alphabetic string monoid on A-Z
_repr_()

Return a string representation of self.

EXAMPLES:

sage: A = AlphabeticStrings()
sage: S = SubstitutionCryptosystem(A)
sage: S
Substitution cryptosystem on Free alphabetic string monoid on A-Z
sage: S._repr_()
'Substitution cryptosystem on Free alphabetic string monoid on A-Z'
deciphering(K, C)

Decrypt the ciphertext C using the key K.

INPUT:

  • K - a key belonging to the key space of this substitution cipher
  • C - a string (possibly empty) over the string monoid of this cryptosystem.

OUTPUT:

  • The plaintext corresponding to the ciphertext C.

EXAMPLES:

sage: S = SubstitutionCryptosystem(AlphabeticStrings())
sage: K = S.random_key()
sage: M = S.encoding("Don't substitute me!")
sage: S.deciphering(K, S.enciphering(K, M)) == M
True
enciphering(K, M)

Encrypt the plaintext M using the key K.

INPUT:

  • K - a key belonging to the key space of this substitution cipher
  • M - a string (possibly empty) over the string monoid of this cryptosystem.

OUTPUT:

  • The ciphertext corresponding to the plaintext M.

EXAMPLES:

sage: S = SubstitutionCryptosystem(AlphabeticStrings())
sage: K = S.random_key()
sage: M = S.encoding("Don't substitute me.")
sage: S.deciphering(K, S.enciphering(K, M)) == M
True
encoding(M)

The encoding of the string M over the string monoid of this substitution cipher. For example, if the string monoid of this cryptosystem is AlphabeticStringMonoid, then the encoding of M would be its upper-case equivalent stripped of all non-alphabetic characters.

INPUT:

  • M - a string, possibly empty

OUTPUT:

  • The encoding of M over the string monoid of this cryptosystem.

EXAMPLES:

sage: M = "Peter Pan(ning) for gold."
sage: A = AlphabeticStrings()
sage: S = SubstitutionCryptosystem(A)
sage: S.encoding(M) == A.encoding(M)
True
inverse_key(K)

The inverse key corresponding to the key K. The specified key is a permutation of the cryptosystem alphabet.

INPUT:

  • K - a key belonging to the key space of this cryptosystem

OUTPUT:

  • The inverse key of K.

EXAMPLES:

sage: S = AlphabeticStrings()
sage: E = SubstitutionCryptosystem(S)
sage: K = E.random_key()
sage: L = E.inverse_key(K)
sage: M = S("THECATINTHEHAT")
sage: e = E(K)
sage: c = E(L)
sage: c(e(M))
THECATINTHEHAT
random_key()

Generate a random key within the key space of this substitution cipher. The generated key is a permutation of the cryptosystem alphabet. Let n be the length of the alphabet. Then there are n! possible keys in the key space.

OUTPUT:

  • A random key within the key space of this cryptosystem.

EXAMPLES:

sage: A = AlphabeticStrings()
sage: S = SubstitutionCryptosystem(A)
sage: K = S.random_key()
sage: Ki = S.inverse_key(K)
sage: M = "THECATINTHEHAT"
sage: e = S(K)
sage: d = S(Ki)
sage: d(e(A(M))) == A(M)
True
class sage.crypto.classical.TranspositionCryptosystem(S, n)

Create a transposition cryptosystem of block length n.

INPUT:

  • S - a string monoid over some alphabet
  • n - integer > 0; a block length of a block permutation

OUTPUT:

  • A transposition cryptosystem of block length n over the alphabet S.

EXAMPLES:

sage: S = AlphabeticStrings()
sage: E = TranspositionCryptosystem(S,14)
sage: E
Transposition cryptosystem on Free alphabetic string monoid on A-Z of block length 14
sage: K = [ 14-i for i in range(14) ]
sage: K
[14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
sage: e = E(K)
sage: e(S("THECATINTHEHAT"))
TAHEHTNITACEHT

TESTS:

sage: S = AlphabeticStrings()
sage: E = TranspositionCryptosystem(S,14)
sage: E == loads(dumps(E))
True
__call__(K)

Create a transposition cipher.

INPUT:

  • K - a key which specifies a block permutation

EXAMPLES:

sage: M = AlphabeticStrings()
sage: E = TranspositionCryptosystem(M,14)
sage: E
Transposition cryptosystem on Free alphabetic string monoid on A-Z of block length 14
sage: K = [ 14-i for i in range(14) ]
sage: K
[14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
sage: e = E(K)
sage: m = M("THECATINTHEHAT")
sage: e(m)
TAHEHTNITACEHT
__init__(S, n)

See TranspositionCryptosystem for full documentation.

EXAMPLES:

sage: S = AlphabeticStrings()
sage: E = TranspositionCryptosystem(S,14)
sage: E
Transposition cryptosystem on Free alphabetic string monoid on A-Z of block length 14
_repr_()

Return a string representation of self.

EXAMPLES:

sage: A = AlphabeticStrings()
sage: T = TranspositionCryptosystem(A, 14)
sage: T
Transposition cryptosystem on Free alphabetic string monoid on A-Z of block length 14
sage: T._repr_()
'Transposition cryptosystem on Free alphabetic string monoid on A-Z of block length 14'
deciphering(K, C)

Decrypt the ciphertext C using the key K.

INPUT:

  • K - a key belonging to the key space of this transposition cipher
  • C - a string (possibly empty) over the string monoid of this cryptosystem.

OUTPUT:

  • The plaintext corresponding to the ciphertext C.

EXAMPLES:

sage: T = TranspositionCryptosystem(AlphabeticStrings(), 14)
sage: K = T.random_key()
sage: M = T.encoding("The cat in the hat.")
sage: T.deciphering(K, T.enciphering(K, M)) == M
True
enciphering(K, M)

Encrypt the plaintext M using the key K.

INPUT:

  • K - a key belonging to the key space of this transposition cipher
  • M - a string (possibly empty) over the string monoid of this cryptosystem

OUTPUT:

  • The ciphertext corresponding to the plaintext M.

EXAMPLES:

sage: T = TranspositionCryptosystem(AlphabeticStrings(), 14)
sage: K = T.random_key()
sage: M = T.encoding("The cat in the hat.")
sage: T.deciphering(K, T.enciphering(K, M)) == M
True
encoding(M)

The encoding of the string M over the string monoid of this transposition cipher. For example, if the string monoid of this cryptosystem is AlphabeticStringMonoid, then the encoding of M would be its upper-case equivalent stripped of all non-alphabetic characters.

INPUT:

  • M - a string, possibly empty

OUTPUT:

  • The encoding of M over the string monoid of this cryptosystem.

EXAMPLES:

sage: M = "Transposition cipher is not about matrix transpose."
sage: A = AlphabeticStrings()
sage: T = TranspositionCryptosystem(A, 11)
sage: T.encoding(M) == A.encoding(M)
True
inverse_key(K, check=True)

The inverse key corresponding to the key K.

INPUT:

  • K - a key belonging to the key space of this transposition cipher
  • check - bool (default: True); check that K belongs to the key space of this cryptosystem.

OUTPUT:

  • The inverse key corresponding to K.

EXAMPLES:

sage: S = AlphabeticStrings()
sage: E = TranspositionCryptosystem(S, 14)
sage: K = E.random_key()
sage: Ki = E.inverse_key(K)
sage: e = E(K)
sage: d = E(Ki)
sage: M = "THECATINTHEHAT"
sage: C = e(S(M))
sage: d(S(C)) == S(M)
True
random_key()

Generate a random key within the key space of this transposition cryptosystem. Let n > 0 be the block length of this cryptosystem. Then there are n! possible keys.

OUTPUT:

  • A random key within the key space of this cryptosystem.

EXAMPLES:

sage: S = AlphabeticStrings()
sage: E = TranspositionCryptosystem(S, 14)
sage: K = E.random_key()
sage: Ki = E.inverse_key(K)
sage: e = E(K)
sage: d = E(Ki)
sage: M = "THECATINTHEHAT"
sage: C = e(S(M))
sage: d(S(C)) == S(M)
True
class sage.crypto.classical.VigenereCryptosystem(S, n)

Create a Vigenere cryptosystem of block length n.

INPUT:

  • S– a string monoid over some alphabet
  • n - integer > 0; block length of an encryption/decryption key

OUTPUT:

  • A Vigenere cryptosystem of block length n over the alphabet S.

EXAMPLES:

sage: S = AlphabeticStrings()
sage: E = VigenereCryptosystem(S,14)
sage: E
Vigenere cryptosystem on Free alphabetic string monoid on A-Z of period 14
sage: K = S('ABCDEFGHIJKLMN')
sage: K
ABCDEFGHIJKLMN
sage: e = E(K)
sage: e
ABCDEFGHIJKLMN
sage: e(S("THECATINTHEHAT"))
TIGFEYOUBQOSMG

TESTS:

sage: S = AlphabeticStrings()
sage: E = VigenereCryptosystem(S,14)
sage: E == loads(dumps(E))
True
__call__(K)

Create a Vigenere cipher.

INPUT: A key which specifies a block permutation.

EXAMPLES:

sage: S = AlphabeticStrings()
sage: E = VigenereCryptosystem(S,14)
sage: E
Vigenere cryptosystem on Free alphabetic string monoid on A-Z of period 14
sage: K = S('ABCDEFGHIJKLMN')
sage: K
ABCDEFGHIJKLMN
sage: e = E(K)
sage: e
ABCDEFGHIJKLMN
sage: e(S("THECATINTHEHAT"))
TIGFEYOUBQOSMG
__init__(S, n)

See VigenereCryptosystem for full documentation.

EXAMPLES:

sage: S = AlphabeticStrings()
sage: E = VigenereCryptosystem(S,14)
sage: E
Vigenere cryptosystem on Free alphabetic string monoid on A-Z of period 14
_repr_()

Return a string representation of self.

EXAMPLES:

sage: A = AlphabeticStrings()
sage: V = VigenereCryptosystem(A, 14)
sage: V
Vigenere cryptosystem on Free alphabetic string monoid on A-Z of period 14
sage: V._repr_()
'Vigenere cryptosystem on Free alphabetic string monoid on A-Z of period 14'
deciphering(K, C)

Decrypt the ciphertext C using the key K.

INPUT:

  • K - a key belonging to the key space of this Vigenere cipher
  • C - a string (possibly empty) over the string monoid of this cryptosystem

OUTPUT:

  • The plaintext corresponding to the ciphertext C.

EXAMPLES:

sage: V = VigenereCryptosystem(AlphabeticStrings(), 24)
sage: K = V.random_key()
sage: M = V.encoding("Jack and Jill went up the hill.")
sage: V.deciphering(K, V.enciphering(K, M)) == M
True
enciphering(K, M)

Encrypt the plaintext M using the key K.

INPUT:

  • K - a key belonging to the key space of this Vigenere cipher
  • M - a string (possibly empty) over the string monoid of this cryptosystem

OUTPUT:

  • The ciphertext corresponding to the plaintext M.

EXAMPLES:

sage: V = VigenereCryptosystem(AlphabeticStrings(), 24)
sage: K = V.random_key()
sage: M = V.encoding("Jack and Jill went up the hill.")
sage: V.deciphering(K, V.enciphering(K, M)) == M
True
encoding(M)

The encoding of the string M over the string monoid of this Vigenere cipher. For example, if the string monoid of this cryptosystem is AlphabeticStringMonoid, then the encoding of M would be its upper-case equivalent stripped of all non-alphabetic characters.

INPUT:

  • M - a string, possibly empty

OUTPUT:

  • The encoding of M over the string monoid of this cryptosystem.

EXAMPLES:

sage: A = AlphabeticStrings()
sage: V = VigenereCryptosystem(A, 24)
sage: M = "Jack and Jill went up the hill."
sage: V.encoding(M) == A.encoding(M)
True
inverse_key(K)

The inverse key corresponding to the key K.

INPUT:

  • K - a key within the key space of this Vigenere cryptosystem

OUTPUT:

  • The inverse key corresponding to K.

EXAMPLES:

sage: S = AlphabeticStrings()
sage: E = VigenereCryptosystem(S,14)
sage: K = E.random_key()
sage: L = E.inverse_key(K)
sage: M = S("THECATINTHEHAT")
sage: e = E(K)
sage: c = E(L)
sage: c(e(M))
THECATINTHEHAT
random_key()

Generate a random key within the key space of this Vigenere cryptosystem. Let n > 0 be the length of the cryptosystem alphabet and let m > 0 be the block length of this cryptosystem. Then there are n^m possible keys.

OUTPUT:

  • A random key within the key space of this cryptosystem.

EXAMPLES:

sage: A = AlphabeticStrings()
sage: V = VigenereCryptosystem(A, 14)
sage: M = "THECATINTHEHAT"
sage: K = V.random_key()
sage: Ki = V.inverse_key(K)
sage: e = V(K)
sage: d = V(Ki)
sage: d(e(A(M))) == A(M)
True

Previous topic

Ciphers.

Next topic

Classical Ciphers.

This Page