]> git.zerfleddert.de Git - fhem-stuff/blob - HMLAN/hmlan-crypt-test.pl
don't get confused when more than one packet is received
[fhem-stuff] / HMLAN / hmlan-crypt-test.pl
1 #!/usr/bin/perl -w
2
3 # key can either be 32 HEX chars (HMLAN) or a password (wired gateway)
4 #my $key="00112233445566778899AABBCCDDEEFF";
5 #my $key="pA$$w0rd1!";
6
7 my $key="00112233445566778899AABBCCDDEEFF";
8
9 # ip address
10 my $hmlan="192.168.1.2";
11
12 BEGIN {
13 use File::Basename;
14 push @INC, dirname($0);
15 }
16
17 use HMLAN_CRYPT;
18 use IO::Socket::INET;
19 use IO::Select;
20 use Digest::MD5;
21
22 #This should be random...
23 my $my_iv = '00112233445566778899AABBCCDDEEFF';
24 my $hmlan_iv = '';
25
26 my $sock = IO::Socket::INET->new(
27 PeerAddr => $hmlan,
28 PeerPort => 1000,
29 Proto => 'tcp',
30 Blocking => 0);
31
32 my $read_set = new IO::Select();
33 $read_set->add($sock);
34
35 $read_set->can_read();
36
37 chomp(my $iv = <$sock>);
38 if ($iv =~ /^H/) {
39 print "${iv}\n";
40 chomp($iv = <$sock>);
41 }
42
43 if ($iv =~ /^V(................................)\r$/) {
44 $hmlan_iv = $1;
45 print $sock "V${my_iv}\r\n";
46 } elsif ($iv =~ /^V(..),(................................)\r$/) {
47 $hmlan_iv = $2;
48 print $sock "V01,${my_iv}\r\n";
49 } else {
50 print STDERR "Unknown IV received: ${iv}\n";
51 exit(1);
52 }
53
54 if (length($key) < 32) {
55 $key = Digest::MD5::md5($key);
56 } else {
57 $key = pack("H*", $key);
58 }
59
60 my $cipher_hmlan=HMLAN_CRYPT->new($key, pack("H*", $my_iv));
61 my $cipher_pc=HMLAN_CRYPT->new($key, pack("H*", $hmlan_iv));
62
63 $read_set->add(\*STDIN);
64
65 while(1) {
66 my @ready = $read_set->can_read();
67 foreach my $r (@ready) {
68 if ($r == $sock) {
69 while(my $buf=<$r>) {
70 print $cipher_hmlan->decrypt($buf);
71 }
72 } else {
73 my $buf = <$r>;
74 exit(1) if (!$buf);
75
76 print $sock $cipher_pc->encrypt($buf);
77 }
78 }
79 }
Impressum, Datenschutz