Sign 驗籤數據準備:php
php的openssl擴展裏已經封裝好了驗籤的方法openssl_verify。函數
若是在Windows下的php.ini須要開啓Openssl模塊:加密
extension=php_openssl.dll3d
這裏再解釋一下openssl_verify函數:code
int openssl_verify ( string $data , string $signature , mixed $pub_key_id [, int $signature_alg = OPENSSL_ALGO_SHA1 ] )orm
openssl_verify() 函數來驗證sign簽名是否正確,是經過指定的data數據和公鑰相關的pub_key_id來實現的。blog
從支付接口獲取到的公鑰Public key,以下:接口
—–BEGIN PUBLIC KEY—–
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxJUb3d2QTkW67SILT33Y
GqmP8HmXqtXpM5DPKFibkMs2f6tk+52qe3aOb4/bryqG754o5s1Yg/9o9duwE3VL
If9tuFVQ6TSrN2cacjDNXsW3aIkk6HdBvytrj/XJePQIXmjZZjPGqbDdItmJxUI+
HXYDxiaZIe4kuVRA8FbKr6iZ5uW9bfO5N2GY+IncPFFZivDa2Xq8kuOwxyZXi+cX
4W9LnsYrkq3jZ5/9RGNGzXJMs+GkFUKFqaHWhqW5xAwsKQcD8xiLuwAHt6wGIfy1
R2yQQQwetK02i44MvKRCEX8tlUYvCvr0T38B3kX2hHgmIxP3Cpv5jbJts+mMD7cG
iQIDAQAB
—–END PUBLIC KEY—–ip
注 意這個是2048位的公鑰應該是9行或者10行,不能爲1行,否則PHP的openssl_pkey_get_public沒法讀 取,pub_key_id的結果爲false,我就在這個上吃了大虧。若是沒有—–BEGIN PUBLIC KEY—– 和 —–END PUBLIC KEY—– 能夠本身加上,最後保存到一個rsa_public_key.pem文件中。像這樣:ssl
<?php $public_key = file_get_contents(dirname(__FILE__).’/rsa_public_key.pem’); $pkeyid = openssl_pkey_get_public($public_key); $data = ‘abc’; $sign = ‘WkMaSsx9Fbj9/YyjoM1X0SLYvaFbsz9VmMaxc42fXxamEEIj5AfqQLrygEZRq0gkLNT4heIwOiSWEAWbfD4imaERKk07ANXEtZJ9jPJvyvg70IVvaYMKAr7bX0dJXmYw4aHnkcWR1kz27Drr6fxPmchB9WCsRmi4VfhVoF1+HRFOp28nIVReGRcbwbW1/bcMisXbitirz9Wq396vY88GUSgbgNdhFXX/kzjRBTjnG+CIhXq4HPdOWovqtPhQoxmK55+V+vxNZk9OPPHHaN3vVswk062NOs2/05yNVObL+PWeg/m43buXYalmkrwEhemdGfjIdNEoSO2D4gikvm43cg==’; $sign = base64_decode($sign); if ($pkeyid) { $verify = openssl_verify($data, $sign, $pkeyid, OPENSSL_ALGO_MD5); openssl_free_key($pkeyid); } var_dump($verify); ?>