Beispiel #1
0
    def __init__(self, parent, key):
        """
        Create a substitution cipher.

        INPUT: Parent and key

        EXAMPLES::

            sage: S = AlphabeticStrings()
            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: K
            ZYXWVUTSRQPONMLKJIHGFEDCBA
            sage: e = E(K)
            sage: m = S("THECATINTHEHAT")
            sage: e(m)
            GSVXZGRMGSVSZG

        TESTS::

            sage: S = AlphabeticStrings()
            sage: E = SubstitutionCryptosystem(S)
            sage: E == loads(dumps(E))
            True
        """
        SymmetricKeyCipher.__init__(self, parent, key)
Beispiel #2
0
    def __init__(self, parent, e1, e2):
        """
        Create a shrinking generator cipher.

        INPUT:


        -  ``parent`` - parent

        -  ``poly`` - connection polynomial

        -  ``IS`` - initial state


        EXAMPLES::

            sage: FF = FiniteField(2)
            sage: P.<x> = PolynomialRing(FF)
            sage: LFSR = LFSRCryptosystem(FF)
            sage: IS_1 = [ FF(a) for a in [0,1,0,1,0,0,0] ]
            sage: e1 = LFSR((x^7 + x + 1,IS_1))
            sage: IS_2 = [ FF(a) for a in [0,0,1,0,0,0,1,0,1] ]
            sage: e2 = LFSR((x^9 + x^3 + 1,IS_2))
            sage: E = ShrinkingGeneratorCryptosystem()
            sage: e = E((e1,e2))
            sage: e
            Shrinking generator cipher on Free binary string monoid
        """
        if not isinstance(e1, LFSRCipher):
            raise TypeError("Argument e1 (= %s) must be a LFSR cipher." % e1)
        if not isinstance(e2, LFSRCipher):
            raise TypeError("Argument e2 (= %s) must be a LFSR cipher." % e2)
        SymmetricKeyCipher.__init__(self, parent, key = (e1, e2))
Beispiel #3
0
    def __init__(self, parent, key):
        r"""
        Create a shift cipher.

        INPUT:

        - ``parent`` -- a ``ShiftCryptosystem`` object.

        - ``key`` -- a secret key.

        EXAMPLES::

            sage: S = ShiftCryptosystem(AlphabeticStrings()); S
            Shift cryptosystem on Free alphabetic string monoid on A-Z
            sage: P = S.encoding("The shift cryptosystem generalizes the Caesar cipher.")
            sage: P
            THESHIFTCRYPTOSYSTEMGENERALIZESTHECAESARCIPHER
            sage: K = 7
            sage: C = S.enciphering(K, P); C
            AOLZOPMAJYFWAVZFZALTNLULYHSPGLZAOLJHLZHYJPWOLY
            sage: S.deciphering(K, C)
            THESHIFTCRYPTOSYSTEMGENERALIZESTHECAESARCIPHER
            sage: S.deciphering(K, C) == P
            True
        """
        SymmetricKeyCipher.__init__(self, parent, key)
Beispiel #4
0
    def __init__(self, parent, key):
        """
        Create a substitution cipher.

        INPUT: Parent and key

        EXAMPLES::

            sage: S = AlphabeticStrings()
            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: K
            ZYXWVUTSRQPONMLKJIHGFEDCBA
            sage: e = E(K)
            sage: m = S("THECATINTHEHAT")
            sage: e(m)
            GSVXZGRMGSVSZG

        TESTS::

            sage: S = AlphabeticStrings()
            sage: E = SubstitutionCryptosystem(S)
            sage: E == loads(dumps(E))
            True
        """
        SymmetricKeyCipher.__init__(self, parent, key)
