PHP使用客户端公钥验证客户端签名

时间:2013-03-19 02:58:54

标签: c# php signature verify phpseclib

您好我正在构建REST服务并计划消除语言限制,以便从此项目开发客户端和服务器,当前使用PHP编写的REST服务器。

为了安全起见,我正在验证他们的哈希码以及他们的客户端应用签名。目前哈希是成功的但是对于签名它总是失败。

每个客户端都有自己的私钥,在服务器上我们将使用他们的公钥来验证签名,每个服务器请求都会发送一个签名。 如果客户端是Web服务器,则所有用户只有一个私钥。如果客户端是本机应用程序(C#),则每个已安装的客户端应用程序都将拥有其唯一的私钥。

PHP客户端到PHP REST服务器 - >计算哈希确定,验证客户端签名确定 C#client(winapp)到PHP REST服务器 - >计算哈希确定,验证客户端签名失败

将来我想尝试使用JAVA,反之亦然。

我使用简单的方法创建自签名证书

openssl genrsa -des3 -out netclient.key 2048
openssl req -new -x509 -key netclient.key -out netclient.crt
openssl pkcs12 -export -inkey netclient.key -in netclient.crt -out netclient.p12

以下是使用C#

对数据进行签名的代码
public static string (string Base64EncryptedData ) {
X509Certificate2 my;
my = new X509Certificate2("cert/netclient.p12", "abcdefg", X509KeyStorageFlags.Exportable | X509KeyStorageFlags.PersistKeySet);

RSACryptoServiceProvider csp = null;

csp = (RSACryptoServiceProvider)my.PrivateKey;

SHA1Managed sha1 = new SHA1Managed();
UnicodeEncoding encoding = new UnicodeEncoding();
byte[] data = encoding.GetBytes(Base64EncryptedData);
byte[] hash = sha1.ComputeHash(data);

return Convert.ToBase64String(csp.SignHash(hash, CryptoConfig.MapNameToOID("SHA1"))); 
}

它返回一个签名(base64),我使用GET方法发送给PHP REST服务器。 在PHP服务器上我重新计算哈希并验证但它永远不会返回1.目前我手动将所有密钥复制到服务器。

set_include_path(get_include_path() . PATH_SEPARATOR ."lib");
include("Crypt/RSA.php");

$p12cert = array();
$fp=fopen("cert/netclient.p12","r");
$priv_key=fread($fp,8192);
fclose($fp);
openssl_pkcs12_read($priv_key, $p12cert, "abcdefg");

//try using the phpseclib library from http://phpseclib.sourceforge.net
//req() will get all the $_POST, $_GET data req("signature") = $_GET["signature"];
//$hash_request is an base64_encode string same exact value as the one in C#
$rsa = new Crypt_RSA();
$rsa->setHash("sha1");
$rsa->setSignatureMode(CRYPT_RSA_SIGNATURE_PKCS1);
$rsa->loadKey($p12cert["cert"]);
$verified_phpseclib = $rsa->verify($hash_request, base64_decode(req("signature"))) ? "verified" : "diff";

//try using php built in openssl 
$verified_builtin = openssl_verify($hash_request, base64_decode(req('signature')), $p12cert["cert");

非结果返回“已验证”或“1”

可能是因为当C#对数据进行签名时,数据的格式是byte []吗? 还是有其他原因吗?

我在linux中运行所有代码 php - 使用apache2 c# - 使用monodevelop


以下是我使用openssl_pkcs12_read读取后的pk12数组,您可以下载它here

Array
(
    [cert] => -----BEGIN CERTIFICATE-----
MIIDXTCCAkWgAwIBAgIJANjuyBraeAN0MA0GCSqGSIb3DQEBBQUAMEUxCzAJBgNV
BAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRlcm5ldCBX
aWRnaXRzIFB0eSBMdGQwHhcNMTMwMzE5MDA0ODI0WhcNMTMwNDE4MDA0ODI0WjBF
MQswCQYDVQQGEwJBVTETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50
ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIB
CgKCAQEAojE1Md/DET9mvKpoNJvb+mbGdTfSYuXQurGxb5pU0ba+gVeARbc8fjaK
D/2EKhUP/7240a31e5qI/2UlgiHO+ghQKDPo+5W5CetDVR1oMbWVA1fp7/gv8DEV
AvNV9nVkYzxnoVPs+W0MhMlWPrhCNSqQ28BbbL0EzXwXESnMw0I2VAhGWDNG0zSl
U03l5zh8jzzRKKK+gw4gIKo0u4lEUqB52MaXPzdwQkOEXRUdT+l1vK3DGfQycMrm
gZEN6IXDvpSFcQfHJlICK06+JNgDM0tYbif2mMfDEPKeFIjGC8S2ZDTZlR92LCk1
/W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C
/fNqptnuA7DKPDKkQIeQSwtHF9HGVoqG0JGOmYCeCZxQK6eZhiuU8VohAAbzsdu8
PgxyS/PRbCsXP4p+OjR1i+3C
-----END PRIVATE KEY-----
)

1 个答案:

答案 0 :(得分:1)

试试这个:

<?php
set_include_path(get_include_path() . PATH_SEPARATOR ."lib");
include('File/X509.php');

$p12cert = array();
$fp=fopen("cert/netclient.p12","r");
$priv_key=fread($fp,8192);
fclose($fp);
openssl_pkcs12_read($priv_key, $p12cert, "abcdefg");

$x509 = new File_X509();
$x509->loadX509($p12cert["cert"]);
$pubkey = $x509->getPublicKey();
$pubkey->setSignatureMode(CRYPT_RSA_SIGNATURE_PKCS1);
$verified_phpseclib = $pubkey->verify($hash_request, base64_decode(req("signature"))) ? "verified" : "diff";