php - AES - The encrypted string is not decrypting. -
i have 3 php file 1 index.php
original string there, encrypt.php
encrypt original string , lastly decrypt.php
decrypt problem when try decrypt result still encrypted not same encrypt it's different. can me decryption?
here picture click encrypt
here decrypted problem output should "fwf2" it's different
here code index.php
<!doctype html> <html> <head> <title></title> </head> <body> <form method="post" action="encrypt.php"> original string <input type="text" name="text"> <input type="submit" name="encrypt" value="encrypt" href="encrypt.php"> </form> </body> </html>
here encrypt.php
<?php $secret_key = "thisismykey12345"; $iv = mcrypt_create_iv(mcrypt_get_iv_size(mcrypt_rijndael_256, mcrypt_mode_ecb), mcrypt_rand); if(isset($_post['encrypt'])){ $string = $_post['text']; $encrypted_string = mcrypt_encrypt(mcrypt_rijndael_256, $secret_key, $string, mcrypt_mode_cbc, $iv); } ?> <!doctype html> <html> <head> <title></title> </head> <body> <form method="post" action="decrypt.php"> encrypted string <input type="text" style="width:500px;" name="encrypted" value="<?php echo $encrypted_string; ?>"> <input type="submit" name="decrypt" value="decrypt" href="decrypt.php"> </body> </html>
here decrypt.php
<?php $secret_key = "thisismykey12345"; $iv = mcrypt_create_iv(mcrypt_get_iv_size(mcrypt_rijndael_256, mcrypt_mode_ecb), mcrypt_rand); if(isset($_post['decrypt'])){ $encrypted_string = $_post['encrypted']; $decrypted_string = mcrypt_decrypt(mcrypt_rijndael_256, $secret_key, $encrypted_string, mcrypt_mode_cbc, $iv); } ?> <!doctype html> <html> <head> <title></title> </head> <body> <form method="post" action="encrypt.php"> decrypted string <input type="text" name="decrypted" style="width:500px;" value="<?php echo $decrypted_string ?>"> </body> </html>
you have reuse iv have things initialized same way encryption , decryption:
// encryption $iv_size = mcrypt_get_iv_size(mcrypt_rijndael_256, mcrypt_mode_ecb); $iv_enc = mcrypt_create_iv($iv_size, mcrypt_rand); $str_enc = mcrypt_encrypt(mcrypt_rijndael_256, 'thisismykey12345', 'hallops', mcrypt_mode_cbc, $iv_enc); $encrypted = $iv_enc . $str_enc; // decryption $iv_size = mcrypt_get_iv_size(mcrypt_rijndael_256, mcrypt_mode_ecb); $iv_dec = substr($encrypted, 0, $iv_size); // extract iv $str_dec = substr($encrypted, $iv_size); // extract encrypted string echo mcrypt_decrypt( mcrypt_rijndael_256, 'thisismykey12345', $str_dec, mcrypt_mode_cbc, $iv_dec ); --> hallops
notice way iv , encrypted data concatenated , "sent" together.
like others have said, there things may need done if sent somewhere, , encryption algorithms safer others.
edit: http://php.net/mcrypt_encrypt explains these things more detailed in examples.
Comments
Post a Comment