Encrypt and Decrypt function with different outputs

Encrypt function return different value with each execute but decrypt function return input value. for example: 

  • first execute: encrypt('hello')       => 'axxdfg34', decrypt('axxdfg34') return 'hello'
  • second execute: encrypt('hello') => 'xvcfghty214s', decrypt('xvcfghty214s') return 'hello'
function get_rnd_iv( $iv_len )
{
   $iv = '';
   while ( $iv_len-- > 0 )
      $iv .= chr( mt_rand() & 0xFF );
   return $iv;
}
 
function encrypt( $plain_text, $password = 'ASXDCFVGED$%kdknddbdFGt123', $iv_len = 5 )
{
   $plain_text .= "";
   $n = strlen( $plain_text );
   if ( $n % 16 )
      $plain_text .= str_repeat( "", 16 - ( $n % 16 ) );
   $i = 0;
   $enc_text = get_rnd_iv( $iv_len );
   $iv = substr( $password ^ $enc_text, 0, 512 );
   while ( $i < $n ) {
      $block = substr( $plain_text, $i, 16 ) ^ pack( 'H*', md5( $iv ) );
      $enc_text .= $block;
      $iv = substr( $block . $iv, 0, 512 ) ^ $password;
      $i += 16;
   }
   $enc_text = base64_encode( $enc_text );
   return $enc_text;
}
 
function decrypt( $enc_text, $password = 'ASXDCFVGED$%kdknddbdFGt123', $iv_len = 5 )
{
   $enc_text = base64_decode( $enc_text );
   $n = strlen( $enc_text );
   $i = $iv_len;
   $plain_text = '';
   $iv = substr( $password ^ substr( $enc_text, 0, $iv_len ), 0, 512 );
   while ( $i < $n ) {
      $block = substr( $enc_text, $i, 16 );
      $plain_text .= $block ^ pack( 'H*', md5( $iv ) );
      $iv = substr( $block . $iv, 0, 512 ) ^ $password;
      $i += 16;
   }
   return preg_replace( '/\x13\x00*$/', '', $plain_text );
}
 
echo $result = encrypt( 'Test' );
echo decrypt( $result );

Tags

No tag here.

Recommended pages

Resize image function...

Resize image function for jpg format. $mood between max and min. if you choose max, this mean max size of image is $ta otherwise min size of image is $ta. function makeTheThumb( $ppath, $ta, $mood ) ...

Human readable file size...

This function return formatting of file sizes in a human readable format. function formatFileSize( $size ) { $file_size = $size; $i = 0;   $name = array( 'byte', 'kB', 'MB', '...

URL Checker...

This function check if an url is valid or not. function is_valid_url($url) { return preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $url); }...

SQL escape string function...

Escapes special characters in a string for use in an SQL statement. howewer it check get_magic_quotes_gpc function is enable or no. if true , it strips string from slashes and escaped string from spec...