line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# $Id: RC4.pm,v 1.5 2001/05/04 08:58:22 btrott Exp $ |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
package Net::SSH::Perl::Cipher::RC4; |
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
7
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
34
|
|
6
|
1
|
|
|
1
|
|
7
|
use warnings; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
35
|
|
7
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
8
|
use Net::SSH::Perl::Cipher; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
31
|
|
9
|
1
|
|
|
1
|
|
7
|
use base qw( Net::SSH::Perl::Cipher ); |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
654
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub new { |
12
|
0
|
|
|
0
|
1
|
|
my $class = shift; |
13
|
0
|
|
|
|
|
|
my $ciph = bless { }, $class; |
14
|
0
|
0
|
|
|
|
|
$ciph->init(@_) if @_; |
15
|
0
|
|
|
|
|
|
$ciph; |
16
|
|
|
|
|
|
|
} |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
sub init { |
19
|
0
|
|
|
0
|
0
|
|
my $ciph = shift; |
20
|
0
|
|
|
|
|
|
my($key, $iv) = @_; |
21
|
0
|
|
|
|
|
|
$ciph->{key} = substr($key, 0, $ciph->keysize); |
22
|
|
|
|
|
|
|
|
23
|
0
|
|
|
|
|
|
$key = substr($key, 0, $ciph->keysize); |
24
|
0
|
|
|
|
|
|
my @k = unpack 'C*', $key; |
25
|
0
|
|
|
|
|
|
my @s = 0..255; |
26
|
0
|
|
|
|
|
|
my($y) = (0); |
27
|
0
|
|
|
|
|
|
for my $x (0..255) { |
28
|
0
|
|
|
|
|
|
$y = ($k[$x % @k] + $s[$x] + $y) % 256; |
29
|
0
|
|
|
|
|
|
@s[$x, $y] = @s[$y, $x]; |
30
|
|
|
|
|
|
|
} |
31
|
0
|
|
|
|
|
|
$ciph->{s} = \@s; |
32
|
0
|
|
|
|
|
|
$ciph->{x} = 0; |
33
|
0
|
|
|
|
|
|
$ciph->{y} = 0; |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
0
|
|
|
0
|
0
|
|
sub blocksize { 8 } |
37
|
0
|
|
|
0
|
0
|
|
sub keysize { 16 } |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub encrypt { |
40
|
0
|
|
|
0
|
1
|
|
my($ciph, $text) = @_; |
41
|
0
|
|
|
|
|
|
$text = RC4($ciph, $text); |
42
|
0
|
|
|
|
|
|
$text; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
sub decrypt { |
46
|
0
|
|
|
0
|
1
|
|
my($ciph, $text) = @_; |
47
|
0
|
|
|
|
|
|
$text = RC4($ciph, $text); |
48
|
0
|
|
|
|
|
|
$text; |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
sub RC4 { |
52
|
0
|
|
|
0
|
0
|
|
my($ciph, $text) = @_; |
53
|
0
|
|
|
|
|
|
my($x, $y, $trans) = ($ciph->{x}, $ciph->{y}, ''); |
54
|
0
|
|
|
|
|
|
my $s = $ciph->{s}; |
55
|
0
|
|
|
|
|
|
for my $c (unpack 'C*', $text) { |
56
|
0
|
|
|
|
|
|
$x = ($x + 1) % 256; |
57
|
0
|
|
|
|
|
|
$y = ( $s->[$x] + $y ) % 256; |
58
|
0
|
|
|
|
|
|
@$s[$x, $y] = @$s[$y, $x]; |
59
|
0
|
|
|
|
|
|
$trans .= pack('C', $c ^= $s->[( $s->[$x] + $s->[$y] ) % 256]); |
60
|
|
|
|
|
|
|
} |
61
|
0
|
|
|
|
|
|
$ciph->{x} = $x; |
62
|
0
|
|
|
|
|
|
$ciph->{y} = $y; |
63
|
0
|
|
|
|
|
|
$trans; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
1; |
67
|
|
|
|
|
|
|
__END__ |