Stream Ciphers - Cryptography

Skip to content
Back to top
View this page
Edit this page

# Stream Ciphers

class sage.crypto.stream_cipher.LFSRCipher(parent, poly, IS)

  Bases: `SymmetricKeyCipher`

  Create a linear feedback shift register (LFSR) cipher.

  INPUT:

  - `parent` – parent

  - `poly` – connection polynomial

  - `IS` – initial state

  EXAMPLES:

  Sage

      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

  Python

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

  connection_polynomial()

    The connection polynomial defining the LFSR of the cipher.

    EXAMPLES:

    Sage

        sage: k = GF(2)
        sage: P.<x> = PolynomialRing( k )
        sage: LFSR = LFSRCryptosystem( k )
        sage: e = LFSR((x^2+x+1,[k(0), k(1)]))
        sage: e.connection_polynomial()
        x^2 + x + 1

    Python

        >>> from sage.all import *
        >>> k = GF(Integer(2))
        >>> P = PolynomialRing( k , names=('x',)); (x,) = P._first_ngens(1)
        >>> LFSR = LFSRCryptosystem( k )
        >>> e = LFSR((x**Integer(2)+x+Integer(1),[k(Integer(0)), k(Integer(1))]))
        >>> e.connection_polynomial()
        x^2 + x + 1

  initial_state()

    The initial state of the LFSR cipher.

    EXAMPLES:

    Sage

        sage: k = GF(2)
        sage: P.<x> = PolynomialRing( k )
        sage: LFSR = LFSRCryptosystem( k )
        sage: e = LFSR((x^2+x+1,[k(0), k(1)]))
        sage: e.initial_state()
        [0, 1]

    Python

        >>> from sage.all import *
        >>> k = GF(Integer(2))
        >>> P = PolynomialRing( k , names=('x',)); (x,) = P._first_ngens(1)
        >>> LFSR = LFSRCryptosystem( k )
        >>> e = LFSR((x**Integer(2)+x+Integer(1),[k(Integer(0)), k(Integer(1))]))
        >>> e.initial_state()
        [0, 1]

class sage.crypto.stream_cipher.ShrinkingGeneratorCipher(parent, e1, e2)

  Bases: `SymmetricKeyCipher`

  Create a shrinking generator cipher.

  INPUT:

  - `parent` – parent

  - `poly` – connection polynomial

  - `IS` – initial state

  EXAMPLES:

  Sage

      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

  Python

      >>> from sage.all import *
      >>> FF = FiniteField(Integer(2))
      >>> P = PolynomialRing(FF, names=('x',)); (x,) = P._first_ngens(1)
      >>> LFSR = LFSRCryptosystem(FF)
      >>> IS_1 = [ FF(a) for a in [Integer(0),Integer(1),Integer(0),Integer(1),Integer(0),Integer(0),Integer(0)] ]
      >>> e1 = LFSR((x**Integer(7) + x + Integer(1),IS_1))
      >>> IS_2 = [ FF(a) for a in [Integer(0),Integer(0),Integer(1),Integer(0),Integer(0),Integer(0),Integer(1),Integer(0),Integer(1)] ]
      >>> e2 = LFSR((x**Integer(9) + x**Integer(3) + Integer(1),IS_2))
      >>> E = ShrinkingGeneratorCryptosystem()
      >>> e = E((e1,e2))
      >>> e
      Shrinking generator cipher on Free binary string monoid

  decimating_cipher()

    The LFSR cipher generating the decimating key stream.

    EXAMPLES:

    Sage

        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.decimating_cipher()
        LFSR cipher on Free binary string monoid

    Python

        >>> from sage.all import *
        >>> FF = FiniteField(Integer(2))
        >>> P = PolynomialRing(FF, names=('x',)); (x,) = P._first_ngens(1)
        >>> LFSR = LFSRCryptosystem(FF)
        >>> IS_1 = [ FF(a) for a in [Integer(0),Integer(1),Integer(0),Integer(1),Integer(0),Integer(0),Integer(0)] ]
        >>> e1 = LFSR((x**Integer(7) + x + Integer(1),IS_1))
        >>> IS_2 = [ FF(a) for a in [Integer(0),Integer(0),Integer(1),Integer(0),Integer(0),Integer(0),Integer(1),Integer(0),Integer(1)] ]
        >>> e2 = LFSR((x**Integer(9) + x**Integer(3) + Integer(1),IS_2))
        >>> E = ShrinkingGeneratorCryptosystem()
        >>> e = E((e1,e2))
        >>> e.decimating_cipher()
        LFSR cipher on Free binary string monoid

  keystream_cipher()

    The LFSR cipher generating the output key stream.

    EXAMPLES:

    Sage

        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.keystream_cipher()
        LFSR cipher on Free binary string monoid

    Python

        >>> from sage.all import *
        >>> FF = FiniteField(Integer(2))
        >>> P = PolynomialRing(FF, names=('x',)); (x,) = P._first_ngens(1)
        >>> LFSR = LFSRCryptosystem(FF)
        >>> IS_1 = [ FF(a) for a in [Integer(0),Integer(1),Integer(0),Integer(1),Integer(0),Integer(0),Integer(0)] ]
        >>> e1 = LFSR((x**Integer(7) + x + Integer(1),IS_1))
        >>> IS_2 = [ FF(a) for a in [Integer(0),Integer(0),Integer(1),Integer(0),Integer(0),Integer(0),Integer(1),Integer(0),Integer(1)] ]
        >>> e2 = LFSR((x**Integer(9) + x**Integer(3) + Integer(1),IS_2))
        >>> E = ShrinkingGeneratorCryptosystem()
        >>> e = E((e1,e2))
        >>> e.keystream_cipher()
        LFSR cipher on Free binary string monoid