Link to home
Start Free TrialLog in
Avatar of Dennie
Dennie

asked on

DES encrypt function from perl to php

Hi experts,

How can I convert this to PHP?

  use Crypt::CBC;
  my $cipher = Crypt::CBC->new(
   {      'key'             => '123456',
      'cipher'          => 'DES',
      'iv'              => '$KJh#(}q',
      'regenerate_key'  => 1,
      'padding'         => 'space',
      'prepend_iv'      => 0
   } );

   $crypt = $cipher->encrypt_hex( $plain );
Avatar of Utte
Utte

I think you are looking for something like this.

For more info:
http://www.php.net/manual/en/book.mcrypt.php
<?php
$key = "132465";
$iv = '$KJh#(}q';


function Encrypt($text)
{
    $crypttext = mcrypt_encrypt(MCRYPT_DES, $key, $text, MCRYPT_MODE_ECB,$iv);
}

function Decrypt($cryptedtext)
{
    $decrypttext = mcrypt_decrypt(MCRYPT_DES,$key,$cryptedtext,MCRYPT_MODE_ECB,$iv);
}
?>

Open in new window

Sorry forgot return statements in the code.
<?php
$key = "132465";
$iv = '$KJh#(}q';


function Encrypt($text)
{
    return $crypttext = mcrypt_encrypt(MCRYPT_DES, $key, $text, MCRYPT_MODE_ECB,$iv);
}

function Decrypt($cryptedtext)
{
    return $decrypttext = mcrypt_decrypt(MCRYPT_DES,$key,$cryptedtext,MCRYPT_MODE_ECB,$iv);
}
?>

Open in new window

Avatar of Dennie

ASKER

Yeah I also found that solution. The problem is that my key is actually a bit longer (so not 123456) and I get the message:

Warning: mcrypt_encrypt() [function.mcrypt-encrypt]: Size of key is too large for this algorithm in
Avatar of Nem Schlecht
I just noticed you posted this question twice.  You should request one or the other be deleted (probably the other one that I posted a comment on).
Argh - sorry!  This is actually a different question.  My bad - they look very much the same.
Avatar of Dennie

ASKER

Yes, they're different :) I'm interested in both solutions
Hello. Is there a reason that you use DES. Because DES is a very weak encryption algorithm. Could you try to encrypt something using a key in Perl so i can try to decrypt it in php?
Avatar of Dennie

ASKER

The reason is that the DB is old, and the values in it are created by the provided PERL function. I'm not able to post any plaintext en encrypted passwords, because the actual key in use is different. Besides this, I'm not running Perl to create some with a different key. I hope you can still help me.
Avatar of Dennie

ASKER

Sorry for the bad English in the sentence above:

"Besides this, I'm not running Perl. So I'm not able to create plain and encrypted combinations with a different key."
Hello again.
I have been playing around with mcrypt and have now created this code.

Please chech if it works for you :)

Cheers Utte
<?php
 class Crypt
{
    static function Encrypt($cipher, $key, $data, $mode, $iv)
    {
        return mcrypt_encrypt($cipher, $key, $data, $mode, $iv);
    }

    static function Encrypt_Hex($cipher, $key, $data, $mode, $iv)
    {

        return bin2hex(Crypt::Encrypt($cipher, $key, $data, $mode, $iv));
    }

    static function Decrypt($cipher, $key, $data, $mode, $iv)
    {
        return mcrypt_decrypt($cipher, $key, $data, $mode, $iv);
    }

    static function Decrypt_Hex($cipher, $key, $hex, $mode, $iv)
    {
        $data='';
        for ($i=0; $i < strlen($hex)-1; $i+=2){
            $data .= chr(hexdec($hex[$i].$hex[$i+1]));
        }
        return Crypt::decrypt($cipher, $key, $data, $mode, $iv);
    }
}
$key = "123456";
$iv = '$KJh#(}q';
$data = "data to encrypt"; // The text to encrypt
$cipher = MCRYPT_DES; // The encryption algorighm
$mode = MCRYPT_MODE_CBC; // CBC mode is for large amounts of data

$encrypted = Crypt::Encrypt_Hex($cipher, $key, $data, $mode, $iv); // will in this case return c073fa74b16201687cce2ce96a195ebc
$plain = Crypt::Decrypt_Hex($cipher, $key, $encrypted, $mode, $iv); // should return the $data
?>

Open in new window

Avatar of Dennie

ASKER

Thanks for the help Utte. I get "Warning: mcrypt_encrypt() [function.mcrypt-encrypt]: Size of key is too large for this algorithm"
ASKER CERTIFIED SOLUTION
Avatar of Utte
Utte

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Dennie

ASKER

I needed to used a hashed version of the key