Beispiel #5
0
    def __init__(self, parent, key):
        """
        Create a Hill cipher.

        INPUT: Parent and key

        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); e
            Hill cipher on Free alphabetic string monoid on A-Z of block length 3
            sage: e(S("LAMAISONBLANCHE"))
            JYVKSKQPELAYKPV

        TESTS::

            sage: S = AlphabeticStrings()
            sage: E = HillCryptosystem(S,3)
            sage: E == loads(dumps(E))
            True
        """
        # TODO: some type checking that the key is an invertible matrix?
        SymmetricKeyCipher.__init__(self, parent, key)
Beispiel #6
0
    def __init__(self, parent, key):
        r"""
        Create a shift cipher.

        INPUT:

        - ``parent`` -- a ``ShiftCryptosystem`` object.

        - ``key`` -- a secret key.

        EXAMPLES::

            sage: S = ShiftCryptosystem(AlphabeticStrings()); S
            Shift cryptosystem on Free alphabetic string monoid on A-Z
            sage: P = S.encoding("The shift cryptosystem generalizes the Caesar cipher.")
            sage: P
            THESHIFTCRYPTOSYSTEMGENERALIZESTHECAESARCIPHER
            sage: K = 7
            sage: C = S.enciphering(K, P); C
            AOLZOPMAJYFWAVZFZALTNLULYHSPGLZAOLJHLZHYJPWOLY
            sage: S.deciphering(K, C)
            THESHIFTCRYPTOSYSTEMGENERALIZESTHECAESARCIPHER
            sage: S.deciphering(K, C) == P
            True
        """
        SymmetricKeyCipher.__init__(self, parent, key)
Beispiel #7
0
    def __init__(self, parent, key):
        """
        Create a Hill cipher.

        INPUT: Parent and key

        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); e
            Hill cipher on Free alphabetic string monoid on A-Z of block length 3
            sage: e(S("LAMAISONBLANCHE"))
            JYVKSKQPELAYKPV

        TESTS::

            sage: S = AlphabeticStrings()
            sage: E = HillCryptosystem(S,3)
            sage: E == loads(dumps(E))
            True
        """
        # TODO: some type checking that the key is an invertible matrix?
        SymmetricKeyCipher.__init__(self, parent, key)
Beispiel #8
0
    def __init__(self, parent, poly, IS):
        """
        Create a linear feedback shift register (LFSR) cipher.

        INPUT:


        -  ``parent`` - parent

        -  ``poly`` - connection polynomial

        -  ``IS`` - initial state


        EXAMPLES::

            sage: FF = FiniteField(2)
            sage: P.<x> = PolynomialRing(FF)
            sage: E = LFSRCryptosystem(FF)
            sage: E
            LFSR cryptosystem over Finite Field of size 2
            sage: IS = [ FF(a) for a in [0,1,1,1,0,1,1] ]
            sage: g = x^7 + x + 1
            sage: e = E((g,IS))
            sage: B = BinaryStrings()
            sage: m = B.encoding("THECATINTHEHAT")
            sage: e(m)
            0010001101111010111010101010001100000000110100010101011100001011110010010000011111100100100011001101101000001111
            sage: FF = FiniteField(2)
            sage: P.<x> = PolynomialRing(FF)
            sage: LFSR = LFSRCryptosystem(FF)
            sage: e = LFSR((x^2+x+1,[FF(0),FF(1)]))
            sage: B = e.domain()
            sage: m = B.encoding("The cat in the hat.")
            sage: e(m)
            00111001110111101011111001001101110101011011101000011001100101101011001000000011100101101010111100000101110100111111101100000101110101111010111101000011
            sage: m == e(e(m))
            True

        TESTS::

            sage: FF = FiniteField(2)
            sage: P.<x> = PolynomialRing(FF)
            sage: E = LFSRCryptosystem(FF)
            sage: E == loads(dumps(E))
            True
        """
        SymmetricKeyCipher.__init__(self, parent, key = (poly, IS))
