我正在嘗試將加密文本存盤在我的資料庫中,但我不知道如何將其解密。我試過了
$salt = 'c0d4#';
$pepper ='nsa-cia-fbi'; // secret text
$pwd_peppered = hash_hmac("sha256", $salt, $pepper);
$pwd_hashed = password_hash($pwd_peppered, PASSWORD_ARGON2ID);
echo($pwd_hashed);
// right password hash
$pwd_hashed = '$argon2id$v=19$m=65536,t=4,p=1$QnVpT1Rqay5WSmIvRW1HZg$rgx DWPl5bvjwlr7plnOjnE1Sf8lim01pwb6lHGzEaU';
//wrong password hash : for testing purposes
$pwd_hashed_wrong = '$argon2id$v=19$m=65536,t=4,p=1$QnVpT1Rqay5WSmIvRW1HZg$rgx DWPl5bvjwlr7plnOjnE1Sf8lim01pwb6lHGzEaU-wrong-!!';
if (password_verify($pwd_peppered, $pwd_hashed)) {
echo "Password matches.";
// I am inside this block of codes, but ...
// no idea how to decrypt and get my text back ... ??
}
else {
echo "Password incorrect.";
}
uj5u.com熱心網友回復:
您無法解密散列文本,如果您想進行密碼驗證,您必須對用戶輸入的密碼進行散列并使用真實密碼的散列對其進行測驗(查看 2 個散列是否相等)
uj5u.com熱心網友回復:
有兩個概念,hash和encrypt。當你散列一個字串時,你不能把它變回原來的字串。密碼必須經過哈希處理,然后您才能存盤它們。如果您加密一個字串,您可以將結果恢復為原始字串。在 Laravel 中,您可以使用Illuminate\Support\Facades\Crypt類。有一個例子:
<?php
namespace App\Http\Controllers;
use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class UserController extends Controller
{
/**
* Store a secret message for the user.
*
* @param Request $request
* @param int $id
* @return Response
*/
public function storeSecret(Request $request, $id)
{
$user = User::findOrFail($id);
$user->fill([
'secret' => encrypt($request->secret)
])->save();
}
}
或者
use Illuminate\Support\Facades\Crypt;
$encrypted = Crypt::encryptString('Hello world.');
$decrypted = Crypt::decryptString($encrypted);
正如laravel在本檔案中提到的那樣。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/321848.html
