带有私钥的PHP签名字符串,并与公共

时间:2016-09-13 12:34:55

标签: php

我需要你的PHP帮助。我是noobie in :)所以主要问题:我无法使用公钥验证我的签名,所以当我openssl_verify时,我有一个例外。我想要签名的字符串是generateDataString()函数输出(全部在一行中):

  

0041011003008008234567890051234500210003EUR017Pangakonto   omanik01150060807439005makse062http://本地主机:8080 / WWW / banklink / banklink / banklink / maksa.php062http://本地主机:8080 / WWW / banklink / banklink / banklink / maksa.php0242016-09-13T11:17:00 + 0300005UTF-8003EST < / p>

所以,在第一步中我得到了我的字符串,需要签名。然后我把它打印出来。然后生成签名并将其打印出来(按原样)。然后我将签名编码为base64并将其打印出来。然后我从base64解码它并再次打印出来(我做了所有的打印来说明/调试)。我做的最后一步,我采取解码的签名字符串,并尝试用公钥验证它...但它失败了。所以......请帮帮我。

<html>
<body>
<?php
// Associative array with data
$vkField = array(
        'VK_SERVICE' => '1011',
        'VK_VERSION' => '008',
        'VK_SND_ID' => '23456789',
        'VK_STAMP' => '12345',
        'VK_AMOUNT' => '10',
        'VK_CURR' => 'EUR',
        'VK_NAME' => 'Pangakonto omanik',
        'VK_REF' => '50060807439',
        'VK_MSG' => 'makse',
        'VK_RETURN' => 'http://localhost:8080/www/banklink/banklink/banklink/maksa.php',
        'VK_CANCEL' => 'http://localhost:8080/www/banklink/banklink/banklink/maksa.php',
        'VK_DATETIME' => '2016-09-13T11:17:00+0300',
        'VK_ENCODING' => 'UTF-8',
        'VK_LANG' => 'EST'
);

$preferences = array(
        'bank_private_key' => 'bank_private.pem',
        'bank_certificate' => 'bank_certificate.pem'
);

// Generate string from associative array (vkField)
function generateDataString() {

    global $vkField;
    $data = '';
    foreach($vkField as $field => $value){
        $str_length = strlen("$value");
        $data .= str_pad($str_length, 3, '0', STR_PAD_LEFT) . $value;
        $data = mb_convert_encoding($data, 'UTF-8');
    }
    return $data;
}

echo "<br/>";
echo "String in UTF-8 format: " . "<br/>";
echo generateDataString();

// Generate signature
$key = openssl_pkey_get_private (
    file_get_contents ($preferences['bank_private_key'])
);

if (!openssl_sign (generateDataString(), $signature, $key)) {
        trigger_error ("Unable to generate signature", E_USER_ERROR);
}

echo "<br/>" . "<br/>" . "<br/>";
echo "Signature as it is: ";
echo "<br/>";
echo $signature;
echo "<br/>" . "<br/>" . "<br/>";
echo "Encoded signature to base64: " . "<br/>";
$signature = base64_encode($signature);
echo $signature . "\n" . "<br/>" . "<br/>";
echo "Decoded signature from base64: ";
$signature = base64_decode($signature);
echo "<br/>";
echo $signature;


echo "<br/>" . "<br/>" . "<br/>";
echo "Verify signature:" . " " . "<br/>";
$key2 = openssl_pkey_get_public (file_get_contents ($preferences['bank_certificate']));
if (!openssl_verify (generateDataString(), $signature, $key2)){
    echo "Invalid signature";
}

echo "<br/>" . "<br/>" . "<br/>";
?>
</body>
</html>

私钥:

  

