Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Netcat like with Blowfish

by Anarion (Hermit)
on Sep 02, 2001 at 19:16 UTC ( #109739=sourcecode: print w/replies, xml ) Need Help??
Category: Networking Code
Author/Contact Info anarion@manresa.net
Description: I usually use netcat. I just want to send some archives encrypted, so i make this program to do it. It uses Blowfish, you need to downoad it from cpan.org.
#!/usr/bin/perl -w
#
# Anarion
# 02-09-2001
#
# FileTransfer encryptet with Blowfish :)
# You need the Blowfish module from www.cpan.org
# 
#
use IO::Socket;
use Crypt::Blowfish;
use Getopt::Std;
use strict;

my %opcions;
getopts('hd:lp:o:kK:',\%opcions) or ajuda();
ajuda() if $opcions{h} || (! $opcions{d} and ! $opcions{l}) || ! $opci
+ons{p};

my $key = exists $opcions{k} && $opcions{k}==1 && llegeix_clau() || ex
+ists $opcions{K} && length($opcions{K}) == 8 && $opcions{K} || '';
print $key ?  "Using your key\n" : "No valid key introduced, skip encr
+iption\n";

server() if $opcions{l};
client() if $opcions{d};

######
#
sub ajuda
{
die <<AJUDA;
Version [1.1] by Anarion
Connect to somewhere:      $0 -d host -p port [-options]
Inbound connects:      $0 -l      -p port [-options]
Options:
        -o output    Redirect the output 
         -k         Read the key from STDIN 
        -K key         use this key [8 chars]

AJUDA
}

######
#
sub llegeix_clau
{
    local $| = 1;
    local *TTY;
    my ($clau1,$clau2);
    open(TTY,"/dev/tty");
    system "stty -echo </dev/tty";
    print STDERR "the key must be 8 chars long\nType the key: ";
    chomp($clau1 = <TTY>);
    print STDERR "\nRe-type key: ";
    chomp($clau2 = <TTY>);
    print "\n\r";
    print "Keys dont match\n" and return unless $clau1 eq $clau2;
    print "The password must be 8 chars long\n" and return if length($
+clau1) != 8;
    return $clau1;
}

######
#
sub server
{
    my $socket = IO::Socket::INET->new(LocalPort => $opcions{p},
                       Listen    => 2,
                       Reuse     => 1,
                       Type         => SOCK_STREAM,
                       Proto     => 'tcp',
                       ) or die "Cant bind socket $opcions{p}: $!";
    my ($client,$resposta);

    while ($client = $socket->accept())
    {
        print "Conection from: ",$client->peerhost(),"\nAccept it? [y/
+n]";
        chop($resposta = <STDIN>);
        last if $resposta eq 'y';
        close($client);
    }
    print "\n";
    open(FITXER,">$opcions{o}") || die "Sucks cant create $opcions{o}\
+n" if $opcions{o};
    my $sortida = *FITXER;
    $sortida = *STDOUT unless $opcions{o};
    my $blow = new Crypt::Blowfish $key if $key;
    while(<$client>)
    {
        chomp($_);
        $_ = pack("H*",$_);
        if ($key)
        {
            my @blocks = unpack("a8" x (int(length($_) / 8)+(length($_
+)%8!=0)),$_);
            my $text;
            foreach my $block (@blocks)
            {
                last unless $block;
                $text .= $blow->decrypt($block);
            }
            $text=~s/ *$//;
            print $sortida "$text\n";
            next;
        }
        print $sortida "$_\n";
    }
    exit;
}

##
#
sub client
{
    my $socket = IO::Socket::INET->new(PeerPort => $opcions{p},
                       PeerAddr => $opcions{d},
                       Type         => SOCK_STREAM,
                       Proto     => 'tcp',
                       ) or die "Cant connect to $opcions{d} $opcions{
+p}: $!";


    my $blow = new Crypt::Blowfish $key if $key;

    while(1)
    {
        last unless $socket;
        chomp($_ = <STDIN>);
        last unless $_;
        if ($key)
        {
            my @blocks = unpack("a8" x (int(length($_) / 8)+(length($_
+)%8!=0)),$_);
            my $text;
            foreach my $block (@blocks)
            {
                $block .= " " x (8 - length($block)) if length($block)
+ != 8;
                $text .= $blow->encrypt($block);
            }
            $text= unpack("H*",$text);
            print $socket "$text\n";
            next;
        }
        $_ = unpack("H*",$_);
        print $socket "$_\n";
    }
    close($socket);
    exit;                               
}
Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://109739]
help
Chatterbox?
[1nickt]: my $x = $aohoaoh->[0]->{' foo'}->[0]->{'bar' }; should work
[thepkd]: not indexing
[thepkd]: getting data out of the ds
[choroba]: it depends. Give more details, please
[thepkd]: i used a series of {}'s but it dont work
[choroba]: You need square brackets for arrays
[GotToBTru]: you can certainly construct a single expression to access any part of the data structure, without using temp variables. but you might make it easier on yourself and any other poor soul who has to understand your code if you do
[thepkd]: to dereference i mean
LanX .oO( oh tempz, oh moretz)
[thepkd]: @GotToBTru Sure. But.

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (13)
As of 2016-12-06 13:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:













    Results (104 votes). Check out past polls.