问题
I have this error with this file:
<?php
// $data and $signature are assumed to contain the data and the signature
$signature = null;
$toSign = "C:/Users/User/Desktop/xampp/htdocs/docum.docx";
$fp = fopen("key.pem", "r");
$priv_key = fread($fp, 8192);
fclose($fp);
$pkeyid = openssl_get_privatekey($priv_key);
openssl_sign($toSign, $signature, $pkeyid);
openssl_free_key($pkeyid);
echo($signature);
// fetch public key from certificate and ready it
$fp = fopen("C:/Users/User/Desktop/xampp/htdocs/pubkey.der", "r");
$cert = fread($fp, 8192);
fclose($fp);
$pubkeyid = openssl_get_publickey($cert);
// state whether signature is okay or not
$ok = openssl_verify($toSign, $signature, $pubkeyid);
if ($ok == 1) {
echo "good";
} elseif ($ok == 0) {
echo "bad";
} else {
echo "ugly, error checking signature";
}
// free the key from memory
openssl_free_key($pubkeyid);
?>
how can I fix this error ?`... I calculated the signature with the private key to the document, now I want to test it. at first I created two php files , the first one that signed the document , the second occurred ke me signing . I just do not know how to take the signature from the first documento.Ho decided to put it all together to try ... How can I fix ?
回答1:
- Are you absolutely certain that your public key file is shorter than 8192 bytes? If you're just looking to read a file into a variable use
file_get_contents()
, it's far simpler. - What did
openssl_get_publickey($cert)
return? - Based on the error you got it looks like OpenSSL expects PEM formatted keys, so you'll need to convert it.
Try:
function der2pem($der_data) {
$pem = chunk_split(base64_encode($der_data), 64, "\n");
$pem = "-----BEGIN CERTIFICATE-----\n".$pem."-----END CERTIFICATE-----\n";
return $pem;
}
$cert = der2pem(file_get_contents('C:/Users/User/Desktop/xampp/htdocs/pubkey.der'));
if( ! $pubkeyid = openssl_get_publickey($cert) ) {
throw new \Exception(openssl_error_string());
}
来源:https://stackoverflow.com/questions/34140563/openssl-verify-warning-openssl-verify-supplied-key-param-cannot-be-coerced