Commit | Line | Data |
---|---|---|
86530b38 AT |
1 | # |
2 | # Copyright (C) 1995, 1996 Systemics Ltd (http://www.systemics.com/) | |
3 | # All rights reserved. | |
4 | # | |
5 | # Modifications are Copyright (c) 2000, W3Works, LLC | |
6 | # All Rights Reserved. | |
7 | ||
8 | package Crypt::DES; | |
9 | ||
10 | require Exporter; | |
11 | require DynaLoader; | |
12 | use vars qw($VERSION @ISA @EXPORT @EXPORT_OK); | |
13 | ||
14 | @ISA = qw(Exporter DynaLoader); | |
15 | ||
16 | # Items to export into callers namespace by default | |
17 | @EXPORT = qw(); | |
18 | ||
19 | # Other items we are prepared to export if requested | |
20 | @EXPORT_OK = qw(); | |
21 | ||
22 | $VERSION = '2.03'; | |
23 | bootstrap Crypt::DES $VERSION; | |
24 | ||
25 | use strict; | |
26 | use Carp; | |
27 | ||
28 | sub usage | |
29 | { | |
30 | my ($package, $filename, $line, $subr) = caller(1); | |
31 | $Carp::CarpLevel = 2; | |
32 | croak "Usage: $subr(@_)"; | |
33 | } | |
34 | ||
35 | ||
36 | sub blocksize { 8; } | |
37 | sub keysize { 8; } | |
38 | ||
39 | sub new | |
40 | { | |
41 | usage("new DES key") unless @_ == 2; | |
42 | ||
43 | my $type = shift; | |
44 | my $self = {}; | |
45 | bless $self, $type; | |
46 | ||
47 | $self->{'ks'} = Crypt::DES::expand_key(shift); | |
48 | ||
49 | return $self; | |
50 | } | |
51 | ||
52 | sub encrypt | |
53 | { | |
54 | usage("encrypt data[8 bytes]") unless @_ == 2; | |
55 | ||
56 | my ($self,$data) = @_; | |
57 | return Crypt::DES::crypt($data, $data, $self->{'ks'}, 1); | |
58 | } | |
59 | ||
60 | sub decrypt | |
61 | { | |
62 | usage("decrypt data[8 bytes]") unless @_ == 2; | |
63 | ||
64 | my ($self,$data) = @_; | |
65 | return Crypt::DES::crypt($data, $data, $self->{'ks'}, 0); | |
66 | } | |
67 | ||
68 | 1; | |
69 | ||
70 | __END__ | |
71 | ||
72 | =head1 NAME | |
73 | ||
74 | Crypt::DES - Perl DES encryption module | |
75 | ||
76 | =head1 SYNOPSIS | |
77 | ||
78 | use Crypt::DES; | |
79 | ||
80 | ||
81 | =head1 DESCRIPTION | |
82 | ||
83 | The module implements the Crypt::CBC interface, | |
84 | which has the following methods | |
85 | ||
86 | =over 4 | |
87 | ||
88 | =item blocksize | |
89 | =item keysize | |
90 | =item encrypt | |
91 | =item decrypt | |
92 | ||
93 | =back | |
94 | ||
95 | =head1 FUNCTIONS | |
96 | ||
97 | =over 4 | |
98 | ||
99 | =item blocksize | |
100 | ||
101 | Returns the size (in bytes) of the block cipher. | |
102 | ||
103 | =item keysize | |
104 | ||
105 | Returns the size (in bytes) of the key. Optimal size is 8 bytes. | |
106 | ||
107 | =item new | |
108 | ||
109 | my $cipher = new Crypt::DES $key; | |
110 | ||
111 | This creates a new Crypt::DES BlockCipher object, using $key, | |
112 | where $key is a key of C<keysize()> bytes. | |
113 | ||
114 | =item encrypt | |
115 | ||
116 | my $cipher = new Crypt::DES $key; | |
117 | my $ciphertext = $cipher->encrypt($plaintext); | |
118 | ||
119 | This function encrypts $plaintext and returns the $ciphertext | |
120 | where $plaintext and $ciphertext should be of C<blocksize()> bytes. | |
121 | ||
122 | =item decrypt | |
123 | ||
124 | my $cipher = new Crypt::DES $key; | |
125 | my $plaintext = $cipher->decrypt($ciphertext); | |
126 | ||
127 | This function decrypts $ciphertext and returns the $plaintext | |
128 | where $plaintext and $ciphertext should be of C<blocksize()> bytes. | |
129 | ||
130 | =back | |
131 | ||
132 | =head1 EXAMPLE | |
133 | ||
134 | my $key = pack("H16", "0123456789ABCDEF"); | |
135 | my $cipher = new Crypt::DES $key; | |
136 | my $ciphertext = $cipher->encrypt("plaintex"); # NB - 8 bytes | |
137 | print unpack("H16", $ciphertext), "\n"; | |
138 | ||
139 | =head1 NOTES | |
140 | ||
141 | Do note that DES only uses 8 byte keys and only works on 8 byte data | |
142 | blocks. If you're intending to encrypt larger blocks or entire files, | |
143 | please use Crypt::CBC in conjunction with this module. See the | |
144 | Crypt::CBC documentation for proper syntax and use. | |
145 | ||
146 | Also note that the DES algorithm is, by today's standard, weak | |
147 | encryption. Crypt::Blowfish is highly recommended if you're | |
148 | interested in using strong encryption and a faster algorithm. | |
149 | ||
150 | =head1 SEE ALSO | |
151 | ||
152 | Crypt::Blowfish | |
153 | Crypt::IDEA | |
154 | ||
155 | Bruce Schneier, I<Applied Cryptography>, 1995, Second Edition, | |
156 | published by John Wiley & Sons, Inc. | |
157 | ||
158 | =head1 COPYRIGHT | |
159 | ||
160 | The implementation of the DES algorithm was developed by, | |
161 | and is copyright of, Eric Young (eay@mincom.oz.au). | |
162 | Other parts of the perl extension and module are | |
163 | copyright of Systemics Ltd ( http://www.systemics.com/ ). | |
164 | Cross-platform work and packaging for single algorithm | |
165 | distribution is copyright of W3Works, LLC. | |
166 | ||
167 | =head1 MAINTAINER | |
168 | ||
169 | This single-algorithm package and cross-platform code is | |
170 | maintained by Dave Paris <amused@pobox.com>. | |
171 | ||
172 | =cut |