1#!/usr/local/bin/perl
2
3# CBC2CODE
4# This small script will decrypt your Filter::CBC'ed code back to your
5# plain code by reading the algorithm and the key.
6
7# This script is part of Filter::CBC. Same license rules apply.
8
9use strict;
10use Crypt::CBC;
11
12my $blank = "This space is left blank intentionally";
13my %Algorithms =
14("RIJNDAEL"=>"Rijndael",
15 "DES"=>"DES",
16 "IDEA"=>"IDEA",
17 "BLOWFISH"=>"Blowfish",
18 "GOST"=>"GOST",
19 "DES_EDE3"=>"DES_EDE3",
20 "TWOFISH"=>"Twofish",
21 "NULL"=>"NULL",
22 "TEA"=>"TEA");
23
24if (!@ARGV)
25{ print "Enter Filename with encrypted code : ";
26  my $file = <STDIN>;
27  chomp $file;
28  push(@ARGV,$file);
29}
30
31while(@ARGV) {
32 my $file = shift;
33
34 die "File $file does not exist !" unless -e $file;
35 die "File $file is a directory !" unless !-d $file;
36
37 open(F,"<$file") || die $!;
38 my ($past_use,$key,$algorithm,$found);
39 $found = 0;
40 my @code = ();
41 while(<F>)
42 { if (/^\# $blank/) { $found++; }
43   if (!$past_use)
44   { ($algorithm,$key) = /use Filter\:\:CBC\s*[\'\"](\w*)[\'\"]\s*\,\s*[\'\"]([^\'\"]*)[\'\"].*?/; }
45   if (defined $algorithm && defined $key && !$past_use) { $past_use++; push(@code ,$_); next;}
46   if ($past_use && defined $key && defined $algorithm && $_ ne $/ && $found)
47   { my (@foo) = <F>;
48     unshift (@foo,$_);
49     my $code = join("",@foo);
50     $algorithm ||= "Rijndael";
51     $algorithm = $Algorithms{uc $algorithm} || $algorithm;
52     $key ||= $blank;
53     my $cipher = new Crypt::CBC($key,$algorithm);
54     $code = $cipher->decrypt($code);
55     open(OUTFILE,">$file.out") || die $!;
56     print OUTFILE @code,$code;
57     close(OUTFILE);
58   }
59   else { push(@code,$_); }
60 }
61 close(F);
62}