简体   繁体   English

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

[英]Passing encrypted data by POST in the form in PHP

I am genereting two keys in sha512 and then I put them in separated files. 我在sha512中创建两个密钥,然后将它们放在单独的文件中。 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. 之后,我读取index.php中的公钥,将表单发布到同一页面上,并以仅在发布时才生成的其他形式对输入在输入中的数据进行编码。

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 Index.php文件

    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 decryp文件

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;

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. 对于那些试图发布加密字符串的人,将不会真正起作用,因为在加密时会生成特殊字符,这些问题使传递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 PHP将字符串转换为十六进制,并将十六进制转换为字符串

from Philippe Gerber 来自Philippe Gerber

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM