Here's a simple snippet that uses password and generates different encryption string every time you reload.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <?php $string = "This is a string to be encrypted" ; $key = "This is a key" ; // Encryption Algorithm $alg = MCRYPT_TWO_FISH; // Create the initialization vector for added security $iv = mcrypt_create_iv(mcrypt_get_iv_size( $alg , MCRYPT_MODE_ECB), MCRYPT_RAND); // Output original string print "Original string: $string <p>" ; // Encrypt $string $encrypted_string = mcrypt_encrypt( $alg , $key , $string , MCRYPT_MODE_CBC, $iv ); // Convert to hexadecimal and output to browser print "Encrypted string: " .bin2hex( $encrypted_string ). "<p>" ; $decrypted_string = mcrypt_decrypt( $alg , $key , $encrypted_string , MCRYPT_MODE_CBC, $iv ); print "Decrypted string: $decrypted_string" ; ?> |