17.3 Classical Cryptosystems

Module: sage.crypto.classical

Classical Cryptosystems

Class: HillCryptosystem

class HillCryptosystem
Hill cryptosystem class
HillCryptosystem( self, S, m)

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

Input: A string monoid S over some alphabet, and a block length m.

       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

Functions: block_length,$ \,$ deciphering,$ \,$ enciphering,$ \,$ encoding,$ \,$ inverse_key,$ \,$ random_key

inverse_key( self, A)

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

Special Functions: __call__,$ \,$ __init__,$ \,$ _repr_

__call__( self, A)

Create a Hill cipher.

Input: A matrix which specifies a block permutation.

       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

Class: SubstitutionCryptosystem

class SubstitutionCryptosystem
Substitution cryptosystem class
SubstitutionCryptosystem( self, S)

Create a substitution cryptosystem.

Input: A string monoid over some alphabet.

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

Functions: deciphering,$ \,$ enciphering,$ \,$ encoding,$ \,$ inverse_key,$ \,$ random_key

inverse_key( self, K)

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

Special Functions: __call__,$ \,$ __init__,$ \,$ _repr_

__call__( self, K)

Create a substitution cipher.

Input: A key which is a permutation of the cryptosystem alphabet.

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

Class: TranspositionCryptosystem

class TranspositionCryptosystem
Transposition cryptosystem class
TranspositionCryptosystem( self, S, n)

Create a transposeition cryptosystem of block length n.

Input: A string monoid S over some alphabet, and a block length n.

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

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

Functions: deciphering,$ \,$ enciphering,$ \,$ encoding,$ \,$ inverse_key,$ \,$ random_key

Special Functions: __call__,$ \,$ __init__,$ \,$ _repr_

__call__( self, K)

Create a transposition cipher.

Input: A key which specifies a block permutation.

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

Class: VigenereCryptosystem

class VigenereCryptosystem
Vigenere cryptosystem class
VigenereCryptosystem( self, S, n)

Create a Vigenere cryptosystem of block length n.

Input: A string monoid S over some alphabet, and a block length n.

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

Functions: deciphering,$ \,$ enciphering,$ \,$ encoding,$ \,$ inverse_key,$ \,$ random_key

inverse_key( self, K)

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

Special Functions: __call__,$ \,$ __init__,$ \,$ _repr_

__call__( self, K)

Create a Vigenere cipher.

Input: A key which specifies a block permutation.

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 About this document... for information on suggesting changes.