Forum Moderators: coopster

Message Too Old, No Replies

Crypting in PHP

         

blakmonk

9:45 pm on Oct 29, 2004 (gmt 0)

10+ Year Member



Hi everyone,


I'm looking for something that will do the following, but in PHP... or at least create crypted output readable by the code blelow or read crypted output created by the code below..

perl code is as follows :

use Crypt::CBC;
$key = "SuperFreak";
$action = shift;
$string = shift;

my $c = new Crypt::CBC($key,"IDEA");
if ($action == 1) { #- crypt
return $c->encrypt_hex($string);
} else { #- decrypt
return $c->decrypt_hex($string);
}

BlakMonk

mincklerstraat

10:08 am on Oct 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



on the manual page for
mdecrypt_generic
[php.net] there's a user-contributed comment with code for encrypting and decrypting stuff. It might inspire you.

blakmonk

12:44 pm on Nov 1, 2004 (gmt 0)

10+ Year Member



Hi,

Yes, it does inspire me, but what they are using is ecb, other examples use fcb... but nothing so far uses cbc...

All I have go right now is this (below), and still no go....

function Cryptage($plain_text,$action)
{
  $key = "SuperFreak";  

  $td = mcrypt_module_open('des', '', 'cbc', '');
  $key = substr($key, 0, mcrypt_enc_get_key_size($td));
  $iv_size = mcrypt_enc_get_iv_size($td);
  $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);

  if (mcrypt_generic_init($td, $key, $iv)!= -1) {
    if ($action == 1) {         #- encryption
      #return bin2hex(mcrypt_cbc($td, $data, MCRYPT_ENCRYPT, $iv));
      $c_t = bin2hex(mcrypt_generic($td, $plain_text));
    } else {                    #- decryption
      #return trim(mcrypt_decrypt($td, $key, pack("H*",$plain_text),  MCRYPT_ENCRYPT,$iv));
      $c_t = trim(mdecrypt_generic($td, $c_t));
    }

    mcrypt_generic_deinit($td);
    mcrypt_module_close($td);  
    return $c_t;
  }else{
    return "ERROR IN ENCRYPTION";
  }
   

Blakmonk

StupidScript

12:10 am on Nov 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It looks like the mcrypt_cbc function [php.net] has been deprecated. The info at the mcrypt_encrypt page [php.net] indicates that CBC is one of the options for the mode argument.

blakmonk

4:11 pm on Nov 5, 2004 (gmt 0)

10+ Year Member



Hi,

I know mcrypt_cbc is deprecated (notice it's commented, to show I've tried it)

The current code you see is taken from examples from that link....

StupidScript

11:49 pm on Nov 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Did you try mcrypt_encrypt using CBC as the mode argument?