Ver Mensaje Individual
  #7 (permalink)  
Antiguo 13/03/2020, 11:01
minombreesmm
 
Fecha de Ingreso: agosto-2012
Ubicación: M.
Mensajes: 2.031
Antigüedad: 11 años, 7 meses
Puntos: 52
Respuesta: Error al desencriptar php con MCRYPT

Cita:
Iniciado por tuadmin Ver Mensaje
mira ahi estas indicando que tu variable $plain_text es de la Anterior en ese Anterior, la variable $iv es distinta a la actual $iv, en cada Ejecucion del SCRIPT, tu $iv sera distinta, aleatoria, random MCRYPT_DEV_URANDOM

asi que lo correcto y como todo mundo lo hace, Almacena ese $IV junto a tu texto Encriptado

como en esta modificacion a tu script

Código PHP:
Ver original
  1. $algorithm = MCRYPT_BLOWFISH;
  2. $key = 'abrete';
  3. $data = 'me gusta el pollo asado';
  4. $mode = MCRYPT_MODE_CBC;
  5.  
  6. $iv = mcrypt_create_iv(mcrypt_get_iv_size($algorithm, $mode),
  7.                        MCRYPT_DEV_URANDOM);
  8.  
  9. $encrypted_data = mcrypt_encrypt($algorithm, $key, $data, $mode, $iv);
  10. $plain_text = base64_encode($encrypted_data).':'. base64_encode($iv) ;
  11. echo $plain_text . "\n";
  12.  
  13. $partes = explode(":",$plain_text);
  14. $encrypted_data = base64_decode($partes[0]);
  15. $iv = base64_decode($partes[1]);
  16. $decoded = mcrypt_decrypt($algorithm, $key, $encrypted_data, $mode, $iv);
  17. echo $decoded . "\n";
Hola gracias,
nadamas que me marco el siguiente error

Warning: Use of undefined constant MCRYPT_BLOWFISH - assumed 'MCRYPT_BLOWFISH' (this will throw an Error in a future version of PHP) in C:\wamp64\www\cookies\encode.php on line 197

Warning: Use of undefined constant MCRYPT_MODE_CBC - assumed 'MCRYPT_MODE_CBC' (this will throw an Error in a future version of PHP) in C:\wamp64\www\cookies\encode.php on line 200

Fatal error: Uncaught Error: Call to undefined function mcrypt_create_iv() in C:\wamp64\www\cookies\encode.php:202 Stack trace: #0 {main} thrown in C:\wamp64\www\cookies\encode.php on line 202


A su vez, ya habia creado otro que me funcionó
Código PHP:
Ver original
  1. $token = '<form action="ejemplo.php" method="get">
  2.  <p>Nombre: <input type="text" name="nombre" size="40"></p>
  3.  <p>Año de nacimiento: <input type="number" name="nacido" min="1900"></p>
  4.  <p>Sexo:
  5.    <input type="radio" name="hm" value="h"> Hombre
  6.    <input type="radio" name="hm" value="m"> Mujer
  7.  </p>
  8.  <p>
  9.    <input type="submit" value="Enviar">
  10.    <input type="reset" value="Borrar">
  11.  </p>
  12. </form>';
  13.  
  14. $cipher_method = 'aes-128-ctr';
  15. $enc_key = openssl_digest("clave maestra", 'SHA256', TRUE);
  16. $enc_iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length($cipher_method));
  17. $crypted_token = openssl_encrypt($token, $cipher_method, $enc_key, 0, $enc_iv) . "::" . bin2hex($enc_iv);
  18. unset($token, $cipher_method, $enc_key, $enc_iv);
  19.  
  20.  
  21.  
  22. echo $crypted_token;
  23.  
  24.  
  25.  
  26. list($crypted_token, $enc_iv) = explode("::", $crypted_token);;
  27. $cipher_method = 'aes-128-ctr';
  28. $enc_key = openssl_digest("clave maestra", 'SHA256', TRUE);
  29. $token = openssl_decrypt($crypted_token, $cipher_method, $enc_key, 0, hex2bin($enc_iv));
  30. unset($crypted_token, $cipher_method, $enc_key, $enc_iv);
  31.  
  32. echo "<br>".$token;
__________________
a veces creemos que es algo básico pero al profundizar nos damos cuenta que estábamos tocando solo la cola de la culebra