#!/usr/bin/env perl use strict; #use warnings; use List::Util qw(first); my $key="a5e0d57354e5fb898a44a979582536141ca3819d1c457db21e3cdf64bdc9001d1903ea7d9814649b2e6edee9674d57a5e47a3b0fcb5dbc8de3181c617c88650a88f9ff4a9852538087dabf35a596d657c86715d89701edefee55453302488ec3aae6a5320319a13ffbd11eb31a7e5c921421a4728b86523889fa20a44f19c4d4abf302490e39b5fa50619a18acf9785b6f1bd55158e1405776fb0ee2f3fa5f56aa828b8c22bd6f4e670c23edefb08add86cc5d3415aaa64a4fcf677d2198f1801d800c6762a84e7b0e1053b95c38c94fdd637757c2f0ef5d0c628ddf1e83e4f178a00cefd41f58c3b4c2fa71c5ed56bfa6aa321120978fbe6f8dfa7fc2b5f109"; $key=pack('H*',$key); my @sbase; for my $i (0..255){$sbase[$i]=chr($i)}; my @sbox=sbox_gen(@sbase); if ( $ARGV[2] eq "fwd" ) { fwd(); } elsif ( $ARGV[2] eq "rev" ) { rev(); } sub fwd { my ($n,$infile,$buf,$data,$outfile,$tmp); open($infile,"<",$ARGV[0]) or die $!; binmode($infile); open($outfile,">",$ARGV[1]) or die $!; binmode($outfile); while (($n=sysread $infile,$buf,256)!=0){ for my $i (0..255) { $tmp=ord(substr($buf,$i,1)); $data.=$sbox[$tmp]; } print $outfile $data;$data=""; } close($outfile); close($infile); } sub rev { my ($n,$infile,$buf,$data,$outfile,$tmp); open($infile,"<",$ARGV[0]) or die $!; binmode($infile); open($outfile,">",$ARGV[1]) or die $!; binmode($outfile); while (($n=sysread $infile,$buf,256)!=0){ for my $i (0..255) { $tmp=substr($buf,$i,1); #my $index=first { $sbox[$_] eq $tmp } 0 .. $#sbox; #$data.=chr($index); for my $j (0..255) { if ($tmp eq $sbox[$j]){$data.=chr($j);last}; } #my %index; #@index{@sbox} = (0..$#sbox); #my $index = $index{$tmp}; #$data.=chr($index); } print $outfile $data;$data=""; } close($outfile); close($infile); } sub sbox_gen { my @sbox; my @sary=@_; for my $i (0..255) { my $tmp=ord(substr($key,$i,1))**3; $tmp=$tmp%scalar(@sary); push @sbox,$sary[$tmp]; splice(@sary,$tmp,1); } return @sbox; }