-1

我在这里得到了答案,如何在 javascript 中加密发布数据并在服务器端对其进行解密。我在使用我的 ajax 表单提交实现 javascriot 时遇到了一个问题。我尝试在我的代码片段中使用我的 ajax 来实现它,但它没有按预期工作。

我没有提前使用带有此功能的 jQuery,但我可以与提供此线程正确解决方案的人一起学习。我在同一个平台stackoverflow上显示了多个线程,但问题有点相似,但我的答案完全是端到端的不同。请不要将其标记为垃圾邮件/重复/待处理。谢谢stackover开发者,

$("#form").unbind("submit").bind("submit", function(e) {
  e.preventDefault();

  function encrypt() {
    var key = CryptoJS.enc.Hex.parse("0123456789abcdef0123456789abcdef");
    var iv = CryptoJS.enc.Hex.parse("abcdef9876543210abcdef9876543210");

    var newpassword = $('input[name="newpassword"]');

    var hash = CryptoJS.AES.encrypt(newpassword, key, {
      iv: iv
    });
    $('input[name="newpassword"]').val() = hash;

    //alert(hash);
    return false;
  }


  grecaptcha.ready(function() {
    grecaptcha.execute("RECAPTCHA_SECRET_CODE", {
      action: "updatepass"
    }).then(function(token) {
      let formData = {
        token: token,
        newpassword: newpassword
      };
      timeExecute = new Date().getTime();
      $.ajax({
        type: "POST",
        data: formData,
        cache: false,
        success: function(response) {
          if (response.status == "success") {
            $(form)[0].reset();
            alert("Password Changed");
          } else {
            alert("ERROR");
          }
        },
        timeout: 10000,
        async: false
      });
    });
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/aes.js"></script>
<script src="//crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/md5.js"></script>
<script src="https://www.google.com/recaptcha/api.js?render=RECAPTCHA_SECRET_CODE"></script>
<form class="cust-form" id="form" method="POST">
  <div class="password-input">
    <input type="password" id="reg_pass" class="form-control" autofocus="true" name="newpassword" autocomplete="off" />
  </div>
  <button class="btn btn-default" id="passwordBtn">Change Password</button>
</form>

4

1 回答 1

1
$("#form").unbind("submit").bind("submit", function(e) {
      e.preventDefault();
      encrypt();//call the function you declared
      function encrypt() {
          var key = CryptoJS.enc.Hex.parse("0123456789abcdef0123456789abcdef");
          var iv = CryptoJS.enc.Hex.parse("abcdef9876543210abcdef9876543210");

          var newpassword = $('input[name="newpassword"]').val();//get the value

          var hash = CryptoJS.AES.encrypt(newpassword, key, {
            iv: iv
          });
          $('input[name="newpassword"]').val(hash);//insert the hashed value
          
          //alert(hash);
          return false;
      }
      $('#form').submit();//submit the form manually if you use e.preventDefault()
});

在你的button元素中:

<button type="submit" class="btn btn-default" id="passwordBtn">Change password</button>
于 2020-07-15T03:24:29.197 回答