line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#--------------------------------------------------------------------# |
2
|
|
|
|
|
|
|
# Crypt::RC4 |
3
|
|
|
|
|
|
|
# Date Written: 07-Jun-2000 04:15:55 PM |
4
|
|
|
|
|
|
|
# Last Modified: 13-Dec-2001 03:33:49 PM |
5
|
|
|
|
|
|
|
# Author: Kurt Kincaid (sifukurt@yahoo.com) |
6
|
|
|
|
|
|
|
# Copyright (c) 2001, Kurt Kincaid |
7
|
|
|
|
|
|
|
# All Rights Reserved. |
8
|
|
|
|
|
|
|
# |
9
|
|
|
|
|
|
|
# This is free software and may be modified and/or |
10
|
|
|
|
|
|
|
# redistributed under the same terms as Perl itself. |
11
|
|
|
|
|
|
|
#--------------------------------------------------------------------# |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
package Crypt::RC4; |
14
|
|
|
|
|
|
|
|
15
|
1
|
|
|
1
|
|
636
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
37
|
|
16
|
1
|
|
|
1
|
|
5
|
use vars qw( $VERSION @ISA @EXPORT $MAX_CHUNK_SIZE ); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
624
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
$MAX_CHUNK_SIZE = 1024 unless $MAX_CHUNK_SIZE; |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
require Exporter; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
@ISA = qw(Exporter); |
23
|
|
|
|
|
|
|
@EXPORT = qw(RC4); |
24
|
|
|
|
|
|
|
$VERSION = '2.02'; |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
sub new { |
27
|
11
|
|
|
11
|
0
|
186
|
my ( $class, $key ) = @_; |
28
|
11
|
|
|
|
|
35
|
my $self = bless {}, $class; |
29
|
11
|
|
|
|
|
20
|
$self->{state} = Setup( $key ); |
30
|
11
|
|
|
|
|
20
|
$self->{x} = 0; |
31
|
11
|
|
|
|
|
16
|
$self->{y} = 0; |
32
|
11
|
|
|
|
|
28
|
$self; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub RC4 { |
36
|
207
|
|
|
207
|
0
|
678
|
my $self; |
37
|
207
|
|
|
|
|
174
|
my( @state, $x, $y ); |
38
|
207
|
100
|
|
|
|
339
|
if ( ref $_[0] ) { |
39
|
198
|
|
|
|
|
192
|
$self = shift; |
40
|
198
|
|
|
|
|
168
|
@state = @{ $self->{state} }; |
|
198
|
|
|
|
|
2916
|
|
41
|
198
|
|
|
|
|
267
|
$x = $self->{x}; |
42
|
198
|
|
|
|
|
216
|
$y = $self->{y}; |
43
|
|
|
|
|
|
|
} else { |
44
|
9
|
|
|
|
|
17
|
@state = Setup( shift ); |
45
|
9
|
|
|
|
|
59
|
$x = $y = 0; |
46
|
|
|
|
|
|
|
} |
47
|
207
|
|
|
|
|
244
|
my $message = shift; |
48
|
207
|
|
|
|
|
173
|
my $num_pieces = do { |
49
|
207
|
|
|
|
|
248
|
my $num = length($message) / $MAX_CHUNK_SIZE; |
50
|
207
|
|
|
|
|
225
|
my $int = int $num; |
51
|
207
|
100
|
|
|
|
370
|
$int == $num ? $int : $int+1; |
52
|
|
|
|
|
|
|
}; |
53
|
207
|
|
|
|
|
316
|
for my $piece ( 0..$num_pieces - 1 ) { |
54
|
213
|
|
|
|
|
546
|
my @message = unpack "C*", substr($message, $piece * $MAX_CHUNK_SIZE, $MAX_CHUNK_SIZE); |
55
|
213
|
|
|
|
|
281
|
for ( @message ) { |
56
|
968
|
50
|
|
|
|
1515
|
$x = 0 if ++$x > 255; |
57
|
968
|
100
|
|
|
|
1541
|
$y -= 256 if ($y += $state[$x]) > 255; |
58
|
968
|
|
|
|
|
1340
|
@state[$x, $y] = @state[$y, $x]; |
59
|
968
|
|
|
|
|
1503
|
$_ ^= $state[( $state[$x] + $state[$y] ) % 256]; |
60
|
|
|
|
|
|
|
} |
61
|
213
|
|
|
|
|
644
|
substr($message, $piece * $MAX_CHUNK_SIZE, $MAX_CHUNK_SIZE) = pack "C*", @message; |
62
|
|
|
|
|
|
|
} |
63
|
207
|
100
|
|
|
|
394
|
if ($self) { |
64
|
198
|
|
|
|
|
293
|
$self->{state} = \@state; |
65
|
198
|
|
|
|
|
903
|
$self->{x} = $x; |
66
|
198
|
|
|
|
|
237
|
$self->{y} = $y; |
67
|
|
|
|
|
|
|
} |
68
|
207
|
|
|
|
|
689
|
$message; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
sub Setup { |
72
|
20
|
|
|
20
|
0
|
69
|
my @k = unpack( 'C*', shift ); |
73
|
20
|
|
|
|
|
302
|
my @state = 0..255; |
74
|
20
|
|
|
|
|
22
|
my $y = 0; |
75
|
20
|
|
|
|
|
32
|
for my $x (0..255) { |
76
|
5120
|
|
|
|
|
6338
|
$y = ( $k[$x % @k] + $state[$x] + $y ) % 256; |
77
|
5120
|
|
|
|
|
7359
|
@state[$x, $y] = @state[$y, $x]; |
78
|
|
|
|
|
|
|
} |
79
|
20
|
100
|
|
|
|
373
|
wantarray ? @state : \@state; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
1; |
84
|
|
|
|
|
|
|
__END__ |