-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchal8.pl
executable file
·45 lines (38 loc) · 1.26 KB
/
chal8.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/perl -w
# chal8.pl - Detect AES in ECB mode (from file of about 200 hex
# strings, one per line, each about 160 bytes).
#
# Copyright (C) 2015 Andrew J. Zimolzak <andyzimolzak@gmail.com>
# Full notice is found in the file 'LICENSE' in the same directory
# as this file.
# usage: ./chal8.pl 8.txt
use strict;
use Cryptopals qw(hamming argmax argmin);
use Rkxor qw(hex2blocks);
my @normdistances;
my @repeat_blocks_per_row;
while(<>){
chomp;
my @b = hex2blocks($_, 16);
my $avg_dist = ( hamming($b[0],$b[1]) +
hamming($b[2],$b[3]) +
hamming($b[4],$b[5]) ) / 3;
push @normdistances, $avg_dist / 16;
my $repeats_this_row = 0;
for my $i (0..$#b){
for my $j (0..$#b){
next if $i >= $j; # left upper triangle matrix
$repeats_this_row++ if $b[$i] eq $b[$j];
}
}
push @repeat_blocks_per_row, $repeats_this_row;
print "$repeats_this_row repeats in line $.:\n "
, join("\n ", @b), "\n"
if $repeats_this_row;
}
my $ax = join(',', argmax(@normdistances)); #um might not want to join.
my $an = join(',', argmin(@normdistances));
print "Line ", $ax+1, " dist $normdistances[$ax], line ",
$an+1, " dist $normdistances[$an]\n";
die unless $repeat_blocks_per_row[132] == 6;
warn "Passed assertion $0\n";