I'm trying to encrypt a string in one php page and pass it to another page using $_POST[] and decrypt it again.
The encryption works fine but when I POST it to another page to decrypt it, it doesn't get decrypted at all and I get another encrypted string again!
This is the code on page 1 where I encrypt:
<?php
/*
* PHP mcrypt - Basic encryption and decryption of a string
*/
$string = "[email protected]";
$secret_key = "This is my secret key";
// Create the initialization vector for added security.
$iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND);
// Encrypt $string
$encrypted_string = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $secret_key, $string, MCRYPT_MODE_CBC, $iv);
// Decrypt $string
$decrypted_string = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $secret_key, $encrypted_string, MCRYPT_MODE_CBC, $iv);
echo "Original string : " . $string . "<br />\n";
echo "Encrypted string : " . $encrypted_string . "<br />\n";
echo "Decrypted string : " . $decrypted_string . "<br />\n";
?>
<form action="2.php" method="post">
<input type="text" id="" name="input" value="<?php echo $encrypted_string; ?>"/>
<button type="submit" >submit</button>
</form>
And this is the code in page 2 where I'm trying to decrypt again:
<?php
$input = $_POST['input'];
$secret_key = "This is my secret key";
// Create the initialization vector for added security.
$iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND);
// Encrypt $string
$encrypted_string = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $secret_key, $input, MCRYPT_MODE_CBC, $iv);
// Decrypt $string
$decrypted_string = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $secret_key, $encrypted_string, MCRYPT_MODE_CBC, $iv);
echo $decrypted_string;
?>
Could someone please advise on this issue? is what i'm trying to do even possible?
BLOWFISH$variableso the users can't see it in the browser as it is in a hidden html input and then decrypt it again on the next page.