Beispiel #9
0
    def __init__(self, parent, key):
        """
        Create a transposition cipher.

        INPUT: Parent and key

        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: m = S("THECATINTHEHAT")
            sage: e(m)
            TAHEHTNITACEHT

        EXAMPLES::

            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

        TESTS::

            sage: S = AlphabeticStrings()
            sage: E = TranspositionCryptosystem(S,14)
            sage: E == loads(dumps(E))
            True
        """
        n = parent.block_length()
        if isinstance(key, list) and not len(key) == n:
            raise ValueError, "key (= %s) must have block length %s" % (key, n)
        SymmetricKeyCipher.__init__(self, parent, key)
Beispiel #10
0
    def __init__(self, parent, key):
        """
        Create a transposition cipher.

        INPUT: Parent and key

        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: m = S("THECATINTHEHAT")
            sage: e(m)
            TAHEHTNITACEHT

        EXAMPLES::

            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

        TESTS::

            sage: S = AlphabeticStrings()
            sage: E = TranspositionCryptosystem(S,14)
            sage: E == loads(dumps(E))
            True
        """
        n = parent.block_length()
        if isinstance(key, list) and not len(key) == n:
            raise ValueError, "key (= %s) must have block length %s" % (key, n)
        SymmetricKeyCipher.__init__(self, parent, key)
Beispiel #11
0
    def __init__(self, parent, key):
        r"""
        Create an affine cipher.

        INPUT:

        - ``parent`` -- an ``AffineCryptosystem`` object.

        - ``key`` -- a secret key. Let `N` be the size of the cipher domain.
          A key of this affine cipher is an ordered pair
          `(a, b) \in \ZZ_N \times \ZZ_N` such that `\gcd(a, N) = 1`.

        EXAMPLES:

        Testing of dumping and loading object::

            sage: A = AffineCryptosystem(AlphabeticStrings())
            sage: AC = A(3, 5)
            sage: AC == loads(dumps(AC))
            True
        """
        SymmetricKeyCipher.__init__(self, parent, key)
Beispiel #12
0
    def __init__(self, parent, key):
        r"""
        Create an affine cipher.

        INPUT:

        - ``parent`` -- an ``AffineCryptosystem`` object.

        - ``key`` -- a secret key. Let `N` be the size of the cipher domain.
          A key of this affine cipher is an ordered pair
          `(a, b) \in \ZZ_N \times \ZZ_N` such that `\gcd(a, N) = 1`.

        EXAMPLES:

        Testing of dumping and loading object::

            sage: A = AffineCryptosystem(AlphabeticStrings())
            sage: AC = A(3, 5)
            sage: AC == loads(dumps(AC))
            True
        """
        SymmetricKeyCipher.__init__(self, parent, key)
Beispiel #13
0
    def __init__(self, parent, key):
        """
        Create a Vigenere cipher.

        INPUT: Parent and key

        EXAMPLES::

            sage: S = AlphabeticStrings()
            sage: E = VigenereCryptosystem(S,11)
            sage: K = S("SHAKESPEARE")
            sage: e = E(K)
            sage: m = S("THECATINTHEHAT")
            sage: e(m)
            LOEMELXRTYIZHT

        TESTS::

            sage: S = AlphabeticStrings()
            sage: E = VigenereCryptosystem(S,11)
            sage: E == loads(dumps(E))
            True
        """
        SymmetricKeyCipher.__init__(self, parent, key)
Beispiel #14
0
    def __init__(self, parent, key):
        """
        Create a Vigenere cipher.

        INPUT: Parent and key

        EXAMPLES::

            sage: S = AlphabeticStrings()
            sage: E = VigenereCryptosystem(S,11)
            sage: K = S("SHAKESPEARE")
            sage: e = E(K)
            sage: m = S("THECATINTHEHAT")
            sage: e(m)
            LOEMELXRTYIZHT

        TESTS::

            sage: S = AlphabeticStrings()
            sage: E = VigenereCryptosystem(S,11)
            sage: E == loads(dumps(E))
            True
        """
        SymmetricKeyCipher.__init__(self, parent, key)