]> git.zerfleddert.de Git - fhem-stuff/blob - HMLAN/HMLAN_CRYPT.pm
rebase against V1.56
[fhem-stuff] / HMLAN / HMLAN_CRYPT.pm
1 package HMLAN_CRYPT;
2
3 use Crypt::Rijndael;
4
5 sub decrypt(@)
6 {
7 my ($self, $ciphertext) = @_;
8 my $plaintext = '';
9
10 while($ciphertext) {
11 if($self->{'keystream'}) {
12 my $len;
13
14 if (length($self->{'keystream'}) > length($ciphertext)) {
15 $len = length($ciphertext);
16 } else {
17 $len = length($self->{'keystream'});
18 }
19
20 my $cpart = substr($ciphertext, 0, $len);
21 my $kpart = substr($self->{'keystream'}, 0, $len);
22
23 $ciphertext = substr($ciphertext, $len);
24 $self->{'keystream'} = substr($self->{'keystream'}, $len);
25
26 $self->{'ciphertext'} .= $cpart;
27
28 $plaintext .= $cpart ^ $kpart;
29 } else {
30 $self->{'keystream'} = $self->{'cipher'}->encrypt($self->{'ciphertext'});
31 $self->{'ciphertext'}='';
32 }
33 }
34
35 $plaintext;
36 }
37
38 sub encrypt(@)
39 {
40 my ($self, $plaintext) = @_;
41 my $ciphertext = '';
42
43 while($plaintext) {
44 if($self->{'keystream'}) {
45 my $len;
46
47 if (length($self->{'keystream'}) > length($plaintext)) {
48 $len = length($plaintext);
49 } else {
50 $len = length($self->{'keystream'});
51 }
52
53 my $ppart = substr($plaintext, 0, $len);
54 my $kpart = substr($self->{'keystream'}, 0, $len);
55
56 $plaintext = substr($plaintext, $len);
57 $self->{'keystream'} = substr($self->{'keystream'}, $len);
58
59 $self->{'ciphertext'} .= $ppart ^ $kpart;
60
61 $ciphertext .= $ppart ^ $kpart;
62 } else {
63 $self->{'keystream'} = $self->{'cipher'}->encrypt($self->{'ciphertext'});
64 $self->{'ciphertext'}='';
65 }
66 }
67
68 $ciphertext;
69 }
70
71 sub new
72 {
73 my $class = shift;
74 my $key = shift;
75 my $iv = shift;
76
77 my $self = {
78 cipher => Crypt::Rijndael->new($key, Crypt::Rijndael::MODE_ECB()),
79 keystream => '',
80 ciphertext => $iv,
81 };
82
83 bless $self, $class;
84 }
85
86 1;
Impressum, Datenschutz