Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

DES ECB mode using DES_PP

by v4169sgr (Sexton)
on Aug 29, 2014 at 08:21 UTC ( [id://1098953]=perlquestion: print w/replies, xml ) Need Help??

v4169sgr has asked for the wisdom of the Perl Monks concerning the following question:

I use Perl 5.10 ActivePerl 5.14 on win7. I am a humble user, and don't really understand what I am doing.

I have a requirement to connect to a remote host and send locally encrypted messages, using DES ECB mode encryption. This is not my choice, but a part of the requirement. I am trying to find out how to implement DES ECB mode encryption simply in Perl.

I do not have access to a C compiler, and cannot be the local administrator on my machine. I don't have download rights either, but can copy / paste source from CPAN into .pm files in my C:. I have copied the source for DES_PP.pm and ECB.pm to C:\Perl 5.10\lib\Crypt\ from CPAN. I've chosen DES_PP.pm as my understanding is that I don't need a C compiler to use it.

I need to make sure that I am using the ECB hex encryption function correctly. My code currently looks like this:

use Crypt::ECB qw(encrypt decrypt encrypt_hex decrypt_hex); my $ascii_key = "0123456789abcdef"; my $plain_text = "Now is the time for all "; print encrypt_hex($ascii_key,"DES_PP",$plain_text,"0");

This 'works' in that it runs without errors, but produces output:

9a8836c3209081ed369a03f8b488a2ef3c85885f76d95430

From the example I am using in the original DES ECB mode specification (FIPS 81), I should see:

3fa40e8a984d48156a271787ab8883f9893d51ec4b563b53

What am I doing wrong?

Thanks in advance for all responses. Yes, I know there are much better ways of achieving encrypted traffic, and I know too that I should really do things the proper way via full local installation and C compilers etc, but I can't do any of that because of local policies, and still need to solve the problem.

Replies are listed 'Best First'.
Re: DES ECB mode using DES_PP
by Corion (Patriarch) on Aug 29, 2014 at 08:45 UTC

    The module expects the key to be in bytes, not in hexadecimal nibbles:

    use strict; use Crypt::ECB qw(encrypt decrypt encrypt_hex decrypt_hex); my $ascii_key = "0123456789abcdef"; my $key= "\x01\x23\x45\x67\x89\xab\xcd\xef"; my $plain_text = "Now is the time for all "; my $expected= '3fa40e8a984d48156a271787ab8883f9893d51ec4b563b53'; my $result= encrypt_hex($key,"DES_PP",$plain_text,0); if( $result eq $expected ) { print "Yay\n"; } else { print "Got : $result\n"; print "Expected: $expected\n"; };

      This works exactly as intended - thank you!

      Gosh, don't I feel stupid now :P

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1098953]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2024-04-26 00:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found