Crypt-DES
view release on metacpan or search on metacpan
2.04 2005-12-08
- Renamed des.h to _des.h to avoid clashing with the system DES
header on Solaris.
- Added META.yml.
2.03 2000-12-05
- Restored "require DynaLoader", missing since 2.00 although
DynaLoader was still listed in @ISA.
- Dropped the stray "package DES;" so blocksize/keysize/new/
encrypt/decrypt are defined in Crypt::DES itself.
- bootstrap now version-checks against $VERSION.
- Declared globals with "use vars".
- Documentation: describe the interface as Crypt::CBC's rather
than Crypt::BlockCipher's.
(2.02 was never released to CPAN.)
2.01 2000-09-11
- Removed a stray #include <stdio.h> from _des.c.
- Comment and dead-code cleanup in DES.pm and test.pl.
#
# Copyright (C) 1995, 1996 Systemics Ltd (http://www.systemics.com/)
# All rights reserved.
#
# Modifications are Copyright (c) 2000, W3Works, LLC
# All Rights Reserved.
package Crypt::DES;
require Exporter;
require DynaLoader;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
@ISA = qw(Exporter DynaLoader);
# Items to export into callers namespace by default
@EXPORT = qw();
# Other items we are prepared to export if requested
@EXPORT_OK = qw();
$VERSION = '2.09';
bootstrap Crypt::DES $VERSION;
use strict;
use Carp;
sub usage
{
my ($package, $filename, $line, $subr) = caller(1);
$Carp::CarpLevel = 2;
croak "Usage: $subr(@_)";
}
sub keysize { 8; }
sub new
{
usage("new DES key") unless @_ == 2;
my $type = shift;
my $self = {};
bless $self, $type;
$self->{'ks'} = Crypt::DES::expand_key(shift);
return $self;
}
sub encrypt
{
usage("encrypt data[8 bytes]") unless @_ == 2;
my ($self,$data) = @_;
return Crypt::DES::crypt($data, $data, $self->{'ks'}, 1);
}
sub decrypt
{
usage("decrypt data[8 bytes]") unless @_ == 2;
my ($self,$data) = @_;
return Crypt::DES::crypt($data, $data, $self->{'ks'}, 0);
}
1;
__END__
=head1 NAME
Crypt::DES - Perl DES encryption module
=head1 SYNOPSIS
use Crypt::DES;
my $key = pack("H16", "0123456789ABCDEF");
my $cipher = Crypt::DES->new($key);
my $ciphertext = $cipher->encrypt($plaintext);
my $plaintext = $cipher->decrypt($ciphertext);
=head1 DESCRIPTION
The module implements the Crypt::CBC interface,
which has the following methods
=over 4
=item blocksize
Returns the size (in bytes) of the block cipher.
=item keysize
Returns the size (in bytes) of the key. Optimal size is 8 bytes.
=item new
my $cipher = Crypt::DES->new($key);
This creates a new Crypt::DES BlockCipher object, using $key,
where $key is a key of C<keysize()> bytes.
=item encrypt
my $cipher = Crypt::DES->new($key);
my $ciphertext = $cipher->encrypt($plaintext);
This function encrypts $plaintext and returns the $ciphertext
where $plaintext and $ciphertext should be of C<blocksize()> bytes.
=item decrypt
my $cipher = Crypt::DES->new($key);
my $plaintext = $cipher->decrypt($ciphertext);
This function decrypts $ciphertext and returns the $plaintext
where $plaintext and $ciphertext should be of C<blocksize()> bytes.
=back
=head1 Security Considerations
This module is not deprecated as there may still be a reason to
encrypt and decrypt using DES. However, DES is known to be weak.
In 2026 a standard laptop can brute-force a DES key in a few
days. B<DO NOT use DES (or this module) with any expectation of
security>.
=head1 EXAMPLE
my $key = pack("H16", "0123456789ABCDEF");
my $cipher = Crypt::DES->new($key);
my $ciphertext = $cipher->encrypt("plaintex"); # NB - 8 bytes
print unpack("H16", $ciphertext), "\n";
my $plaintext = $cipher->decrypt($ciphertext);
print $plaintext, "\n";
=head1 NOTES
Do note that DES only uses 8 byte keys and only works on 8 byte data
blocks. If you're intending to encrypt larger blocks or entire files,
please use Crypt::CBC in conjunction with this module. See the
Crypt::CBC documentation for proper syntax and use.
Also note that the DES algorithm is, by today's standard, weak
encryption. CryptX (Crypt::Cipher::AES) is recommended if you
want to replace Crypt::DES in a Crypt::CBC use case. Otherwise,
replacing it with something like Crypt::AuthEnc::GCM would be
recommended.
=head1 SEE ALSO
Crypt::AuthEnc::GCM
Crypt::Blowfish
Crypt::IDEA
Bruce Schneier, I<Applied Cryptography>, 1995, Second Edition,
typedef unsigned char i8;
typedef unsigned long i32;
#include "_des.h"
#ifndef sv_undef
#define sv_undef PL_sv_undef
#endif
MODULE = Crypt::DES PACKAGE = Crypt::DES PREFIX = _des_
PROTOTYPES: DISABLE
char *
_des_expand_key(key)
char * key = NO_INIT
STRLEN key_len = NO_INIT
CODE:
{
des_ks ks;
Makefile.PL view on Meta::CPAN
#! /usr/local/bin/perl
use ExtUtils::MakeMaker;
# See lib/ExtUtils/MakeMaker.pm for details of how to influence
# the contents of the Makefile being created.
require 5.004;
WriteMakefile(
'NAME' => 'Crypt::DES',
'DISTNAME' => 'Crypt-DES',
'VERSION_FROM' => 'DES.pm',,
'OBJECT' => 'DES.o _des.o',
'dist' => {COMPRESS=>'gzip', SUFFIX=>'gz'},
META_MERGE => {
'meta-spec' => { version => 2 },
'resources' => {
'repository' => {
'type' => 'git',
'url' => 'git://github.com/timlegge/perl-Crypt-DES.git',
Crypt::DES - an XS-based DES implimentation for Perl.
The 2.XX tree represents a major improvement over the
1.XX tree. This package builds on big-endian
machines and many more x86 platforms than before.
(with a few rare exceptions, like gcc on DUX against
5.004).
mod_ssl conflicts have also been resolved. Thank you
to Jan 'Kozo' Vajda for pointing out the des_SPtrans
overlap between these two packages.
See Changes introduced in 2.08 for release information.
Prerequisites
-------------
For the full test suite to run, Crypt::CBC, version 1.22 or higher
is required (recommended is 1.25 or higher), however this module
is not mandatory for standalone DES use, and all other tests
will run to completion.
Installing Crypt::DES
---------------------
nothing unusual:
1. perl Makefile.PL
2. make
3. make test
4. make install
Notes
#
# Extended testing is Copyright (C) 2000 W3Works, LLC
# All rights reserved.
#
#
# Copyright (C) 1995, 1996 Systemics Ltd (http://www.systemics.com/)
# All rights reserved.
#
package Crypt::DES;
require Exporter;
require DynaLoader;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
@ISA = (Exporter, DynaLoader);
# Items to export into callers namespace by default
@EXPORT = qw();
# Other items we are prepared to export if requested
@EXPORT_OK = qw();
$VERSION = '2.09';
bootstrap Crypt::DES;
use strict;
use Carp;
sub usage
{
my ($package, $filename, $line, $subr) = caller(1);
$Carp::CarpLevel = 2;
croak "Usage: $subr(@_)";
}
sub keysize { 8; }
sub new
{
usage("new DES key") unless @_ == 2;
my($type,$key) = @_;
my $self = {};
bless $self, $type;
$self->{'ks'} = Crypt::DES::expand_key($key);
return $self;
}
sub encrypt
{
usage("encrypt data[8 bytes]") unless @_ == 2;
my ($self,$data) = @_;
return Crypt::DES::crypt($data, $data, $self->{'ks'}, 1);
}
sub decrypt
{
usage("decrypt data[8 bytes]") unless @_ == 2;
my ($self,$data) = @_;
return Crypt::DES::crypt($data, $data, $self->{'ks'}, 0);
}
package main;
use Data::Dumper;
use Benchmark;
my $i = 1;
my $fail = 0;
my $tt = (scalar(@{$testval}) *2);
print "1..$tt\n";
my $t0 = Benchmark->new();
foreach my $tst (@{$testval}) {
my ($anot,$bnot) = (0,0);
foreach(@{$tst}) { $_ = pack("H*",$_) }
my $cipher = Crypt::DES->new($tst->[0]);
$anot = 1 unless ($cipher->encrypt($tst->[1]) eq $tst->[2]);
if($anot) {
#print "not ";
$fail++;
}
$i++;
$bnot = 1 unless ($cipher->decrypt($tst->[2]) eq $tst->[1]);
if($bnot) {
#print "not ";
my $ts0 = timestr($td0);
print "$tt basic tests ran in $ts0\n";
print "$suc of $tt tests passed ($fp\%)\n";
if($fail > 0) {
print "Not all tests successful. Please attempt to rebuild the package\n";
} else {
print "\nRunning speed tests...\n";
print "\nnon-cached cipher speed test. 5000 encrypt iterations\n";
my $t2 = Benchmark->new();
for(1..5000) {
my $cipher = Crypt::DES->new(pack("H*",'1c587f1c13924fef'));
$cipher->encrypt(pack("H*",'305532286d6f295a'));
}
my $t3 = Benchmark->new();
my $td1 = timediff($t3,$t2);
my $ts1 = timestr($td1);
print "$ts1\nok 343\n";
print "\nnon-cached cipher speed test. 5000 decrypt iterations\n";
my $t4 = Benchmark->new();
for(1..5000) {
my $cipher = Crypt::DES->new(pack("H*",'1c587f1c13924fef'));
$cipher->decrypt(pack("H*",'63fac0d034d9f793'));
}
my $t5 = Benchmark->new();
my $td2 = timediff($t5,$t4);
my $ts2 = timestr($td2);
print "$ts2\nok 344\n";
print "\ncached cipher speed test. 10000 encrypt iterations\n";
{
my $t6 = Benchmark->new();
my $cipher = Crypt::DES->new(pack("H*",'1c587f1c13924fef'));
for(1..10000) {
$cipher->encrypt(pack("H*",'305532286d6f295a'));
}
my $t7 = Benchmark->new();
my $td3 = timediff($t7,$t6);
my $ts3 = timestr($td3);
print "$ts3\nok 345\n";
}
print "\ncached cipher speed test. 10000 decrypt iterations\n";
{
my $t8 = Benchmark->new();
my $cipher = Crypt::DES->new(pack("H*",'1c587f1c13924fef'));
for(1..10000) {
$cipher->decrypt(pack("H*",'63fac0d034d9f793'));
}
my $t9 = Benchmark->new();
my $td4 = timediff($t9,$t8);
my $ts4 = timestr($td4);
print "$ts4\nok 346\n";
}
}
( run in 1.580 second using v1.01-cache-2.11-cpan-b16cb0d3907 )