----- BEGIN PRIVATE KEY ----- MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCUMIz8mPepfK6l   MxDK + Zh1M7iMx2Ia43zc + lJzEMpw9gqmYht7OllzM7s3Q9wShxMQxkP1slSAsRDR   zSoMIRRn8K4PXoJh9QB5YdtnZpQ7r + joP1INnn8Wz2D / JnwVVdgqghaxgF6XhnLe   sOx6vUvnLVsb9qayf1fLOGAomyIVs + X8aRzekzTVTp20SYR7cBo8SpwU3Kgi4qBK   ufkup7gO1ri97dqn / fd1uFekfXTsC46A0saSpsEL9zlOKo + cM5xJQPvy9y8yvEZQ   1jOcgtykkOGZez44a1DNFiW9pZRMJbIpskUDfZiroNmqSFmoUsfA2Gvw + 4p3P9iQ   nd4ZylP7AgMBAAECggEAewcHtZolWr + ZmqsXkPvA1 + S1naLWTu54NllX5BGXkEqk   MtgWaqrZomvbTRl4dqVPbci8mRmRUDowdY24D9PSxiwpxba31i16VEuqxGCERj1X   xqDzNT1V05EdvRatQ2U + jqPrxTeG0JOdgEYwPgI0Jtwf3MpcQKq6L4xBQNTyakLj   2vXuqcGF8VGAt90BdYkLHQkIG5jabkYEBhZS6glWVNmF + FLkWbHHhuqfeVicYuQl   tZQvkikdtrtS8cve4t4alt8JEkZP5MaOKwLvpQZamNszJmky2qFfqM8luh4UKKaM   sQK8jmdMK22wdvH0KQPmTKC + NC / fjGl4DKdP9nXyCQKBgQDs2q5rczEM4dIED2oo   l4152s9S / FuC4uQxAPe9zU8bZueTPRyoGkWsBF6usFArPfEimauthhhkZZVJ0h26   aIXWCAG6HEs4Eulhl8IVcVWKOuwDhJr6dPypgnw3XyRj9oOZxQcInaIs9hxTQhQp   AE9 + aBnZxEshVpsFvPGzdLTbDQKBgQCgKxkfHaBBZHHbth0A8oemdtBX / mtoZVri   R8a3RfrmDgMXn7TzFqDfJGIpBuJNx9QFWg5649qNYG + x2hK2A1HeE7GtGW9zwxkk   ExPLeNDjIZrpVoZTTOtETMQ8pY / hrQSLE9e0tiB6xETSuIluk1tuMc4FfNHHzpY1   EtPSsE + JJwKBgQDXwzpCrdPQDWLYygYF5lE94SMeUL84U2JWdbA5We8nYi2wxmn1   hMFVO0HZVsohKjMhNuXalB5iTtuZPOxR5GePGwoftSicczXg7nMiP3USECzx43me   kLWd4T0w393xph4qFP8cTSTwWl5hRf / BfgPsff6Wjyd1vkRIDOANf1JFfQKBgDOz   lclgg9F3 / fThRV1 / 7F / ErJ4q5zrstLikWLXiZgoK7d6bVAq // C / ri7AYo8Zdwcf0   eQYcNPk77vInJbERQ0T / cHQi6MIicSQe8F3W4v7eYyX9IyESjEMg0JuPCfHcKsJG   JnQT8koOvvSSUvKZqILamtlUoFbyDbSWHkA9X38zAoGBAL5NQy6VxV4ey3aTk31b   rrGNbXxKR4JuaP3gJIlA8bf3QTjTmyJO / gAYVoQQZebUb4Xmp6tUupIu1dW2xTdk   6HC + vKM2 + 1jiN4AjQ0hWDRRfatftWzPowm4XCFum1pUz61jAAEupawGHQ1TPhIg8   2QZnTbklTOv0dTRLulv9Nrv8   ----- END PRIVATE KEY -----

公钥:

  

-----开始证书----- MIIDYDCCAkigAwIBAgIGAUl1tzczMA0GCSqGSIb3DQEBBQUAMHExCzAJBgNVBAYT   AkVFMREwDwYDVQQIEwhIYXJqdW1hYTEQMA4GA1UEBxMHVGFsbGlubjETMBEGA1UE   ChMKTm9yZGVhVGVzdDETMBEGA1UECxMKTm9yZGVhVGVzdDETMBEGA1UEAxMKTm9y   ZGVhVGVzdDAeFw0xNDExMDMxMjUyNTFaFw0zOTEwMjgxMjUzMDFaMHExCzAJBgNV   BAYTAkVFMREwDwYDVQQIEwhIYXJqdW1hYTEQMA4GA1UEBxMHVGFsbGlubjETMBEG   A1UEChMKTm9yZGVhVGVzdDETMBEGA1UECxMKTm9yZGVhVGVzdDETMBEGA1UEAxMK   Tm9yZGVhVGVzdDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAJ4OkLy7   EIIpfU + nKB9aFQ9UU ++ eYfftotXth6jsSiiBfGCcDPv692u0uHIkxmtpen9UGBM6   IHF8wQKQJlyHadcMptl9uGKd1rXh7Or6 / yZJJnyzZKwfHurzq7BV2lNThZaMPM9H   bvfsjNyuCcTygxO + d5CBezlU4DoakUTA2S6NdQdNuvSEaCcSDAlEvSX3eVsfcX2A   TLk6obqme + SRqGctGXHMw3d4jIEXqddiufY9SsoKK2JsWes1G8fWVO11jbfCkPZ4   gImwII1SMR4afc1Rj9a10fyU3slx + AQlMNrCcsPOD10GpIqWxDmdtSI5gdbX15yZ   u1S + nwQszFrKp40CAwEAATANBgkqhkiG9w0BAQUFAAOCAQEAEdC7H / IAVCE / ejyk   vTy3ebT / ui51qlg1OdtZzy9SYCjRphnSDn6Yu / JLq3kEsXQ5 / 41Zcl0IR1GGsl4X   TRzZmxUjuHLkes0YPBCyFi + SI70WNRFGe3oUIWpyWKbWk / jxtw + L / m5uD7LtHgMt   VlpI / hBYWh0GZvI3QYpyB44AKXIfmD9JUmHhcM84oH76Xje5EJ2kYVopDLkwDF7M   CjGavEpwYobOOd / Dwtouvm9NEFnXeuKPGAdmE0Z3SyI9ZUvT88r1w4SUMKGEIBFy   KlkO4nmRrCoGktJjOgZWRtemfLXVxKRoN + RDVntajKD + lafQ1PsPHtrSpuGwkX / 1   hBRa / A ==   -----结束证书-----

0 个答案:

没有答案