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
sizebytes 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, thenlooks like ECBif N is more than 0, otherwiseno 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
1 63 63 63 63 78 78 78 78 63 63 63 63
repeated blocks: 10 looks like ECB
1 158 221 24 103 11 23 123 71 122 81 212 59
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