我在使用 Codeigniter 4 和加密服務時遇到了一個問題。一切正常,我可以毫無問題地加密和解密,但我想檢測加密字串何時出現錯誤“解密:身份驗證失敗”。
基本上我要做的就是這樣:
- 獲取加密字串
- 解密字串:如果解密成功則繼續執行腳本,否則給出404錯誤
這是我的測驗代碼,也許它有助于解釋我的問題
$ct1 是正確的加密字串,$ct2 是相同的字串但更改了第一個字符,只是為了模擬錯誤,第一個正確解密,第二個給出錯誤,我想檢測這個錯誤并顯示404頁面(我知道如何顯示404頁面,我只需要一種檢測錯誤的方法)。
$enc = \Config\Services::encrypter();
$ct1 = "9c68ef9159b..."; //The string is very long, I don't think is necessary to write it all
$ct2 = "1c68ef9159b...";
print_r($enc->decrypt(hex2bin($ct1)));
print_r($enc->decrypt(hex2bin($ct2)));
在此先感謝所有愿意幫助我的人。
uj5u.com熱心網友回復:
您可以為解密創建一個函式,如下所示:
public function decrypt_token($ct1){
try{
$enc->decrypt(hex2bin($ct1))
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
// or
header('Location: http://www.example.com/404');
}
}
有關更多資訊,您可以在這里閱讀。
uj5u.com熱心網友回復:
只是為了記錄,這里是 Codeigniter 4 中使用的完整代碼。
public function testEncryption()
{
$enc = \Config\Services::encrypter();
$encrypted_string = "9c68ef9159...";
try
{
// print the encrypted string
echo $enc->decrypt(hex2bin($encrypted_string));
}
catch(\Exception $e)
{
// or if encrypted_string is not valid then show 404 page or do whatever you want
return view('errors/html/error_404');
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/348584.html
標籤:php 代码点火器 加密 codeigniter-4
下一篇:將資訊從AJAX傳遞到控制器類
