dtwr2012
2016-09-22 09:00 阅读 36
已采纳

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

The following code loops my decryption I have no idea how to decrypt each inner['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;
}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    douwo1862 douwo1862 2016-09-22 10:02

    As stated in the comments you cannot decrypt a hash. For that you need to use an encryption algorithm, using mcrypt_encrypt() before saving and mcrypt_decrypt() when displaying.

    Seeing as you've already hashed your content, there is no way to get it back. You need to start over, and delete all of the old content, in order to do what you're looking to do.

    点赞 评论 复制链接分享
  • dtfbj24048 dtfbj24048 2016-09-22 09:09

    you have to return an variable $text instead of echo in decr() function

    function decr($text){
        $iterations = 1;
        $salt = mcrypt_create_iv(16, MCRYPT_DEV_URANDOM);
        $text = hash_pbkdf2("sha512", $text, $salt, $iterations, 512);
        return $text;
    }
    
    点赞 评论 复制链接分享

相关推荐