支付宝RSA签名验签工具签名格式化

最近在做支付宝的支付接入,介于生产的统一支付服务器使用的事PHP5.3的版本。使用的支付宝支付sdk也是以前的比较老旧的,看了一遍蚂蚁金融的开发文档,尝试了很多次,都是因为应用公钥和秘钥格式的问题无法成功调起支付。

后来看了下前辈们的写法,都是把RSA的签名证书存成一个文件然后用sdk调用的,目前又不想重写前辈们的方法。毕竟涉及了太多的业务逻辑,所以也只能是照葫芦画瓜了。用支付宝官方的RSA生成的工具生成的公钥和秘钥都是适用新版支付宝服务端sdk用到旧版的sdk的话显然是不适用的。

支付宝工具生成的公钥

所以我需要把密钥和公钥格式化成pem证书的格式,就是有头有尾64个字符回车换一行,下面试代码;

<?php
$publicKey = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAlupbM1oYty7eUKve9qkIGG3PsG0FtChg41iW7lrJhaA78BCWaXwoarrWgNQGckUASFEgQSpijsk+AB9A4AzGbVW2hOzrfc2rL5cOVsgjjkPhk6GveHZ+AoqOfoF06e9lVgEzdA0fuktgchrhEK9FZ2z0nrCJSwvRPvyyiwSmTdD8sim/j0VkyzaTQhrC98gYtvUrAf/Sb4t1uoU6sPjRzhFvVK/XGBsiIOBE+yGU0zIJLyukS02QbwaGkUQH9tth+xWByy2oTJ+sSMRb4oCnbPFiSgZ6FHAZ0OgGQpkDesj4OCV2TaXSHZlHFwLhJMdPWPnKq8o9YDVu4y4Xi3FD2QIDAQAB';
$privateKey = 'MIIEogIBAAKCAQEAsdmwf3vidizLm+BRLR2VT9kYr783JQAHo6EJ1YhALvxF0JpaPxS+1gv2ntktapuuaNVfK4zMg2+Tv6h/Xojn61jC6UnbaI9FIjLndApR318PIvosJE01yFRiiLdHdE6/H3P97CVeTdNwLLHCeQxcVGLJq94hefsvv8kiRJLbT/EvIrMF9zT/bDqMPQtdPufhy46IVQSI9YOKR5qtisNDyOjhNNm7r35VLvHcBC+woBN/o+Mk2g0yxTizmFb2X7q8abO7CwId+5Nka/Fe9e702F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n8wEckvZQweXrFaU5FbI6IuNNcaCdr8I0z91XJ3APodJiuHdyu3fbTTECNAoGANsV/jgsjVieYmdupe9EHjtm/+wYAKn/VT0bhrJjaN4gbBMOMDvYSC8G4cCo8vfMwo3bELRfDRaFL1e/4zVZM8AH1vour5DLP5/1WKtGbYicLryZ9heaHwVCgTsY6vgd0b7/OxGrNVvGqOvnr2x/2mREGhpfjUwCBp4vazvrs+uA=';

$pubPem = chunk_split($publicKey,64,"\n");
$pubPem = "-----BEGIN PUBLIC KEY-----\n" . $pubPem . "-----END PUBLIC KEY-----\n";
print_r($pubPem);
// 验证公钥格式是否正确
$pu_key = openssl_pkey_get_public($pubPem);
if (!$pu_key) die('$pu_key 格式不正确');

$priPem = chunk_split($privateKey,64,"\n");
$priPem = "-----BEGIN PRIVATE KEY-----\n" . $priPem . "-----END PRIVATE KEY-----\n";
Comments: 1

「人生在世,留句话给我吧」

提交评论