如何使用AES加密和解密消息

问题描述 投票:0回答:1

我正在尝试使用 AES 加密和解密一条简单的消息,到目前为止尚未成功。

我分享我的代码:

index.html

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Chiffrement AES</title>
  <link rel="stylesheet" href="styles.css">
</head>
<body>
  <div class="container">
    <h2>Chiffrement AES</h2>
    <textarea id="message" placeholder="Entrez votre message"></textarea>
    <button onclick="encryptMessage()">Crypter</button>
    <div id="encryptedMessage"></div>
  </div>

  <textarea id="encryptedText" placeholder="Entrez le message crypté"></textarea>
    <button onclick="decryptMessage()">Décrypter</button>
    <div id="decryptedMessage"></div>


  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  <script src="script.js"></script>
</body>
</html>

脚本.js :

function encryptMessage() {
    var message = $('#message').val();
  
    $.post("encrypt.php", { message: message }, function(data) {
      $('#encryptedMessage').text("Message crypté : " + data);
    });
  }

function decryptMessage() {
    var encryptedText = $('#encryptedText').val();

    $.post("decrypt.php", { encryptedText: encodeURIComponent(encryptedText) }, function(data) {
        $('#decryptedMessage').html("Message décrypté : " + data);
    });
}

加密.php :

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {

    echo "<pre>";
    print_r($_POST);
    echo "</pre>";

    $message = $_POST['message'];

    $secretKey = "CleSecrete";

    $cipher = "aes-256-cbc";

    $ivlen = openssl_cipher_iv_length($cipher);
    $iv = openssl_random_pseudo_bytes($ivlen);

    $encrypted = openssl_encrypt($message, $cipher, $secretKey, 0, $iv);

    echo "Message: " . $message . "<br>";
    echo "Encrypted Text: " . $encrypted . "<br>";
}
?>

解密.php :

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    echo "<pre>";
    print_r($_POST);
    echo "</pre>";

    // Récupère le texte crypté envoyé depuis JavaScript et le décode
    $encodedEncryptedText = $_POST['encryptedText'];
    $encryptedText = urldecode($encodedEncryptedText);

    // Clé utilisée pour le chiffrement (doit être la même que celle utilisée pour le chiffrement)
    $secretKey = "CleSecrete";

    // Algorithme de chiffrement et mode
    $cipher = "aes-256-cbc";

    // Longueur du vecteur d'initialisation pour l'algorithme de chiffrement
    $expected_iv_length = openssl_cipher_iv_length($cipher);

    // Récupération du vecteur d'initialisation et du texte chiffré
    $iv = substr($encryptedText, 0, $expected_iv_length);
    $encrypted = substr($encryptedText, $expected_iv_length);

    // Affichage de la longueur attendue du vecteur d'initialisation
    echo "Expected IV Length: " . $expected_iv_length . "<br>";

    // Longueur réelle du vecteur d'initialisation reçu
    $received_iv_length = strlen($iv);
    echo "Received IV Length: " . $received_iv_length . "<br>";

    if ($received_iv_length !== $expected_iv_length) {
        echo "Error: Received IV Length does not match the expected length. Please check.<br>";
    }

    // Affichage du texte crypté reçu pour vérification
    echo "Encrypted Text Received: " . $encryptedText . "<br>";
    echo "IV: " . $iv . "<br>";
    echo "Encrypted: " . $encrypted . "<br>";

    // Décryptage du texte
    $decrypted = openssl_decrypt($encrypted, $cipher, $secretKey, 0, $iv);

    if ($decrypted === false) {
        while ($msg = openssl_error_string()) {
            echo $msg . "<br>";
        }
    } else {
        // Affichage du texte décrypté pour vérification
        echo "Decrypted Text: " . $decrypted . "<br>";
    }
}
?>

这是加密“hello world”后的结果: 消息加密:

数组([message] => hello world)
消息:hello world
加密文本:FS+bGfvAuuu9RWrjM0/yvQ==

这是尝试解密消息 FS+bGfvAuuu9RWrjM0/yvQ== 后的结果:

消息解密:

数组 ( [加密文本] => FS%2BbGfvAuuu9RWrjM0%2FyvQ%3D%3D )

预期 IV 长度:16 收到的 IV 长度:16 收到加密文本:FS+bGfvAuuu9RWrjM0/yvQ== IV: FS+bGfvAuuu9RWrj 加密:M0/yvQ== 错误:1C80006B:提供程序例程::最终块长度错误

我尝试了很多修改,但没有一个是决定性的,有人可以帮助我吗? 谢谢大家

我尝试加密/解密坚果,但它还不起作用

javascript php encryption aes
1个回答
0
投票

您可以使用CryptoJS库进行解密,并根据您的配置自定义此功能。

function Decrypt() {
    var ciphertext = ciphertext ;
    var key = secretKey;
    var iv = iv;

    var ciphertextWA = CryptoJS.enc.Hex.parse(ciphertext);
    var keyWA = CryptoJS.enc.Utf8.parse(key);
    var ivWA = CryptoJS.enc.Utf8.parse(iv);
    var ciphertextCP = { ciphertext: ciphertextWA };

    var decrypted = CryptoJS.AES.decrypt(
      ciphertextCP,
      keyWA, 
      { iv: ivWA }
   );

 console.log(decrypted.toString(CryptoJS.enc.Utf8)); 
}
© www.soinside.com 2019 - 2024. All rights reserved.