TripleDES.pm

filter: Statementstmt 21.6% 8 / 37 Branchbran 0.0% 0 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 33.3% 2 / 6 Podpod 66.6% 2 / 3 totaltotal 22.2% 12 / 54 CC 7 SCAR 31.1
CC
7
Cov
18%
decrypt3
42.0
AUTOLOAD
6.0
new
2.0
CRAP 22.5
↑ Summary PPDES.pm »
1 #!/usr/bin/perl
2 ##
3 ## Crypt::TripleDES -- Pure Perl Triple DES encryption.
4 ##
5 ## Copyright (c) 1999, Vipul Ved Prakash. All rights reserved.
6 ## This code is free software; you can redistribute it and/or modify
7 ## it under the same terms as Perl itself.
8 ##
9 ## $Id: TripleDES.pm,v 0.24 1999/10/13 23:26:15 root Exp root $
10  
11 package Crypt::TripleDES;
12 1 ▶use Crypt::PPDES;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use vars qw( $AUTOLOAD $VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ( $VERSION ) = '$Revision: 0.24 $' =~ /\s(\d+\.\d+)\s/;
15  
16 0 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: not called CC=2 SCAR=17.9
17 0 my ( $self, @args ) = @_;
18 0 my $key = $AUTOLOAD; $key =~ s/.*://;
19 0 ▶ if ( $key eq "encrypt3" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "encrypt3") 0 0 0
20 0 return $self->decrypt3 ( @args, 1 );
21 }
22 }
23  
24 0 ▶sub new { return bless {}, shift }
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
25  
26 0 ▶sub decrypt3 {
Subroutine decrypt3: not called CC=6 SCAR=37.4
Pod documented
27  
28 0 my ( $self, $plaintext, $passphrase, $flag ) = @_;
29 0 my %keyvecs;
30 0 $passphrase .= ' ' x (16*3);
31  
32 0 for ( 0..2 ) {
33 0 my @kvs = Crypt::PPDES::des_set_key( pack( "H*", substr($passphrase, 16*$_, 16 )));
34 0 $keyvecs{$_} = \@kvs;
35 }
36  
37 0 my $size = length ( $plaintext );
38 0 ▶ my $tail = 8 - ( $size % 8 ); $tail = 0 if $tail > 7;
Branch: 1 branch
BranchTrueFalseTotal
if $tail > 7 0 0 0
39 0 $plaintext .= chr(32) x $tail;
40 0 $size = length ( $plaintext );
41 0 my $cyphertext = "";
42  
43 0 for ( 0 .. (($size)/8) -1 ) {
44 0 my $pt = substr( $plaintext, $_*8, 8 );
45 0 ▶ $pt = Crypt::PPDES::des_ecb_encrypt( $flag ? $keyvecs{0} : $keyvecs{2}, $flag, $pt );
Branch: 1 branch
BranchTrueFalseTotal
$flag ? : 0 0 0
46 0 $pt = Crypt::PPDES::des_ecb_encrypt( $keyvecs{1}, (not $flag), $pt );
47 0 ▶ $pt = Crypt::PPDES::des_ecb_encrypt( $flag ? $keyvecs{2} : $keyvecs{0}, $flag, $pt );
Branch: 1 branch
BranchTrueFalseTotal
$flag ? : 0 0 0
48 0 $cyphertext .= $pt;
49 }
50  
51 0 return substr ( $cyphertext, 0, $size );
52  
53 }
54  
55 0 ▶sub debug {
Subroutine debug: not called CC=1 SCAR=6.9
Pod undocumented
56 0 my ( @mess ) = @_;
57 0 open D, ">>debug";
58 0 print D "@mess\n";
59 0 close D;
60 }
61  
62 1 "True Value";
63  
64 =head1 NAME
65  
66 Crypt::TripleDES - Triple DES encyption.
67  
68 =head1 SYNOPSIS
69  
70 my $des = new Crypt::TripleDES;
71 my $cyphertext = $des->encrypt3 ( $plaintext, $passphrase );
72 my $plaintext = $des->decrypt3 ( $cyphertext, $passphrase );
73  
74 =head1 DESCRIPTION
75  
76 This module implements 3DES encryption in ECB mode. The code is based on
77 Eric Young's implementation of DES in pure perl. It's quite slow because of
78 the way Perl handles bit operations and is not recommended for use with
79 large texts.
80  
81 =head1 METHODS
82  
83 =over 4
84  
85 =item B<new>
86  
87 The constructor.
88  
89 =item B<encrypt3> $plaintext, $passphrase
90  
91 Encrypts the plaintext string using the passphrase. Whitespace characters
92 are appended to the string if its length is not a multiple of eight. User
93 applications can correct for this by storing plaintext size with the
94 cyphertext. The passphrase is an ASCII character string of upto 48
95 characters.
96  
97 =item B<decrypt3> $cyphertext, $passphrase
98  
99 Inverse of encrypt3().
100  
101 =back
102  
103 =head1 AUTHOR
104  
105 Vipul Ved Prakash, mail@vipul.net
106 Eric Young, eay@psych.psy.uq.oz.au
107  
108 Patches:
109 Jonathan Mayer <jmayer@cobaltnet.com>
110  
111 =cut
112  
113  
↑ Summary PPDES.pm »