Create a Hill cryptosystem defined by the x
matrix space
over
, where
is the alphabet size of
the string monoid S.
INPUT:
OUTPUT:
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
Create a Hill cipher.
INPUT:
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
See HillCryptosystem for full documentation.
Create a Hill cryptosystem defined by the x
matrix space
over
, where
is the alphabet size of
the string monoid S.
INPUT:
OUTPUT:
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
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'
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:
EXAMPLES:
sage: A = AlphabeticStrings()
sage: n = randint(1, A.ngens() - 1)
sage: H = HillCryptosystem(A, n)
sage: H.block_length() == n
True
Decrypt the ciphertext C using the key A.
INPUT:
OUTPUT:
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
Encrypt the plaintext M using the key A.
INPUT:
OUTPUT:
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
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:
OUTPUT:
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
The inverse key corresponding to the key A.
INPUT:
OUTPUT:
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
A random key within the key space of this Hill cipher. That is,
generate a random x
matrix to be used as a block
permutation, where
is the block length of this Hill cipher. If
is the size of the cryptosystem alphabet, then there are
possible keys. However the number of valid keys,
i.e. invertible
x
square matrices, is smaller than
.
OUTPUT:
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
Create a substitution cryptosystem.
INPUT:
OUTPUT:
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
Create a substitution cipher.
INPUT:
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
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
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'
Decrypt the ciphertext C using the key K.
INPUT:
OUTPUT:
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
Encrypt the plaintext M using the key K.
INPUT:
OUTPUT:
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
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:
OUTPUT:
EXAMPLES:
sage: M = "Peter Pan(ning) for gold."
sage: A = AlphabeticStrings()
sage: S = SubstitutionCryptosystem(A)
sage: S.encoding(M) == A.encoding(M)
True
The inverse key corresponding to the key K. The specified key is a permutation of the cryptosystem alphabet.
INPUT:
OUTPUT:
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
Generate a random key within the key space of this substitution
cipher. The generated key is a permutation of the cryptosystem
alphabet. Let be the length of the alphabet. Then there are
possible keys in the key space.
OUTPUT:
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
Create a transposition cryptosystem of block length n.
INPUT:
OUTPUT:
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
Create a transposition cipher.
INPUT:
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
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
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'
Decrypt the ciphertext C using the key K.
INPUT:
OUTPUT:
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
Encrypt the plaintext M using the key K.
INPUT:
OUTPUT:
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
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:
OUTPUT:
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
The inverse key corresponding to the key K.
INPUT:
OUTPUT:
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
Generate a random key within the key space of this transposition
cryptosystem. Let be the block length of this cryptosystem.
Then there are
possible keys.
OUTPUT:
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
Create a Vigenere cryptosystem of block length n.
INPUT:
OUTPUT:
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
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
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
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'
Decrypt the ciphertext C using the key K.
INPUT:
OUTPUT:
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
Encrypt the plaintext M using the key K.
INPUT:
OUTPUT:
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
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:
OUTPUT:
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
The inverse key corresponding to the key K.
INPUT:
OUTPUT:
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
Generate a random key within the key space of this Vigenere
cryptosystem. Let be the length of the cryptosystem alphabet
and let
be the block length of this cryptosystem. Then there
are
possible keys.
OUTPUT:
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