douningzhi1991 2019-05-11 18:36
浏览 360
已采纳

通过POST以PHP形式传递加密数据

I am genereting two keys in sha512 and then I put them in separated files. After that I read the public key in the index.php, post the form to the same page and encode the data posted in the input in other form generated only when have the post.

When I try to post the encrypted data to another page, to decryp, nothing happens on the decryp page.

Looks like the encrypted data posted is not valid to the private key.

What I am trying to do is to simulate a comunication between two servers with encrypted data.

Index.php file

    if (isset($_POST['name']) ) {
        $file = fopen('chave_publica.txt', 'r');
        $file2 = fopen('chave_privada.txt', 'r');
        $publicKey =  fread($file,filesize("chave_publica.txt"));
        // $chavePrivada =  fread($file2,filesize("chave_privada.txt"));
        // echo 'Valor digitado: '.$_POST['name'].'<br>';
        // Encrypting
        openssl_public_encrypt($_POST['name'], $criptedData, $publicKey);

        echo $criptedData;
        // decrypting
        // openssl_private_decrypt($criptedData, $decriptado, $chavePrivada);
        // echo '<br>'.'Valor decriptado: '. $decriptado;
    }

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>

    <!-- <?php echo $criptedData ?> -->
    <?php if (isset($_POST['name']) ) { ?>
        <form method="post" style="margin-top: 20px;" action="decryp.php">
        <textarea name="name"><?php echo $criptedData; ?></textarea>
            <button type="submit">Send</button>
        </form>
    <?php }else{ ?>
        <form method="post" style="margin-top: 20px;" action="index.php" enctype="application/x-www-form-urlencoded">
            <input type="text" name="name" placeholder="Seu nome aqui">
            <button type="submit">Encriptar</button>
        </form>
    <?php } ?>
</body>
</html>

the decryp file

echo $cripted = $_POST['name'];

$file2 = fopen('chave_privada.txt', 'r');
$privateKey =  fread($file2,filesize("chave_privada.txt"));
// $decrypted = 'a';

openssl_private_decrypt($cripted, $decrypted, $privateKey);

echo '<br>'.'Valor decrypted: '. $decrypted;
  • 写回答

1条回答 默认 最新

  • dpkajqd31574096 2019-05-17 01:07
    关注

    For those who are trying to make the post of the encrypted string, will not really work, because when encrypting are generated special characters, these give problem to pass in POST.

    The solution I found was to convert the string to hexadecimal and decode it on the other side.

    bin2hex("that's all you need");
    # 74686174277320616c6c20796f75206e656564
    
    hex2bin('74686174277320616c6c20796f75206e656564');
    # that's all you need
    

    PHP convert string to hex and hex to string

    from Philippe Gerber

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?