加密 PHP 字符串并在页面重新加载后解密

Encrypt PHP string and decrypt after page reload

我正在创建一个注册页面,当您尝试注册时,它将生成一个随机的六位数字。然后这个号码将通过电子邮件发送给您,然后您在浏览器中输入该号码以 'verify'.

虽然我在加密字符串然后在页面重新加载后解密它时遇到问题,但我通过一种不仅实际上不起作用的形式传递密钥和 iv_size,我也会假定它非常不安全,因为 IV 应该保密。

问题来了,我如何加密 PHP 中的字符串并在页面重新加载后解密它,本质上是一个新页面,因为没有变量保留。

向您提供我当前的代码有点傻,因为它无法正常工作,但如果您认为可以修复它,就在这里;

$randInt = mt_rand(0, 9);
            for($i=1; $i<=6; $i++){
                $randInt .= mt_rand(0, 9);
            }
            $key = pack('H*', "bcb04b7e103a0cd8b54763051cef08bc55abe029fdebae5e1d417e2ffb2a00a3");
            $key_size =  strlen($key);
            $plaintext = $randInt;
            $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
            $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
            $ciphertext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $plaintext, MCRYPT_MODE_CBC, $iv);
            $ciphertext = $iv . $ciphertext;
            $ciphertext_base64 = base64_encode($ciphertext);

            ?>
            <script>
                notify('Please Enter The Verification Number We Sent You Below', "#C60");
            </script>
            <?
            echo('We sent a verification code to the email provided, please enter it below to continue with your sign up process.');

            ?>
            <form method="post">
                <input type="hidden" name="encryptedKey" value="<?php echo($ciphertext_base64);?>">
                <input type="hidden" name="ivSize" value="<?php echo($iv_size);?>">
                <input type="hidden" name="key" value="<?php echo($key);?>">
                <input type="number" name="verify" id="verify">
            </form>

这是应该解密的部分:

$iv_size = $_POST['ivSize'];
            $key = $_POST['key'];
            $ciphertext_dec = base64_decode($_POST['encryptedKey']);
            $iv_dec = substr($ciphertext_dec, 0, $iv_size);
            $ciphertext_dec = substr($ciphertext_dec, $iv_size);
            $plaintext_dec = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $ciphertext_dec, MCRYPT_MODE_CBC, $iv_dec);
            echo  $plaintext_dec . "\n";

就算能修好,也太不安全了。我得到的错误是告诉我密钥太长,如果我将两个代码块放在一起,它就可以工作,但是当我将它们分开时,它们就不起作用。我觉得表格在传递时弄乱了变量。

如果您正在编码和解码整数,我建议您查看 Hashids Lib http://hashids.org/php/.

$hashids = new Hashids\Hashids();
$id = $hashids->encode(1, 2, 3);
// Output $id => laHquq

您可以使用数字数组或单个数字并限制编码级别中的字符。

阅读Hashids文档你就会明白

这个回答是针对我之前回答中他在评论区提出的问题。 [不是他原来问题的答案]

Html 文件

<form method="POST" action="#" id="validation-form">
<input id="validation-key" name="validation-key">
</form>

 <script>
      $('#validation-form').submit(function(){
            var data = $('#validation-form').serialize();
            $.ajax({
                url:"validation_key.php",
                type:"POST",
                data:data,
                success:function(result){
                    // do what when the form validation is success
                    alert(result);

                },
                error:function( jqXHR, textStatus, errorThrown){
                    alert("Something went wrong");
                }
            })
      })
</script>

PHP 验证文件

<?php
if (isset($_POST['validation-key'])) {
    // DO what you to validate the key is in here

    if($_POST['validation-key'] == "123456"){
        echo "Valid key";
    }else{
        echo "Invalid key";
    }


}else{
    echo "Validation key is missing";
}

不要忘记添加 jquery 库 http://code.jquery.com/jquery-migrate-1.2.1.min.js