<?xml version="1.0" encoding="windows-1252"?>
<node id="109739" title="Netcat like with Blowfish" created="2001-09-02 15:16:14" updated="2005-08-12 13:30:42">
<type id="1748">
sourcecode</type>
<author id="70091">
Anarion</author>
<data>
<field name="doctext">
&lt;code&gt;
#!/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}) || ! $opcions{p};

my $key = exists $opcions{k} &amp;&amp; $opcions{k}==1 &amp;&amp; llegeix_clau() || exists $opcions{K} &amp;&amp; length($opcions{K}) == 8 &amp;&amp; $opcions{K} || '';
print $key ?  "Using your key\n" : "No valid key introduced, skip encription\n";

server() if $opcions{l};
client() if $opcions{d};

######
#
sub ajuda
{
die &lt;&lt;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 &lt;/dev/tty";
	print STDERR "the key must be 8 chars long\nType the key: ";
	chomp($clau1 = &lt;TTY&gt;);
	print STDERR "\nRe-type key: ";
	chomp($clau2 = &lt;TTY&gt;);
	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-&gt;new(LocalPort =&gt; $opcions{p},
					   Listen    =&gt; 2,
					   Reuse     =&gt; 1,
					   Type	     =&gt; SOCK_STREAM,
					   Proto     =&gt; 'tcp',
					   ) or die "Cant bind socket $opcions{p}: $!";
	my ($client,$resposta);

	while ($client = $socket-&gt;accept())
	{
		print "Conection from: ",$client-&gt;peerhost(),"\nAccept it? [y/n]";
		chop($resposta = &lt;STDIN&gt;);
		last if $resposta eq 'y';
		close($client);
	}
	print "\n";
	open(FITXER,"&gt;$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(&lt;$client&gt;)
	{
		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-&gt;decrypt($block);
			}
			$text=~s/ *$//;
			print $sortida "$text\n";
			next;
		}
		print $sortida "$_\n";
	}
	exit;
}

##
#
sub client
{
	my $socket = IO::Socket::INET-&gt;new(PeerPort =&gt; $opcions{p},
					   PeerAddr =&gt; $opcions{d},
					   Type	     =&gt; SOCK_STREAM,
					   Proto     =&gt; 'tcp',
					   ) or die "Cant connect to $opcions{d} $opcions{p}: $!";


	my $blow = new Crypt::Blowfish $key if $key;

	while(1)
	{
		last unless $socket;
		chomp($_ = &lt;STDIN&gt;);
		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-&gt;encrypt($block);
			}
			$text= unpack("H*",$text);
			print $socket "$text\n";
			next;
		}
		$_ = unpack("H*",$_);
		print $socket "$_\n";
	}
	close($socket);
	exit;							   
}
&lt;/code&gt;</field>
<field name="codedescription">
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.</field>
<field name="codecategory">
Networking Code</field>
<field name="codeauthor">
anarion@manresa.net</field>
</data>
</node>
