7 my $QUIS = $0; $QUIS =~ s
:^.*/::;
11 my $TAG = "ciphersaber-%s";
15 print $f "Usage: $QUIS [-de] [-t TAG] [-o FILE] file...\n";
19 print $f "$QUIS, catacomb-perl version $Catacomb::VERSION\n";
28 Implements the CipherSaber file encryption system (as described in
29 http://ciphersaber.gurus.com/). Options available are:
31 -h Display this help text.
32 -v Show the program's version number.
33 -u Show this usage message.
35 -d Decrypt the input files.
36 -e Encrypt the input files. [default]
37 -t TAG Use TAG as the passphrase tag.
38 -o FILE Write the output to FILE.
43 sub gripe
{ print STDERR
join(": ", $QUIS, @_), "\n"; $GRIPE = 1; }
44 sub barf
{ gripe
(@_); exit(1); }
45 sub hexify
{ unpack("H*", join("", @_)); }
46 sub unhexify
{ my $x = join("", @_); $x =~ tr/\s//d; pack("H*", $x); }
53 (@_ ?
" = " . join(" ", map { "<" . hexify
($_) . ">" } @_) : ""),
58 my $salt = pack("N", time()) . $Catacomb::random
->fill(6);
60 my $tag = sprintf($TAG, hexify
($salt));
61 my $pass = Catacomb
::Passphrase
->verify($tag);
62 barf
("passwords don't match") unless defined($pass);
63 open OUT
, "> $OFILE" or barf
("couldn't write file `$OFILE'", $!);
64 syswrite(OUT
, $salt) or barf
("error writing `$OFILE'", $!);
65 my $c = $Catacomb::Cipher
::rc4
->init($pass . $salt);
66 foreach my $f (@ARGV ?
@ARGV : "-") {
67 open IN
, $f or barf
("couldn't read file `$f'", $!);
70 my $rc = sysread(IN
, $buf, 8192);
71 barf
("error reading `$f'", $!) unless defined($rc);
73 syswrite(OUT
, $c->encrypt($buf)) or barf
("error writing `$OFILE'", $!);
77 close(OUT
) or barf
("error writing `$OFILE'", $!);
81 open OUT
, "> $OFILE" or barf
("couldn't write file `$OFILE'", $!);
82 foreach my $f (@ARGV ?
@ARGV : "-") {
83 open IN
, $f or barf
("couldn't read file `$f'", $!);
85 my $rc = sysread(IN
, $salt, 10);
86 barf
("error reading `$f'", $!) unless defined($rc);
87 barf
("ciphertext file is too short") unless $rc;
89 my $tag = sprintf($TAG, hexify
($salt));
90 my $pass = Catacomb
::Passphrase
->read($tag)
91 or barf
("couldn't read passphrase", $!);
92 my $c = $Catacomb::Cipher
::rc4
->init($pass . $salt);
95 my $rc = sysread(IN
, $buf, 8192);
96 barf
("error reading `$f'", $!) unless defined($rc);
98 syswrite(OUT
, $c->decrypt($buf)) or barf
("error writing `$OFILE'", $!);
102 close(OUT
) or barf
("error writing `$OFILE'", $!);
107 last if $opt eq "-" || $opt =~ /^[^-]/;
109 last if $opt eq "--";
110 $opt = substr($opt, 1);
111 while (length($opt)) {
112 my $o = substr($opt, 0, 1);
113 $opt = substr($opt, 1);
115 $OFILE = length($opt) ?
$opt : shift(@ARGV); $opt = "";
116 gripe
("option `-o' requires an argument") unless defined($OFILE);
117 } elsif ($o eq "d") {
119 } elsif ($o eq "e") {
121 } elsif ($o eq "t") {
122 $TAG = length($opt) ?
$opt : shift(@ARGV); $opt = "";
123 gripe
("option `-t' requires an argument") unless defined($TAG);
124 } elsif ($o eq "h") {
127 } elsif ($o eq "v") {
130 } elsif ($o eq "u") {
133 } elsif ($o eq "D") {
136 gripe
("unknown option `-$o'");
140 if ($GRIPE) { usage
(\
*STDERR
); exit(1); }