home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / perl / Blowfish.pm < prev    next >
Encoding:
Perl POD Document  |  2001-05-07  |  2.9 KB  |  109 lines

  1. # $Id: Blowfish.pm,v 1.14 2001/05/08 02:55:40 btrott Exp $
  2.  
  3. package Net::SSH::Perl::Cipher::Blowfish;
  4.  
  5. use strict;
  6.  
  7. use Net::SSH::Perl::Cipher;
  8. use base qw( Net::SSH::Perl::Cipher );
  9.  
  10. use Net::SSH::Perl::Cipher::CBC;
  11.  
  12. use vars qw( $BF_CLASS );
  13. BEGIN {
  14.     my @err;
  15.     for my $mod (qw( Crypt::Blowfish Crypt::Blowfish_PP )) {
  16.         eval "use $mod;";
  17.         $BF_CLASS = $mod, last unless $@;
  18.         push @err, $@;
  19.     }
  20.     die "Failed to load Crypt::Blowfish and Crypt::Blowfish_PP: @err"
  21.         unless $BF_CLASS;
  22. }
  23.  
  24. sub new {
  25.     my $class = shift;
  26.     my $ciph = bless { }, $class;
  27.     $ciph->init(@_) if @_;
  28.     $ciph;
  29. }
  30.  
  31. sub keysize { 16 }
  32. sub blocksize { 8 }
  33.  
  34. sub init {
  35.     my $ciph = shift;
  36.     my($key, $iv, $is_ssh2) = @_;
  37.     my $blow = $BF_CLASS->new($is_ssh2 ? substr($key, 0, 16) : $key);
  38.     $ciph->{cbc} = Net::SSH::Perl::Cipher::CBC->new($blow,
  39.         $iv ? substr($iv, 0, 8) : undef);
  40.     $ciph->{is_ssh2} = defined $is_ssh2 ? $is_ssh2 : 0;
  41. }
  42.  
  43. sub encrypt {
  44.     my($ciph, $text) = @_;
  45.     $ciph->{is_ssh2} ?
  46.         $ciph->{cbc}->encrypt($text) :
  47.         _swap_bytes($ciph->{cbc}->encrypt(_swap_bytes($text)));
  48. }
  49.  
  50. sub decrypt {
  51.     my($ciph, $text) = @_;
  52.     $ciph->{is_ssh2} ?
  53.         $ciph->{cbc}->decrypt($text) :
  54.         _swap_bytes($ciph->{cbc}->decrypt(_swap_bytes($text)));
  55. }
  56.  
  57. sub _swap_bytes {
  58.     my $str = $_[0];
  59.     $str =~ s/(.{4})/reverse $1/sge;
  60.     $str;
  61. }
  62.  
  63. 1;
  64. __END__
  65.  
  66. =head1 NAME
  67.  
  68. Net::SSH::Perl::Cipher::Blowfish - Wrapper for SSH Blowfish support
  69.  
  70. =head1 SYNOPSIS
  71.  
  72.     use Net::SSH::Perl::Cipher;
  73.     my $cipher = Net::SSH::Perl::Cipher->new('Blowfish', $key);
  74.     print $cipher->encrypt($plaintext);
  75.  
  76. =head1 DESCRIPTION
  77.  
  78. I<Net::SSH::Perl::Cipher::Blowfish> provides Blowfish encryption
  79. support for I<Net::SSH::Perl>. To do so it wraps around either
  80. I<Crypt::Blowfish> or I<Crypt::Blowfish_PP>; the former is a
  81. C/XS implementation of the blowfish algorithm, and the latter is
  82. a Perl implementation. I<Net::SSH::Perl::Cipher::Blowfish> prefers
  83. to use I<Crypt::Blowfish>, because it's faster, so we try to load
  84. that first. If it fails, we fall back to I<Crypt::Blowfish_PP>.
  85. Note that, when using I<Crypt::Blowfish_PP>, you'll experience
  86. a very noticeable decrease in performance.
  87.  
  88. The blowfish used here is in CBC filter mode with a key length
  89. of 32 bytes.
  90.  
  91. SSH1 adds an extra wrinkle with respect to its blowfish algorithm:
  92. before and after encryption/decryption, we have to swap the bytes
  93. in the string to be encrypted/decrypted. The byte-swapping is done
  94. four bytes at a time, and within each of those four-byte blocks
  95. we reverse the bytes. So, for example, the string C<foobarba>
  96. turns into C<boofabra>. We swap the bytes in this manner in the
  97. string before we encrypt/decrypt it, and swap the
  98. encrypted/decrypted string again when we get it back.
  99.  
  100. This byte-swapping is not done when Blowfish is used in the
  101. SSH2 protocol.
  102.  
  103. =head1 AUTHOR & COPYRIGHTS
  104.  
  105. Please see the Net::SSH::Perl manpage for author, copyright,
  106. and license information.
  107.  
  108. =cut
  109.