Under construction

Still being built. Things may change

DeCSS 2

Time limit1sMemory limit1024 MB

Summary
Given a partially known CSS keystream, find one 42-bit key that reproduces every known byte.
Level

Medium6 of 10

Topics
Brute force, Matrix, Bit manipulation
Solved
No attempts yet

Problem

Content Scramble System (CSS) is a method that encrypts content on DVD media so that only licensed devices can access it. In the variant of this system covered here, the key KK is a bit string of exactly 42 bits k1,k2,…,k42k_1, k_2, \ldots, k_{42}, and the content is a sequence of nn bytes. The content is encrypted by first generating a keystream T(K)T(K) from the key. The keystream is also a sequence of nn bytes. Each content byte is then combined with the corresponding keystream byte by bitwise XOR.

If the encrypted text and some content bytes are known, the corresponding keystream bytes can be determined. Your task is to find one possible key KK based on a partially known keystream T(K)T(K).

The keystream function T(K)T(K) is based on a linear-feedback shift register (LFSR) circuit. The state of an LFSR consists of mm bits b1,b2,…,bmb_1, b_2, \ldots, b_m, and a set of feedback positions is defined. In one cycle, an LFSR produces one output bit and updates its state as follows:

  1. Compute the output bit bb by XORing the bits at the feedback positions. If the result is even, bb is 0. Otherwise, bb is 1.
  2. Shift all state bits left. Bit b1b_1 is discarded, and bb is placed in the last position. The new state is b2,b3,…,bm,bb_2, b_3, \ldots, b_m, b.

One step of an LFSR consists of 8 cycles, and its result is one byte. If the output bits of the cycles are i1,i2,…,i8i_1, i_2, \ldots, i_8 in order, the result is the integer between 0 and 255 whose binary form is (i8i7…i1)2(i_8 i_7 \ldots i_1)_2.

CSS uses two such circuits:

  • LFSR17 has 17 bits, with feedback positions 1 and 15. Its initial state is filled in order with key bits k1k_1 through k17k_{17}.
  • LFSR25 has 25 bits, with feedback positions 1, 4, 5, and 13. Its initial state is filled in order with key bits k18k_{18} through k42k_{42}.

Figure 1: Keystream generation

The keystream T(K)T(K) is generated as follows:

  1. Set LFSR17 and LFSR25 to the initial states determined by the key KK.
  2. Set the variable cc to 0.
  3. Repeat nn times:
    1. Perform one step of LFSR17, and let xx be its result.
    2. Perform one step of LFSR25, and let yy be its result.
    3. Compute z=x+y+cz = x + y + c.
    4. If z≥256z \geq 256, subtract 256 from zz and set cc to 1. Otherwise, set cc to 0.
    5. The next keystream byte is zz.

A partially known keystream is given, where some bytes are known and some are unknown. Find one possible key KK that produces a keystream matching the given one when generated in the way described above.

Input

The first line contains a natural number nn, the length of the given keystream. The second line contains nn integers t1,t2,…,tnt_1, t_2, \ldots, t_n, which are the keystream bytes in order. If the kk-th byte is unknown, then tk=−1t_k = -1. If it is known, then 0≤tk≤2550 \leq t_k \leq 255.

Output

Print the bits of the key k1,k2,…,k42k_1, k_2, \ldots, k_{42} on one line without spaces.

A solution always exists, but it may not be unique.

Hint

The table below shows how the first four keystream bytes of the first example are generated. The first row is the initial state. Every other row is the state right after the given cycle or step ends. In each LFSR, the shaded positions are the feedback positions, and the underlined last bit is the output bit of that cycle.

StepCycleLFSR17LFSR25xycz
Initial state0 1100 0110 0101 00101 1001 1000 0000 0000 0011 10110
11 1000 1100 1010 01001 0011 0000 0000 0000 0111 0110
21 0001 1001 0100 10000 0110 0000 0000 0000 1110 1101
30 0011 0010 1001 00010 1100 0000 0000 0001 1101 1011
40 0110 0101 0010 00101 1000 0000 0000 0011 1011 0110
50 1100 1010 0100 01001 0000 0000 0000 0111 0110 1101
61 1001 0100 1000 10010 0000 0000 0000 1110 1101 1011
71 0010 1001 0001 00110 0000 0000 0001 1101 1011 0110
80 0101 0010 0010 01110 0000 0000 0011 1011 0110 1101
12281821154
90 1010 0100 0100 11110 0000 0000 0111 0110 1101 1011
101 0100 1000 1001 11110 0000 0000 1110 1101 1011 0111
110 1001 0001 0011 11100 0000 0001 1101 1011 0110 1110
121 0010 0010 0111 11010 0000 0011 1011 0110 1101 1101
130 0100 0100 1111 10100 0000 0111 0110 1101 1011 1011
140 1000 1001 1111 01000 0000 1110 1101 1011 0111 0110
151 0001 0011 1110 10010 0001 1101 1011 0110 1110 1101
160 0010 0111 1101 00110 0011 1011 0110 1101 1101 1010
220391139
170 0100 1111 1010 01100 0111 0110 1101 1011 1011 0100
180 1001 1111 0100 11010 1110 1101 1011 0111 0110 1001
191 0011 1110 1001 10111 1101 1011 0110 1110 1101 0010
200 0111 1101 0011 01111 1011 0110 1101 1101 1010 0100
210 1111 1010 0110 11111 0110 1101 1011 1011 0100 1000
221 1111 0100 1101 11110 1101 1011 0111 0110 1001 0001
231 1110 1001 1011 11101 1011 0110 1110 1101 0010 0010
241 1101 0011 0111 11001 0110 1101 1101 1010 0100 0101
3621620225
251 1010 0110 1111 10000 1101 1011 1011 0100 1000 1011
261 0100 1101 1111 00011 1011 0111 0110 1001 0001 0110
270 1001 1011 1110 00111 0110 1110 1101 0010 0010 1101
281 0011 0111 1100 01100 1101 1101 1010 0100 0101 1011
290 0110 1111 1000 11001 1011 1011 0100 1000 1011 0111
300 1101 1111 0001 10011 0111 0110 1001 0001 0110 1111
311 1011 1110 0011 00100 1110 1101 0010 0010 1101 1110
321 0111 1100 0110 01011 1101 1010 0100 0101 1011 1101
4166189199

Examples1

  1. Example 1

    Input
    7
    154 39 225 99 151 145 -1
    
    Expected output
    011000110010100101100110000000000000111011