encrypt / decrypt MD5 structure (encrypted result only numbers and letters) with php

2

First of all, thanks in advance for reading this thread.

You see, I use the following functions to encrypt / decrypt some variables at the server level and create URLs. I already use this function: link They even helped me right here with some doubts I had (here the link): Encrypt and Decrypt ID obtained by GET parameter in php

The problem is the following: I'm removing content with ajax, and for that, each container in HTMl I put a unique identifier encrypted. The problem is that for example: if the ID of the record is 1 the structure that throws me the previous functions is something like this:

  

t + i + 6lJ95 / fVPYL9 + wPkaKc8RGheD / lbkx7chHT / zl4 =

That is, the enriched result has the characters "/", "+", "=". Then, trying to manipulate those identifiers from jquery throws me these errors.

Then it occurs to me, to encrypt my IDs MD5 style. That is, pure numbers and letters, like this:

  

c4ca4238a0b923820dcc509a6f75849b

However, I have known in advance that MD5 can not be decrypted, so I would like you to help me to orientate myself as (using the function that I currently have or creating a new one), I can encrypt my IDs but as a result only have numbers and letters, like this:

  

c4ca4238a0b923820dcc509a6f75849b

Thank you very much for your guidance. Greetings

PS: It is important to describe the result to obtain the identifier and be able to delete the database.

    
asked by Neftali Acosta 24.04.2018 в 07:05
source

1 answer

4

The functions mcrypt_encrypt and mcrypt_decrypt have been declared obsolete and their use is totally inadvisable (see documentation here and here ) instead it is recommended to use the

I give you a commented example to do what you are looking for, simply adapt it to your needs.

<?php

/**
 * Función para encriptar
 */
function my_encrypt($data, $key) {
    // Generamos una cadena de bytes pseudo-aleatoria en base al método de cifrado
    // en este caso: blowfish
    $iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('blowfish'));
    // Ciframos los datos usando blowfish
    $cifrado = openssl_encrypt($data, 'blowfish', $key, 0, $iv);
    // Añadimos el $iv y retornamos en base64
    // el $iv es necesario para poder decodificar los datos por eso lo unimos a 
    // los datos mediante un separador único (|||)
    return base64_encode($cifrado . '|||' . $iv);
}

/**
 * Función para desencriptar
 */
function my_decrypt($data, $key) {
    // Decodificamos los datos y dividimos por el separador único (|||)
    $dataIv = explode('|||', base64_decode($data), 2);
    // comprobamos que tenemos 2 valores
    if(count($dataIv) != 2) {
        return false;
    }
    // Asignamos los datos, para una mejor lectura del código
    $data = $dataIv[0];
    $iv =  $dataIv[1];
    // Validamos longitud correcta del IV
    if(strlen($iv) != openssl_cipher_iv_length('blowfish')) {
        return false;
    }
    // desciframos los datos y retornamos
    return openssl_decrypt($data, 'blowfish', $key, 0, $iv);
}

// Nuestra clave de cifrado
$key = 'Esto es un ejemplo de contraseña';
// Datos a cifrar
$texto = 'Lorem ipsum dolor sit amet. ';

echo "Clave de cifrado: <br>";
echo $key."<br><br>";

echo "Texto original: <br>";
echo $texto."<br><br>";

echo "Cifrado: <br>";
$texto_cifrado = my_encrypt($texto, $key);
echo $texto_cifrado . "<br><br>";

echo "Descifrado: <br>";
echo my_decrypt($texto_cifrado, $key);

If you need to know the types of encryption available in your system you can use the following code:

$cifrados         = openssl_get_cipher_methods();
$cifrados_y_alias = openssl_get_cipher_methods(true);
$alias_cifrados   = array_diff($cifrados_y_alias, $cifrados);

echo '<pre>';
print_r($cifrados);
print_r($alias_cifrados);
echo '</pre>';
    
answered by 24.04.2018 / 21:01
source