9
The functions of the type mcrypt_*
were discontinued in PHP 7.1.0 and removed in PHP 7.2.0 as shown in manual:
Warning This Function was DEPRECATED in PHP 7.1.0, and REMOVED in PHP 7.2.0.
I was looking at this question Migrate PHP function from mcrypt to Openssl but there is only one alternative for migration. What other alternatives are there?
To give a better context I have some very old functions to encrypt strings, such as some strings that go through GET with information that I don’t want the client to see.
That is, instead of displaying the id (or any other string)
meusite.pt/user/Edit/&id=354
Displays an encryption so the client does not invent:
meusite.en/user/Edit/&id=5x6ypIpvj6Wd9TpekI2s4DetXUgBTSpZ_cyEAJxxBVU
The functions I currently use are these:
public function encode( $text )
{
$myKey = "stackoverflowemportugues";
if( !$text )
return false;
$iv_size = mcrypt_get_iv_size( MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB );
$iv = mcrypt_create_iv( $iv_size, MCRYPT_RAND );
$crypttext = mcrypt_encrypt( MCRYPT_RIJNDAEL_256, $myKey,
$text, MCRYPT_MODE_ECB, $iv );
return trim( base64_encode( $crypttext ) );
}
public function decode( $text )
{
$myKey = "stackoverflowemportugues";
if( !$text )
return false;
$crypttext = base64_decode( $text );
$iv_size = mcrypt_get_iv_size( MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB );
$iv = mcrypt_create_iv( $iv_size, MCRYPT_RAND );
$decrypttext = mcrypt_decrypt( MCRYPT_RIJNDAEL_256, $myKey,
$crypttext, MCRYPT_MODE_ECB, $iv );
return trim( $decrypttext );
}
PS: By the way, if you want to improve the answer, you can migrate the functions to be more complete.
I believe that it is better than direct conversion to analyze what is best for each specific use.
– Bacco
A string based on 64 should not contain a text, right? So why
trim
?– Costamilam
@Costamilam yes, but that’s not very important to the question.
– Jorge B.
You can use: password_hash.
– Ivan Ferrer
@Ivanferrer I didn’t mention the functions of type
password_*
because it makes no sense in this case, I already use them when I move with passwords.– Jorge B.