Trouble decrypting data

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • laredotornado@zipmail.com

    Trouble decrypting data

    Hi,

    I am using PHP 4.4.3 and trying to encrypt and decrypt data. Below are
    my functions. The problem is that when I run this code, different
    results are printed out ...

    require("util_f ns.php");
    $data = "wood";
    $encData = encryptData($da ta);
    $decData = decryptData($en cData);
    // This prints out "wood" and then "wood" with a bunch of junk
    after it.
    print "$data<BR>\n$de cData<BR>\n";

    Could someone help me correct what's wrong? Thanks, - Dave

    function encryptData($p_ str)
    {
    if (ENCRYPTION_ENA BLED) {
    $iv_size = mcrypt_get_iv_s ize(MCRYPT_XTEA ,
    MCRYPT_MODE_ECB );
    $iv = mcrypt_create_i v($iv_size, MCRYPT_RAND);

    $enc = mcrypt_encrypt( MCRYPT_XTEA,
    ENCRYPTION_KEY, $p_str, MCRYPT_MODE_ECB , $iv);
    return $enc;
    } else {
    return $p_str;
    } // if
    } // encryptData

    function decryptData($p_ str)
    {
    if (ENCRYPTION_ENA BLED) {
    $iv_size = mcrypt_get_iv_s ize(MCRYPT_XTEA ,
    MCRYPT_MODE_ECB );
    $iv = mcrypt_create_i v($iv_size, MCRYPT_RAND);
    $crypttext = mcrypt_decrypt( MCRYPT_XTEA,
    ENCRYPTION_KEY, $p_str, MCRYPT_MODE_ECB , $iv);
    return $crypttext;
    } else {
    return $p_str;
    } // if
    } // decryptData

  • Benjamin

    #2
    Re: Trouble decrypting data

    I would suggest you take a look at the mcypt functions found at

    you need to call mcrypt_module_o pen first to get a module then pass it
    as the first parameter for the rest of the functions. I would suggest
    you look at the documentation for all the params
    laredotornado@z ipmail.com wrote:
    Hi,
    >
    I am using PHP 4.4.3 and trying to encrypt and decrypt data. Below are
    my functions. The problem is that when I run this code, different
    results are printed out ...
    >
    require("util_f ns.php");
    $data = "wood";
    $encData = encryptData($da ta);
    $decData = decryptData($en cData);
    // This prints out "wood" and then "wood" with a bunch of junk
    after it.
    print "$data<BR>\n$de cData<BR>\n";
    >
    Could someone help me correct what's wrong? Thanks, - Dave
    >
    function encryptData($p_ str)
    {
    if (ENCRYPTION_ENA BLED) {
    $iv_size = mcrypt_get_iv_s ize(MCRYPT_XTEA ,
    MCRYPT_MODE_ECB );
    $iv = mcrypt_create_i v($iv_size, MCRYPT_RAND);
    >
    $enc = mcrypt_encrypt( MCRYPT_XTEA,
    ENCRYPTION_KEY, $p_str, MCRYPT_MODE_ECB , $iv);
    return $enc;
    } else {
    return $p_str;
    } // if
    } // encryptData
    >
    function decryptData($p_ str)
    {
    if (ENCRYPTION_ENA BLED) {
    $iv_size = mcrypt_get_iv_s ize(MCRYPT_XTEA ,
    MCRYPT_MODE_ECB );
    $iv = mcrypt_create_i v($iv_size, MCRYPT_RAND);
    $crypttext = mcrypt_decrypt( MCRYPT_XTEA,
    ENCRYPTION_KEY, $p_str, MCRYPT_MODE_ECB , $iv);
    return $crypttext;
    } else {
    return $p_str;
    } // if
    } // decryptData

    Comment

    • veg_all@yahoo.com

      #3
      Re: Trouble decrypting data

      laredotornado@z ipmail.com wrote:
      Hi,
      >
      I am using PHP 4.4.3 and trying to encrypt and decrypt data. Below are
      my functions. The problem is that when I run this code, different
      the problem is the initialization vector is different each time . you
      would need to use the same $iv to get the same result. i can paste
      some code using rijndael-128 if interested.

      Comment

      • laredotornado@zipmail.com

        #4
        Re: Trouble decrypting data

        Please do.

        Thanks, - Dave

        veg_all@yahoo.c om ha escrito:
        laredotornado@z ipmail.com wrote:
        Hi,

        I am using PHP 4.4.3 and trying to encrypt and decrypt data. Below are
        my functions. The problem is that when I run this code, different
        >
        the problem is the initialization vector is different each time . you
        would need to use the same $iv to get the same result. i can paste
        some code using rijndael-128 if interested.

        Comment

        • veg_all@yahoo.com

          #5
          Re: Trouble decrypting data

          here is some untested code i pasted below: basically the iv,
          initailization vector is prepended to the encrypted data.

          ////////////////////////////////

          function aes_encrypt ($input) {

          global $key;

          $td = mcrypt_module_o pen('rijndael-128', '', 'cbc', '');

          // iv = initiailization _vector;

          $iv = mcrypt_create_i v (mcrypt_enc_get _iv_size($td), MCRYPT_RAND );

          mcrypt_generic_ init($td, $key, $iv);

          $encrypted_data = mcrypt_generic( $td, $input);

          mcrypt_generic_ deinit($td);
          mcrypt_module_c lose($td);

          $encrypted_data = bin2hex ( $iv ) . bin2hex ( $encrypted_data );

          return $encrypted_data ;

          } // end function

          //////////////////////////////

          function aes_decrypt ( $input ) {

          global $key;

          $iv = substr ( $input, 0, 32 );
          $iv = pack ( H32, $iv );

          $input = substr ( $input, 32, strlen ( $input ) );
          $input =pack("H" . strlen ( $input ) , $input );

          $td = mcrypt_module_o pen('rijndael-128', '', 'cbc', '');

          mcrypt_generic_ init($td, $key, $iv);

          $dec = mdecrypt_generi c($td, $input );

          mcrypt_generic_ deinit($td);
          mcrypt_module_c lose($td);

          return trim ( $dec );

          } // end function

          //////////////////////////////

          Comment

          Working...