openssl_verify,警告:openssl_verify():提供的密钥参数不能强制转换为公共密钥 [英] openssl_verify , Warning: openssl_verify(): supplied key param cannot be coerced into a public key

查看:1219
本文介绍了openssl_verify,警告:openssl_verify():提供的密钥参数不能强制转换为公共密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此文件出现此错误:

<?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);
?>

如何解决此错误?`... 我使用文档的私钥计算了签名,现在我想对其进行测试. 最初我创建了两个php文件,第一个是对文档进行签名的文件,第二个是使我签名的文件.我只是不知道如何从第一份文档中获得签名.何先生决定将所有内容放在一起尝试...我该如何解决?

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. 您是否完全确定您的公钥文件少于8192个字节?如果您只是想使用file_get_contents()将文件读入变量,则要简单得多.
  2. openssl_get_publickey($cert)返回了什么?
  3. 根据错误信息,您似乎认为OpenSSL需要PEM格式的密钥,因此您需要对其进行转换.
  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.
  2. What did openssl_get_publickey($cert) return?
  3. Based on the error you got it looks like OpenSSL expects PEM formatted keys, so you'll need to convert it.

尝试:

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());
}

这篇关于openssl_verify,警告:openssl_verify():提供的密钥参数不能强制转换为公共密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