如何在foreach循环中解密变量?



下面的代码循环我的解密我不知道如何解密每个内部['post_text']

https://gyazo.com/672cd615b86b3c107da7e2c386d3b2f9

if (!empty($_SESSION['room_id'])) {
    $getPosts = $verbinding->prepare("SELECT user_name, post_text FROM posts WHERE room_id = :room_id");
    $getPosts->bindParam(':room_id', $_SESSION['room_id']);
    $getPosts->execute();
    $posts = $getPosts->fetchAll(PDO::FETCH_ASSOC);
    foreach ($posts as $inner) {
        $username = $inner['user_name'];
        $text = $inner['post_text'];
        $um = "@4um:~$";
        echo "<br><div class = "posts">" . $username . "$um&nbsp" . decr($text) . " </div>";
    }
}
function decr($text){
    $iterations = 1;
    $salt = mcrypt_create_iv(16, MCRYPT_DEV_URANDOM);
    $text = hash_pbkdf2("sha512", $text, $salt, $iterations, 512);
    echo $text;
}

如注释所述,您无法解密散列。为此,需要使用加密算法,保存前使用mcrypt_encrypt(),显示时使用mcrypt_decrypt()

看到你已经散列你的内容,没有办法得到它回来。你需要重新开始,并删除所有旧的内容,以便做你想做的事情。

您必须返回变量$text而不是decr()函数中的echo

function decr($text){
    $iterations = 1;
    $salt = mcrypt_create_iv(16, MCRYPT_DEV_URANDOM);
    $text = hash_pbkdf2("sha512", $text, $salt, $iterations, 512);
    return $text;
}

最新更新