【问题标题】:How to Encrypt messages using PHP如何使用 PHP 加密消息
【发布时间】:2022-01-16 18:36:50
【问题描述】:

我需要帮助来做这个聊天应用程序,我已经用 pass_hash 加密了注册密码和登录密码。现在我需要帮助加密消息,这对于我目前的编码知识来说真的很难。我想要做的是在数据库上对消息进行加密,而不必在 transint 中加密。如果有人可以帮助我,我将不胜感激!我已经尝试编写此代码,但在聊天时返回空,我不知道为什么。

插入聊天

<?php 
    session_start();
    if(isset($_SESSION['unique_id'])){
        include_once "config.php";
        $outgoing_id = $_SESSION['unique_id'];
        $incoming_id = mysqli_real_escape_string($conn, $_POST['incoming_id']);
        $message = mysqli_real_escape_string($conn, $_POST['message']);
        $message_to_encrypt = $message;
        $secret_key = 'mysecretkey' ;
        $method = "aes128";
        $iv_length = openssl_cipher_iv_length($method);
        $iv = openssl_random_pseudo_bytes($iv_length);

        $encrypted_message = openssl_encrypt($message_to_encrypt, $method, $secret_key, 0, $iv);

        if(!empty($message)){
            $sql = mysqli_query($conn, "INSERT INTO messages (incoming_msg_id, outgoing_msg_id, msg)
                                        VALUES ({$incoming_id}, {$outgoing_id}, '{$encrypted_message}')") or die();
        }
    }else{
        header("location: ../login.php");
    }


    
?>

聊天

<?php 
    session_start();
    if(isset($_SESSION['unique_id'])){
        include_once "config.php";
        $outgoing_id = $_SESSION['unique_id'];
        $incoming_id = mysqli_real_escape_string($conn, $_POST['incoming_id']);
        $output = "";
        $sql = "SELECT * FROM messages LEFT JOIN users ON users.unique_id = messages.outgoing_msg_id
                WHERE (outgoing_msg_id = {$outgoing_id} AND incoming_msg_id = {$incoming_id})
                OR (outgoing_msg_id = {$incoming_id} AND incoming_msg_id = {$outgoing_id}) ORDER BY msg_id";
        $query = mysqli_query($conn, $sql);
        
        $message_to_encrypt = $row['msg'] ;
        $secret_key = "mysecretkey";
        $method = "aes128";
        $iv_length = openssl_cipher_iv_length($method);
        $iv = openssl_random_pseudo_bytes($iv_length);
        $encrypted_message = openssl_encrypt($message_to_encrypt, $method, $secret_key, 0, $iv);

        $decrypted_message = openssl_decrypt($encrypted_message, $method, $secret_key, 0, $iv);

        if(mysqli_num_rows($query) > 0){
            while($row = mysqli_fetch_assoc($query)){
                if($row['outgoing_msg_id'] === $outgoing_id){
                    $output .= '<div class="chat outgoing">
                                <div class="details">
                                    <p>'.$row['msg']  .'</p>
                                </div>
                                </div>';
                }else{
                    $output .= '<div class="chat incoming">
                                <img src="php/images/'.$row['img'].'" alt="">
                                <div class="details">
                                    <p>'.$row['msg'].'</p>
                                </div>
                                </div>';
                }
            }
        }else{
            $output .= '<div class="text">Sem mensagens disponiveis.Envie uma agora :)</div>';
        }
        echo $output;
    }else{
        header("location: ../login.php");
    }

?>

chat

source code git hub

【问题讨论】:

  • 在您尝试插入之前,这些值是否正确?它们是否正确进入数据库但未正确提取和显示?调试时,您的代码的哪一部分不工作?
  • 我不明白您为什么要从数据库中检索(大概)加密的消息,再次对其进行加密,然后解密您刚刚加密的消息。然后,您对这些变量什么都不做,只显示数据库中的msg 列。当然,您只需要检索它、解密它,然后显示解密的消息吗?
  • 出于各种原因,您还需要阅读准备好的语句,而不是像这样将字符串连接到查询中。我不进行加密,但如果加密的字符串有可能包含单引号,您的查询将失败并出现语法错误,因为它写在这里。
  • 用户 wtites 将加密到数据库 (msg) 中的消息然后在代码中“get-chat.php”在数据库中进行重新搜索并写入值 (msg),但是问题是我希望消息仅在用户显示中显示为解密
  • 警告:您对SQL Injections 持开放态度,应该使用参数化的prepared statements,而不是手动构建查询。它们由PDOMySQLi 提供。永远不要相信任何形式的输入!即使您的查询仅由受信任的用户执行,you are still in risk of corrupting your dataEscaping is not enough!

标签: php encryption


【解决方案1】:

您可以在 Mysql 中使用 AES_DECRYPT() 和 AES_ENCRYPT() 来加密和解密您想要的任何数据。 查看更多here

【讨论】:

  • 我会尝试但我将 AES_ENCRYPT() 放在 // VALUES ({$incoming_id}, {$outgoing_id}, '{$encrypted_message}')") 或 die();/ / ?
  • 首先定义一个KEY,然后创建一个这样的sql:“INSERT INTO chats (incoming_id,outgoing_id,encrypted_message) VALUES ($incoming_id','$outgoing_id',AES_ENCRYPT('$encrypted_message',' ".KEY."'));";
猜你喜欢
  • 2012-01-17
  • 1970-01-01
  • 2014-01-09
  • 2022-01-28
  • 2023-03-05
  • 1970-01-01
  • 2021-04-21
  • 2020-11-01
  • 2021-07-04
相关资源
最近更新 更多