title: encrypt string through url and retrieve on next page?
body: <p>I am using the following php script to encrypt a string when i pass it through a url.</p>
  
  <p>The majority of my code is stored in my encryption.php file.</p>
  
  <p>encryption.php:</p>
  
  <pre><code>&lt;?php class encryption{
      private $config;
  
      public function __construct( $options=array() ){
          $this-&gt;config=array_merge(
              array(
                  'cipher'    =&gt;  MCRYPT_RIJNDAEL_256,
                  'mode'      =&gt;  MCRYPT_MODE_ECB,
                  'key'       =&gt;  FALSE,
                  'iv'        =&gt;  FALSE,
                  'size'      =&gt;  FALSE,
                  'base64'    =&gt;  TRUE,
                  'salt'      =&gt;  FALSE
              ),
              $options
          );
      }
      private function getivs( $config=object ){
          $config-&gt;size=mcrypt_get_iv_size( $config-&gt;cipher, $config-&gt;mode );
          $config-&gt;iv=mcrypt_create_iv( $config-&gt;size, MCRYPT_RAND );
      }
      public function encrypt( $data=NULL ){
          $config=(object)$this-&gt;config;
          $this-&gt;getivs( $config );
          $data=trim( $data );
          $module = mcrypt_module_open( $config-&gt;cipher, '', $config-&gt;mode, '' );
          mcrypt_generic_init( $module, $config-&gt;key, $config-&gt;iv );
  
          $output = $config-&gt;base64 ? base64_encode( mcrypt_generic( $module, $data ) ) : mcrypt_generic( $module, $data );
  
          mcrypt_generic_deinit( $module );
          mcrypt_module_close( $module );
          return $output;
      }
      public function decrypt( $data=NULL ){
          $config=(object)$this-&gt;config;
          $this-&gt;getivs( $config );
          mb_detect_order( 'auto' );
          $encoding=mb_detect_encoding( $data );
          if( !$data or is_null( $data ) or empty( $data ) or !$encoding or $data=='' or base64_decode( $data )=='' ) return FALSE;
  
          $module = mcrypt_module_open( $config-&gt;cipher, '', $config-&gt;mode, '' );
          mcrypt_generic_init( $module, $config-&gt;key, $config-&gt;iv );
  
          $output = $config-&gt;base64 ? rtrim( mdecrypt_generic( $module, base64_decode( $data ) ),"\0" ) : rtrim( mdecrypt_generic( $module, $data ),"\0" );
  
          mcrypt_generic_deinit( $module );
          mcrypt_module_close( $module );
          return urldecode( $output );
      }
  }//end class
  
  ?&gt;
  </code></pre>
  
  <p>I then call my function and pass my string as an encrypted string through the url in my track_request.php file:</p>
  
  <pre><code>require_once 'dependables/encryption.php';
          $string = 'NS' . substr( md5(rand()), 0, 7);
          $enc=new encryption( array( 'key'=&gt;'PlowFish' ) );
          $encrypted_string = $enc-&gt;encrypt( $string );
  
  
          echo '&lt;a href="ns_application.php?ns_request='.$encrypted_string.'"&gt;Click&lt;/a&gt;
  </code></pre>
  
  <p>Lastly i then retrieve the encrypted string and decrypt it again on my ns_application.php page:</p>
  
  <pre><code>require_once 'dependables/encryption.php';
  $reference = isset($_GET['ns_request']) ? $_GET['ns_request'] : null;
  $enc=new encryption( array( 'key'=&gt;'PlowFish' ) );
  
  $decrypted_string=$enc-&gt;decrypt( $reference ); ?&gt;
  
  
  &lt;?php echo $decrypted_string; ?&gt;
  </code></pre>
  
  <p>The problem is this script works 5 times out of 10, and i will end up with a decrypted string with something that looks like 'NS1H57347' or 'NS197G347' and on and on etc.</p>
  
  <p>However the other 5 times it will fail and it won't decrypt at all so i end up with a really long encrypted string like: 'FJQRU248T\£$90=}\31232HGfd*,^='</p>
  
  <p>Please can someone show me where i am going wrong? thanks</p>