首页 > 解决方案 > url中的加密/解密字符串随机停止工作

问题描述

我具有加密和解密从电子邮件的 url 中传递的字符串的功能。此功能已经运行良好一年,最近停止工作。几周内没有部署到生产服务器。没有代码更改......我能想到的服务器没有任何更改。我很难理解为什么这会停止工作。

当我向用户发送估计值时,我对字符串进行了加密

$token = $contact->email."|".$user->id."|".$estimate->magiclink;

$cipher_method = 'aes-128-ctr';
$enc_key = openssl_digest(php_uname(), 'SHA256', TRUE);
$enc_iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length($cipher_method));
$crypted_token = openssl_encrypt($token, $cipher_method, $enc_key, 0, $enc_iv) . "::" . bin2hex($enc_iv);
do{
    $cipher_method = 'aes-128-ctr';
    $enc_key = openssl_digest(php_uname(), 'SHA256', TRUE);
    $enc_iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length($cipher_method));
    $crypted_token = openssl_encrypt($token, $cipher_method, $enc_key, 0, $enc_iv) . "::" . bin2hex($enc_iv);
}
while(is_int(strpos($crypted_token, '/'))); //if there's an int it means a / was found

unset($token, $cipher_method, $enc_key, $enc_iv);
$linkToEstimate = '/viewestimateeasy/'.$crypted_token;

当用户单击电子邮件中的链接时,我会解密该字符串。 该字符串包含他们的用户 ID、电子邮件地址和唯一标识符

list($crypted_token, $enc_iv) = explode("::", $crypted_token);
$cipher_method = 'aes-128-ctr';
$enc_key = openssl_digest(php_uname(), 'SHA256', TRUE);
$token = openssl_decrypt($crypted_token, $cipher_method, $enc_key, 0, hex2bin($enc_iv));
unset($crypted_token, $cipher_method, $enc_key, $enc_iv);

//get the token parts
list($email, $user_id, $magiclink) = explode("|", $token);

我希望 $token 的值看起来像“user@domain.com|423|340934fafasdf9cas532424”。

我得到一个似乎没有正确编码的值。我得到的是这样的:“И�^�I�+y��(�Ƀa��lqj��>O������UDmE�Ĩ���l�")�]” 0��Q"

我的代码

list($email, $user_id, $magiclink) = explode("|", $token);

正在返回错误“未定义的数组键 1”,因为 $token 不包含任何要拆分的管道。

正如我所提到的,这只是随机停止工作。没有对生产环境进行部署。就好像我的代码之外的东西已经改变了。

我用一个旧链接进行了测试,我确信过去可以工作,但我遇到了同样的错误。

有没有人有什么建议?

标签: phpencryption

解决方案


推荐阅读