Page 1 of 1

how to encrypt and decrypt the data in php

Posted: Tue Jun 23, 2009 1:37 am
by kushaljutta
Hi all....

can anyone help me in encryption and decryption...

my code is

Code: Select all

 
 <?php
      function cryptastic( $data, $key, $encrypt=true ) {
       
          # Serialize, if encrypting
          if ( $encrypt ) $data = serialize($data);
          # Open cipher module
          if ( ! $td = mcrypt_module_open('rijndael-256', '', 'cfb', '') )
              return false;
          $ks = mcrypt_enc_get_key_size($td);     # Required key size
          $key = substr(sha1($key), 0, $ks);      # Harden / adjust length
          $ivs = mcrypt_enc_get_iv_size($td);     # IV size
          $iv = $encrypt ?
              mcrypt_create_iv($ivs, MCRYPT_RAND) :   # Create IV, if encrypting
              substr($data, 0, $ivs);                 # Extract IV, if decrypting
          # Extract data, if decrypting
          if ( ! $encrypt ) $data = substr($data, $ivs);
          if ( mcrypt_generic_init($td, $key, $iv) !== 0 ) # Initialize buffers
              return false;
          $data = $encrypt ?
              mcrypt_generic($td, $data) :    # Perform encryption
              mdecrypt_generic($td, $data);   # Perform decryption
          if ( $encrypt ) $data = $iv . $data;    # Prepend IV, if encrypting
          mcrypt_generic_deinit($td);             # Clear buffers
          mcrypt_module_close($td);               # Close cipher module
          # Unserialize, if decrypting
          if ( ! $encrypt ) $data= unserialize($data);
          return $data;
      }
?>
 

Code: Select all

 
<?php
      $privateKey = "This is the private key.";
      $secretData = "This is the secret message.";
      # Encrypt the secret data
      $encryptedData = cryptastic($secretData, $privateKey);
      
      echo "Encrypted data:".$encryptedData;
       
      # Decrypt the ciphertext
      $decryptedData = cryptastic($encryptedData, $privateKey, false);
 
?>
 
here it gives and error

call to undefined function mcrypt_module_open().....

can anyone help me in this..

i configured all the files ie .dll files which are required for enc and dec

Re: how to encrypt and decrypt the data in php

Posted: Tue Jun 23, 2009 3:31 am
by requinix
kushaljutta wrote:i configured all the files ie .dll files which are required for enc and dec
No, you didn't.

Try again.