我尝试使用RSA
加密来签署一些数据,但是当我通过表单发送数据时,它会以某种方式更改数据并且它无法正常运行,我的脚本会获取数据并生成一个数字签名,我试图通过表单发送签名并用它验证数据,它在我发送到表单之前有效,但之后没有工作,这是我的代码:
<!doctype html>
<html lang="en">
<head>
<title>Hello, world!</title>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-
to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-
beta.2/css/bootstrap.min.css" integrity="sha384-
PsH8R72JQ3SOdhVi3uxftmaW6Vc51MKb0q5P2rRUpPvrszuE4W1povHYgTpBfshb"
crossorigin="anonymous">
<style media="screen">
body{
padding:60px;
background: white;
}
</style>
</head>
<body>
<?php
if(array_search("",$_POST)){
$config = array(
"digest_alg" => "sha256",
"private_key_bits" => 4096,
"private_key_type" => OPENSSL_KEYTYPE_RSA,
);
// Create the private and public key
$result = openssl_pkey_new( $config );
// Extract the private key from $result to $privKey
openssl_pkey_export( $result, $privKey );
// Extract the public key from $result to $pubKey
$pubKey = openssl_pkey_get_details( $result );
$pubKey = $pubKey["key"];
openssl_sign('hello',$signature,$privKey);
echo openssl_verify('hello',$signature,$pubKey);
}
else{
foreach ($_POST as $key => $value) {
echo "we got ".$key." : ".$value."<br><br><br>"; }
echo openssl_verify($_POST['data'],$_POST['signature'],$_POST['pubKey']);
}
?>
<form action="#" method="post" id="test">
<input type="text" name="data" value="hello">
<textarea name="signature" rows="8" cols="80" form="test"><?php
if(isset($signature)){echo $signature;}?></textarea>
<textarea name="privKey" rows="8" cols="80" form="test"><?php
if(isset($privKey)){echo $privKey;}?></textarea>
<textarea name="pubKey" rows="8" cols="80" form="test"><?php
if(isset($pubKey)){echo $pubKey;}?></textarea>
<input type="submit" value="go">
</form>
</body>
</html>