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 );
PerlPHP

Avatar of undefined
Last Comment
Dennie

8/22/2022 - Mon
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

Utte

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

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
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
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).
Nem Schlecht

Argh - sorry!  This is actually a different question.  My bad - they look very much the same.
Dennie

ASKER
Yes, they're different :) I'm interested in both solutions
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Utte

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?
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.
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."
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Utte

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

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
Utte

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Dennie

ASKER
I needed to used a hashed version of the key
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.