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;