line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# easy en/decryption with DES/IDEA/Blowfish and some other ciphers |
2
|
|
|
|
|
|
|
# Mike Blazer |
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
package Crypt::CBCeasy; |
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
22090
|
use 5.003; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
55
|
|
7
|
1
|
|
|
1
|
|
5
|
use Crypt::CBC; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
25
|
|
8
|
1
|
|
|
1
|
|
6
|
use Carp; |
|
1
|
|
|
|
|
8
|
|
|
1
|
|
|
|
|
103
|
|
9
|
1
|
|
|
1
|
|
3272
|
use Symbol; |
|
1
|
|
|
|
|
1295
|
|
|
1
|
|
|
|
|
98
|
|
10
|
|
|
|
|
|
|
|
11
|
1
|
|
|
1
|
|
7
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
41
|
|
12
|
1
|
|
|
1
|
|
5
|
no strict 'refs'; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
30
|
|
13
|
1
|
|
|
1
|
|
6
|
use vars qw($VERSION @DEFAULT_CIPHERS $LastCipher); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
899
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
$VERSION = '0.24'; |
16
|
|
|
|
|
|
|
@DEFAULT_CIPHERS = qw/DES IDEA Blowfish/; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
#-------------- |
20
|
|
|
|
|
|
|
sub useCBC { |
21
|
|
|
|
|
|
|
#-------------- |
22
|
|
|
|
|
|
|
# $from - handler (r), filename or just plain or encrypted text |
23
|
|
|
|
|
|
|
# $to - handler (r), or filename. If '' or undef sub returns $to-string |
24
|
0
|
|
|
0
|
0
|
0
|
my ($key, $from, $to) = @_; |
25
|
0
|
|
|
|
|
0
|
my $sub = (caller(1))[3]; # caller subroutine |
26
|
0
|
|
|
|
|
0
|
my ($algorithm, $op) = $sub =~ /^(.*)::(.*)$/; |
27
|
|
|
|
|
|
|
#print "$algorithm, $op\n"; |
28
|
0
|
|
|
|
|
0
|
$LastCipher = $algorithm; |
29
|
|
|
|
|
|
|
|
30
|
0
|
|
|
|
|
0
|
my ($fhi, $fho, $fromFile, $INopened, $OUTopened, |
31
|
|
|
|
|
|
|
$buffer, $fromStr, $toStr, $cipher); |
32
|
|
|
|
|
|
|
|
33
|
0
|
0
|
|
|
|
0
|
croak "CBCeasy: source not defined\n" unless defined $from; |
34
|
0
|
0
|
|
|
|
0
|
croak "CBCeasy: key not defined\n" unless defined $key; |
35
|
0
|
0
|
0
|
|
|
0
|
croak "CBCeasy: I can do only `encipher' or `decipher'\n" |
36
|
|
|
|
|
|
|
unless $op && $op =~ /^(encipher|decipher)$/i; |
37
|
|
|
|
|
|
|
|
38
|
0
|
0
|
0
|
|
|
0
|
if ((UNIVERSAL::isa($from, 'GLOB') || # \*HANDLE |
|
|
0
|
0
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
39
|
|
|
|
|
|
|
UNIVERSAL::isa(\$from,'GLOB') # *HANDLE |
40
|
|
|
|
|
|
|
) && defined fileno $from |
41
|
|
|
|
|
|
|
) { |
42
|
|
|
|
|
|
|
|
43
|
0
|
|
|
|
|
0
|
$fhi = $from; |
44
|
0
|
|
|
|
|
0
|
$fromFile = 1; |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
} elsif (-e $from && -r _) { # filename |
47
|
0
|
|
|
|
|
0
|
$fhi = gensym; |
48
|
0
|
|
|
|
|
0
|
$fromFile = 1; |
49
|
0
|
|
|
|
|
0
|
$INopened = 1; |
50
|
0
|
0
|
|
|
|
0
|
open ($fhi, $from) || croak "CBCeasy: file `$from' not found/readable\n"; |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
} elsif (-e $from && !-r _) { # filename |
53
|
0
|
|
|
|
|
0
|
croak "CBCeasy: file `$from' not readable\n"; |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
} else { # stream itself in $from |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
0
|
|
|
|
|
0
|
$cipher = new Crypt::CBC($key, $algorithm); |
59
|
0
|
|
|
|
|
0
|
$cipher->start(lc $op); |
60
|
|
|
|
|
|
|
|
61
|
0
|
0
|
|
|
|
0
|
if ($fromFile) { |
62
|
|
|
|
|
|
|
|
63
|
0
|
|
|
|
|
0
|
binmode $fhi; |
64
|
|
|
|
|
|
|
# fails with too long chains |
65
|
0
|
|
|
|
|
0
|
while (read($fhi,$buffer,4096)) { |
66
|
0
|
|
|
|
|
0
|
$toStr .= $cipher->crypt($buffer); |
67
|
|
|
|
|
|
|
} |
68
|
0
|
|
|
|
|
0
|
$toStr .= $cipher->finish; |
69
|
|
|
|
|
|
|
|
70
|
0
|
0
|
|
|
|
0
|
close $fhi if $INopened; |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
} else { |
73
|
|
|
|
|
|
|
# fails with too long chains |
74
|
0
|
|
|
|
|
0
|
while ($from) { |
75
|
0
|
|
|
|
|
0
|
$fromStr = substr($from, 0, 4096); |
76
|
0
|
|
|
|
|
0
|
substr($from, 0, 4096) = ''; |
77
|
0
|
|
|
|
|
0
|
$toStr .= $cipher->crypt($fromStr); |
78
|
|
|
|
|
|
|
} |
79
|
0
|
|
|
|
|
0
|
$toStr .= $cipher->finish; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
0
|
0
|
|
|
|
0
|
return $toStr unless $to; |
83
|
|
|
|
|
|
|
|
84
|
0
|
0
|
0
|
|
|
0
|
if ((UNIVERSAL::isa($to, 'GLOB') || # \*HANDLE |
|
|
|
0
|
|
|
|
|
85
|
|
|
|
|
|
|
UNIVERSAL::isa(\$to,'GLOB') # *HANDLE |
86
|
|
|
|
|
|
|
) && defined fileno $to |
87
|
|
|
|
|
|
|
) { |
88
|
|
|
|
|
|
|
|
89
|
0
|
|
|
|
|
0
|
$fho = $to; |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
} else { # filename |
92
|
0
|
|
|
|
|
0
|
$fho = gensym; |
93
|
0
|
|
|
|
|
0
|
$OUTopened = 1; |
94
|
0
|
0
|
|
|
|
0
|
open ($fho, ">$to") || croak "CBCeasy: can't write file `$to'\n"; |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
0
|
|
|
|
|
0
|
binmode $fho; |
99
|
0
|
|
|
|
|
0
|
print $fho $toStr; |
100
|
|
|
|
|
|
|
|
101
|
0
|
0
|
|
|
|
0
|
close $fho if $OUTopened; |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
#-------------- |
106
|
|
|
|
|
|
|
sub import { |
107
|
1
|
|
|
1
|
|
10
|
my $pkg = shift; |
108
|
|
|
|
|
|
|
|
109
|
1
|
50
|
|
|
|
6
|
for (@_ ? @_ : @DEFAULT_CIPHERS) { |
110
|
3
|
50
|
|
0
|
|
4
|
eval <<"E_O_P" unless defined *{"$_\::encipher"}{CODE}; |
|
3
|
|
|
0
|
|
383
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
sub $_\::encipher { useCBC(\@_) } |
113
|
|
|
|
|
|
|
sub $_\::decipher { useCBC(\@_) } |
114
|
|
|
|
|
|
|
E_O_P |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
1; |
120
|
|
|
|
|
|
|
__END__ |