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 / DES_EDE3.pm < prev    next >
Encoding:
Perl POD Document  |  2001-09-15  |  2.7 KB  |  119 lines

  1. # $Id: DES_EDE3.pm,v 1.2 2001/09/15 03:41:09 btrott Exp $
  2.  
  3. package Crypt::DES_EDE3;
  4. use strict;
  5.  
  6. use Crypt::DES;
  7. use vars qw( $VERSION );
  8. $VERSION = '0.01';
  9.  
  10. sub new {
  11.     my $class = shift;
  12.     my $ede3 = bless {}, $class;
  13.     $ede3->init(@_);
  14. }
  15.  
  16. sub keysize   { 24 }
  17. sub blocksize { 8 }
  18.  
  19. sub init {
  20.     my $ede3 = shift;
  21.     my($key) = @_;
  22.     for my $i (1..3) {
  23.         $ede3->{"des$i"} = Crypt::DES->new(substr $key, 8*($i-1), 8);
  24.     }
  25.     $ede3;
  26. }
  27.  
  28. sub encrypt {
  29.     my($ede3, $block) = @_;
  30.     $ede3->{des3}->encrypt(
  31.         $ede3->{des2}->decrypt(
  32.             $ede3->{des1}->encrypt($block)
  33.         )
  34.     );
  35. }
  36.  
  37. sub decrypt {
  38.     my($ede3, $block) = @_;
  39.     $ede3->{des1}->decrypt(
  40.         $ede3->{des2}->encrypt(
  41.             $ede3->{des3}->decrypt($block)
  42.         )
  43.     );
  44. }
  45.  
  46. 1;
  47. __END__
  48.  
  49. =head1 NAME
  50.  
  51. Crypt::DES_EDE3 - Triple-DES EDE encryption/decryption
  52.  
  53. =head1 SYNOPSIS
  54.  
  55.     use Crypt::DES_EDE3;
  56.     my $ede3 = Crypt::DES_EDE3->new($key);
  57.     $ede3->encrypt($block);
  58.  
  59. =head1 DESCRIPTION
  60.  
  61. I<Crypt::DES_EDE3> implements DES-EDE3 encryption. This is triple-DES
  62. encryption where an encrypt operation is encrypt-decrypt-encrypt, and
  63. decrypt is decrypt-encrypt-decrypt. This implementation uses I<Crypt::DES>
  64. to do its dirty DES work, and simply provides a wrapper around that
  65. module: setting up the individual DES ciphers, initializing the keys,
  66. and performing the encryption/decryption steps.
  67.  
  68. DES-EDE3 encryption requires a key size of 24 bytes.
  69.  
  70. You're probably best off not using this module directly, as the I<encrypt>
  71. and I<decrypt> methods expect 8-octet blocks. You might want to use the
  72. module in conjunction with I<Crypt::CBC>, for example. This would be
  73. DES-EDE3-CBC, or triple-DES in outer CBC mode.
  74.  
  75. =head1 USAGE
  76.  
  77. =head2 $ede3 = Crypt::DES_EDE3->new($key)
  78.  
  79. Creates a new I<Crypt::DES_EDE3> object (really, a collection of three DES
  80. ciphers), and initializes each cipher with part of I<$key>, which should be
  81. at least 24 bytes. If it's longer than 24 bytes, the extra bytes will be
  82. ignored.
  83.  
  84. Returns the new object.
  85.  
  86. =head2 $ede3->encrypt($block)
  87.  
  88. Encrypts an 8-byte block of data I<$block> using the three DES ciphers in
  89. an encrypt-decrypt-encrypt operation.
  90.  
  91. Returns the encrypted block.
  92.  
  93. =head2 $ede3->decrypt($block)
  94.  
  95. Decrypts an 8-byte block of data I<$block> using the three DES ciphers in
  96. a decrypt-encrypt-decrypt operation.
  97.  
  98. Returns the decrypted block.
  99.  
  100. =head2 $ede3->blocksize
  101.  
  102. Returns the block size (8).
  103.  
  104. =head2 $ede3->keysize
  105.  
  106. Returns the key size (24).
  107.  
  108. =head1 LICENSE
  109.  
  110. Crypt::DES_EDE3 is free software; you may redistribute it and/or modify
  111. it under the same terms as Perl itself.
  112.  
  113. =head1 AUTHOR & COPYRIGHTS
  114.  
  115. Crypt::DES_EDE3 is Copyright 2001 Benjamin Trott, ben@rhumba.pair.com. All
  116. rights reserved.
  117.  
  118. =cut
  119.