Crypt::TripleDES - Triple DES encyption.


Crypt-TripleDES documentation Contained in the Crypt-TripleDES distribution.

Index


Code Index:

NAME

Top

Crypt::TripleDES - Triple DES encyption.

SYNOPSIS

Top

 my $des = new Crypt::TripleDES; 
 my $cyphertext = $des->encrypt3 ( $plaintext, $passphrase );
 my $plaintext = $des->decrypt3 ( $cyphertext, $passphrase );

DESCRIPTION

Top

This module implements 3DES encryption in ECB mode. The code is based on Eric Young's implementation of DES in pure perl. It's quite slow because of the way Perl handles bit operations and is not recommended for use with large texts.

METHODS

Top

new

The constructor.

encrypt3 $plaintext, $passphrase

Encrypts the plaintext string using the passphrase. Whitespace characters are appended to the string if its length is not a multiple of eight. User applications can correct for this by storing plaintext size with the cyphertext. The passphrase is an ASCII character string of upto 48 characters.

decrypt3 $cyphertext, $passphrase

Inverse of encrypt3().

AUTHOR

Top

 Vipul Ved Prakash, mail@vipul.net    
 Eric Young, eay@psych.psy.uq.oz.au

 Patches: 
 Jonathan Mayer <jmayer@cobaltnet.com>


Crypt-TripleDES documentation Contained in the Crypt-TripleDES distribution.

#!/usr/bin/perl 
##
## Crypt::TripleDES -- Pure Perl Triple DES encryption.
##
## Copyright (c) 1999, Vipul Ved Prakash.  All rights reserved.
## This code is free software; you can redistribute it and/or modify
## it under the same terms as Perl itself.
##
## $Id: TripleDES.pm,v 0.24 1999/10/13 23:26:15 root Exp root $

package Crypt::TripleDES; 
use Crypt::PPDES; 
use vars qw( $AUTOLOAD $VERSION); 
( $VERSION )  = '$Revision: 0.24 $' =~ /\s(\d+\.\d+)\s/; 

sub AUTOLOAD { 
    my ( $self, @args ) = @_; 
    my $key = $AUTOLOAD;  $key =~ s/.*://;
    if ( $key eq "encrypt3" ) { 
        return $self->decrypt3 ( @args, 1 ); 
    } 
}

sub new { return bless {}, shift } 

sub decrypt3 { 

    my ( $self, $plaintext, $passphrase, $flag ) = @_; 
    my %keyvecs;
    $passphrase .= ' ' x (16*3); 

    for ( 0..2 ) {  
        my @kvs = Crypt::PPDES::des_set_key( pack( "H*", substr($passphrase, 16*$_, 16 )));
        $keyvecs{$_} = \@kvs;
    }

    my $size = length ( $plaintext );
    my $tail = 8 - ( $size % 8 ); $tail = 0 if $tail > 7;
       $plaintext .= chr(32) x $tail; 
       $size = length ( $plaintext );
    my $cyphertext = "";

    for ( 0 .. (($size)/8) -1 ) { 
     my $pt = substr( $plaintext, $_*8, 8 );
        $pt = Crypt::PPDES::des_ecb_encrypt( $flag ? $keyvecs{0} : $keyvecs{2}, $flag, $pt );
        $pt = Crypt::PPDES::des_ecb_encrypt( $keyvecs{1}, (not $flag), $pt );
        $pt = Crypt::PPDES::des_ecb_encrypt( $flag ? $keyvecs{2} : $keyvecs{0}, $flag, $pt );
        $cyphertext .= $pt; 
    } 

    return substr ( $cyphertext, 0, $size );

}

sub debug { 
    my ( @mess ) = @_; 
    open D, ">>debug"; 
    print D "@mess\n"; 
    close D; 
}

"True Value";