Spot ECB mode

ECB mode encrypts equal blocks to equal cipher blocks. Repeated blocks in a cipher text are a strong sign that ECB was used.

What to build

Split a cipher text into blocks and report how many blocks repeat an earlier block.

Requirements

  • Line 1 is the block size in bytes. Line 2 is the cipher text as byte values separated by spaces.
  • Blocks start at byte 0 and take the next size bytes each; the cipher text's length is a multiple of the size.
  • Count the blocks that are equal to some block before them.
  • Print repeated blocks: N, then looks like ECB if N is more than 0, otherwise no repeats.

Input

Two lines, as described.

Output

Two lines.

Constraints

The block size is 1 to 16. The cipher text has 1 to 400 bytes.

Example

Input:

1
63 63 63 63 78 78 78 78 63 63 63 63

Output:

repeated blocks: 10
looks like ECB

Why it works

A block cipher maps each input block to one output block, so in ECB the same plain block always gives the same cipher block, while chained modes such as CBC make repeats vanish.

Examples

Example 1 · Input
1
63 63 63 63 78 78 78 78 63 63 63 63
Expected output
repeated blocks: 10
looks like ECB
Example 2 · Input
1
158 221 24 103 11 23 123 71 122 81 212 59
Expected output
repeated blocks: 0
no repeats

Hints

Hint 1

Turn each block into a string with block.join(",") so you can keep the ones you have seen in a Set.

Hint 2

slice(i, i + size) takes one block.

Solution

Show one correct solution (try it yourself first)
const size = Number(prompt());
const bytes = prompt().trim().split(/\s+/).map(Number);
const seen = new Set();
let repeated = 0;
for (let i = 0; i < bytes.length; i += size) {
  const block = bytes.slice(i, i + size).join(",");
  if (seen.has(block)) repeated++;
  else seen.add(block);
}
console.log(`repeated blocks: ${repeated}`);
console.log(repeated > 0 ? "looks like ECB" : "no repeats");

▶ Open this solution in the compiler, then press Visualize to watch it run step by step

Lesson: Symmetric encryption and AES →