rileggendo il contest vecchio ( http://www.infernet-x.com/contest/cifrario-simmetrico-t-14176-2.html )
mi è venuta voglia di riscrivere il codice di meh.
esercizio:
ed ecco il mio codice perl (solo cifratura)
qualcuno noteràche manca il reverse e lo spostamento, ma ecco perchè:
quindi i due reverse si annullano a vicenda
mi è venuta voglia di riscrivere il codice di meh.
esercizio:
Codice:
/*
- Get the binary from the ascii code xor'd 42
- Invert the bits
- Reverse the order
- Move the first 4 bits at the end
- Swap the first bit with the last the second with the seventh etc
- Substitute 1 and 0 with their (random) possible chars:
zeroes: / $ [ ] { ~ ) @ # _ , ; . :
ones: + - % ? } < > ( !
- Add a random char with a 42% chance:
0-9 A-Z a-z " ' & | * ^ =
*/
ed ecco il mio codice perl (solo cifratura)
Codice:
#!/usr/bin/perl -w
@A = ( '/', '$', '[', ']', '{', '~', ')', '@', '#', '_', ',', ';', '.', ':' );
@B = ( '+', '-', '%', '?', '}', '<', '>', '(', '!' );
@C = ( 0 .. 9, "A" .. "Z", "a" .. "z", '"', '\'', '&', '|', '*', '^', '=' );
print "> ";
$text = <>;
chomp $text;
@text = map( ord ^ 42, split //, $text );
for $i ( 0 .. $#text ) {
$text[$i] = 0 . sprintf( '%b', $text[$i] ) while length $text[$i] < 8;
@bits = split //, $text[$i] ;
for $j ( 0 .. $#bits ) {
$bits[$j] =~ s/1/x/;
$bits[$j] =~ s/0/1/;
$bits[$j] =~ s/x/0/;
}
@bits = split //, ( $bits[4] . $bits[5] . $bits[6] . $bits[7] . $bits[0] . $bits[1] . $bits[2] . $bits[3] );
for $j ( 0 .. $#bits ) {
$output .= $A[ int( rand(@A) ) ] if $bits[$j] eq "0";
$output .= $B[ int( rand(@B) ) ] if $bits[$j] eq "1";
$output .= $C[ int( rand(@C) ) ] if int( rand(100) ) < 42;
}
}
print "$output\n";
qualcuno noteràche manca il reverse e lo spostamento, ma ecco perchè:
scimmia nel mio cervello ha detto:- Reverse the order
- Move the first 4 bits at the end
- Swap the first bit with the last the second with the seventh etc
- Swap the first bit with the last the second with the seventh etc ==> reverse
- Reverse the order
- Move the first 4 bits at the end
- Reverse the order
01234567
76543210
32107654
45670123
quindi i due reverse si annullano a vicenda