John Davidson

php - Store encrypted texts in the database , query and decrypt it back base on password

0 comments
Message:


I'm trying to store an encrypted text in my database, but I have no idea how to decrypted it back. I've tried


$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.";
}


My Answer:
$encrypted_text = openssl_encrypt($plain_text, 'AES-256-CBC', $password, 0, $salt);

// Store $encrypted_text in the database

// To decrypt the text back
$decrypted_text = openssl_decrypt($encrypted_text, 'AES-256-CBC', $password, 0, $salt);

echo $decrypted_text;

Rate this post

3 of 5 based on 1857 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us