java的加签用php如何实现

1. 问题描述?

 对接一个甘肃农信的支付通道,他们的demo是用java写的,我们是php. 比对着java试写着php的加签demo,结果验签失败.

2. 代码块

java:
public static String signPaygateData(String plain) throws Exception {
    String signatureAlgorithm = "MD5withRSA";
    String keyAlias="ddbank";
    KeyReader keyReader = new KeyReader();
    //使用pfx加签
    PrivateKey privateKey = keyReader.readPrivateKeyfromPKCS12StoredFile(certPath, certPassword);
    byte[] signature = null;
System.out.println("privateKey=========="+privateKey);
    try {
    signature = sign(privateKey, signatureAlgorithm, plain.getBytes("UTF-8"));
    } catch (Exception e) {
        throw new Exception(e);
    }
    return Hex2Byte.byte2Hex(signature);
}

java的加签用php如何实现

java的加签用php如何实现

PHP:
# 获取私钥
$cert = file_get_contents(realpath('./ddbank_test.pfx'));
openssl_pkcs12_read($cert, $cert_info, '123456');
$private_key = $cert_info['pkey']; //私钥

# 签名数据
$sign_data = '123456';
$priKey = openssl_get_privatekey($private_key);
if ($priKey === false) {
    throw new \Exception('证书文件路径错误或者传入的证书格式错误');
}
# 加签
openssl_sign(utf8_encode($sign_data), $signs, $priKey, OPENSSL_ALGO_MD5);
return bin2hex($signs);

3. 您期望得到的结果?

我想知道问题导致俩边加签数据不一致的原因可能有哪些引起的,我对java不了解.
私钥
MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAKTLTFQIJnUGk+XFRwu6t+hPh60T/NZQVVAwWKOjhwz1EuhWSMVJiHFya7rVsXxhmmIz+RzBDO5Ce4xi0+4lRIPvGPa2mUNG8fcPjCtXC8BQyicfvAWCqiyKpPekN7grczt3HYV2/Y+DhXKXuulVkNqBn5Bxf8xLuw9kpt6++/hfAgMBAAECgYAVEGtkPlp0EPAsqYfIRwik6C2GgQyLdF9DTbq2x/tGO7k7+3N3zEtlfOOqURBLvsoWDWClPUPi3JqgOi2uvhLAub/ooolTbfqWOv/C/3FI0BLatMgkuQg+3jtKhFdveblrLJLtZb80CLkJjCdyR4/yJOkkGUiT5EA2uzLBstnO+QJBAOEJnq9CKybIvymkr2bRxoIOkbIyVg4AHbqfmb/aH6Q/NAsGBmP5Yo+WveHMkt6W2XywBg8xkTIZmQyzv1ho31sCQQC7d8FTCcdgjNZNcPka+Jm3ZSrI+kCj6SrCoYE7ARjAoOLnRodlgnSmCH17ryBvRMMrpXaReCdVR8UFyjTGJn5NAkBJMZvPXTIz1y92F/6E7s4mFx8EWqLqwtbokGitogCQp44B19/UPJT2Jw1zl9pdU6tBhu8xmemj5feFziD6XIHpAkAatVgUtYrzRX1usQ68efikeURcN9MnxxtxeUMJHgHhx8or+w9lNe3ajdEa4vty/s/zkvJOjVjFPKFkTsewB2HtAkEAxfxQuKBR6HYKDqDQHqXdZZtf2J77G65Jt5LSnuUfTh2tlpsYo4ur2+qeUuc4FjS99/gf2FXT7PFNu7MmsTPQ/A==
真实加签内容:
$sign_data = "<Message><TransId>TJSK</TransId><MerchantId>95162014214P003</MerchantId><OrderId>220422154103083890517</OrderId><Subject>解忧杂货铺    收款0.01元</Subject><MerTransDateTime>20220422154103</MerTransDateTime><MerTransDate>20220422</MerTransDate><TransAmount>0.01</TransAmount><termId>5006A01V</termId><MerchantIP>100.120.72.108</MerchantIP><qrCode>133664342880547822</qrCode><MerchantUrl>https://www.weiwoju.com/PayNotify/gunx</MerchantUrl><areaInfo>1568200</areaInfo><consignee>CDS</consignee><SignType>RSA</SignType></Message>";

    java:1fb08bba187d6e41950eac7ca9d93ae4c18de1f974ab022884e88455e5a0dda22f19a6236f17272ffe46870783ccae310756a74b09423d85c3843dc1f6e2df0768fdcac480099222c1c85609f568ed5df4fddd7b4fd17b734c1a841be22dae0ad86b695b4b1eaa6cc74d68ee5ccd63fb71dab3fe3c5fbdd5efdb091cd4cce2d3

php:4a2fcfc9567b9c251bdbc4e6016fce7846990e025a57f845236a25c43516e32b65efb638902a28d5843f9561b2438f8a7872416a931529c22e995a53edbcd3d6d4324ca69389fda382ac70536e964b2841b98c261b189fbcc6106919ee4291d74aa8a505cdacbabeab8016393eb98345d316671bda5935ba9b9e8de65e50c0b3


感谢大家,问题已解决
原因:俩边的证书不同(我真无语,还特地问了,他们说就用demo的证书就行,结果商户和证书不匹配,搞了俩天多,感谢大家的回应).
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
抄你码科技有限公司
最佳答案

用java+你贴私钥+你提供的$sign_data跑了下输出是这个

package com.jiangslee;

import org.apache.commons.codec.binary.Hex;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.Signature;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.*;
import java.nio.charset.StandardCharsets;

class Demo {
    public static void main(String[] args){
        String signData = "<Message><TransId>TJSK</TransId><MerchantId>95162014214P003</MerchantId><OrderId>220422154103083890517</OrderId><Subject>解忧杂货铺    收款0.01元</Subject><MerTransDateTime>20220422154103</MerTransDateTime><MerTransDate>20220422</MerTransDate><TransAmount>0.01</TransAmount><termId>5006A01V</termId><MerchantIP>100.120.72.108</MerchantIP><qrCode>133664342880547822</qrCode><MerchantUrl>https://www.weiwoju.com/PayNotify/gunx</MerchantUrl><areaInfo>1568200</areaInfo><consignee>CDS</consignee><SignType>RSA</SignType></Message>";

        try {
            System.out.println(signPaygateData(signData));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String signPaygateData(String plain) throws Exception {
        String signatureAlgorithm = "MD5withRSA";
        byte[] signature = null;
        String privateString = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAKTLTFQIJnUGk+XFRwu6t+hPh60T/NZQVVAwWKOjhwz1EuhWSMVJiHFya7rVsXxhmmIz+RzBDO5Ce4xi0+4lRIPvGPa2mUNG8fcPjCtXC8BQyicfvAWCqiyKpPekN7grczt3HYV2/Y+DhXKXuulVkNqBn5Bxf8xLuw9kpt6++/hfAgMBAAECgYAVEGtkPlp0EPAsqYfIRwik6C2GgQyLdF9DTbq2x/tGO7k7+3N3zEtlfOOqURBLvsoWDWClPUPi3JqgOi2uvhLAub/ooolTbfqWOv/C/3FI0BLatMgkuQg+3jtKhFdveblrLJLtZb80CLkJjCdyR4/yJOkkGUiT5EA2uzLBstnO+QJBAOEJnq9CKybIvymkr2bRxoIOkbIyVg4AHbqfmb/aH6Q/NAsGBmP5Yo+WveHMkt6W2XywBg8xkTIZmQyzv1ho31sCQQC7d8FTCcdgjNZNcPka+Jm3ZSrI+kCj6SrCoYE7ARjAoOLnRodlgnSmCH17ryBvRMMrpXaReCdVR8UFyjTGJn5NAkBJMZvPXTIz1y92F/6E7s4mFx8EWqLqwtbokGitogCQp44B19/UPJT2Jw1zl9pdU6tBhu8xmemj5feFziD6XIHpAkAatVgUtYrzRX1usQ68efikeURcN9MnxxtxeUMJHgHhx8or+w9lNe3ajdEa4vty/s/zkvJOjVjFPKFkTsewB2HtAkEAxfxQuKBR6HYKDqDQHqXdZZtf2J77G65Jt5LSnuUfTh2tlpsYo4ur2+qeUuc4FjS99/gf2FXT7PFNu7MmsTPQ/A==";
        PrivateKey privateKey = generatePrivateKey(privateString);
        signature = sign(privateKey, signatureAlgorithm, plain.getBytes(StandardCharsets.UTF_8));
        System.out.println(signature);
        return Hex.encodeHexString(signature);
    }

    public static byte[] sign(PrivateKey paramPrivateKey, String paramString, byte[] paramArrayOfByte) throws Exception {
        Signature localSignature = Signature.getInstance(paramString);
        localSignature.initSign(paramPrivateKey);
        localSignature.update(paramArrayOfByte);
        return localSignature.sign();
    }

    public static PrivateKey generatePrivateKey(String privateKey) throws Exception {

        byte[] encodedPv = Base64.getDecoder().decode(privateKey);

        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(encodedPv);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        return keyFactory.generatePrivate(keySpec);
    }
}

结果:

6dc9cbdc903128c9f083061bf8f2ee0a5f1d324cafef71d8d8816480d5565a482040fac0db258885e0b79c88c43a49eb3fb8ed2f27575bc31f4256110309eaa53ecab595ce442eca54ee3c415c81befd41eb9727c8371cad8cd7114ce0dade677c0e96bfabec9adf5759b63971c8e90ab6d811accbd845962962eef003584444

与你的也不一致:

java:1fb08bba187d6e41950eac7ca9d93ae4c18de1f974ab022884e88455e5a0dda22f19a6236f17272ffe46870783ccae310756a74b09423d85c3843dc1f6e2df0768fdcac480099222c1c85609f568ed5df4fddd7b4fd17b734c1a841be22dae0ad86b695b4b1eaa6cc74d68ee5ccd63fb71dab3fe3c5fbdd5efdb091cd4cce2d3

用123456与PHP进行验证

$signData=”123456”;
java结果:

68b95f71b0eedd5fac4c9b7dbdd9232d567ce895696e2ceda9f793d35fc69e128610fc02fa518619cd7962711cc2a949ef5a8550e337ae1653b6ff115e07645a7acce2f9b106231a7991715aeca68555063e6d3bd5a82dc8ee0bcbb8766b294b5d7fc1993022b17c450a74ee26bce81aa4d4b9c4d2806ecf8f14f18fd08f1157 

php结果:

$private_key = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAKTLTFQIJnUGk+XFRwu6t+hPh60T/NZQVVAwWKOjhwz1EuhWSMVJiHFya7rVsXxhmmIz+RzBDO5Ce4xi0+4lRIPvGPa2mUNG8fcPjCtXC8BQyicfvAWCqiyKpPekN7grczt3HYV2/Y+DhXKXuulVkNqBn5Bxf8xLuw9kpt6++/hfAgMBAAECgYAVEGtkPlp0EPAsqYfIRwik6C2GgQyLdF9DTbq2x/tGO7k7+3N3zEtlfOOqURBLvsoWDWClPUPi3JqgOi2uvhLAub/ooolTbfqWOv/C/3FI0BLatMgkuQg+3jtKhFdveblrLJLtZb80CLkJjCdyR4/yJOkkGUiT5EA2uzLBstnO+QJBAOEJnq9CKybIvymkr2bRxoIOkbIyVg4AHbqfmb/aH6Q/NAsGBmP5Yo+WveHMkt6W2XywBg8xkTIZmQyzv1ho31sCQQC7d8FTCcdgjNZNcPka+Jm3ZSrI+kCj6SrCoYE7ARjAoOLnRodlgnSmCH17ryBvRMMrpXaReCdVR8UFyjTGJn5NAkBJMZvPXTIz1y92F/6E7s4mFx8EWqLqwtbokGitogCQp44B19/UPJT2Jw1zl9pdU6tBhu8xmemj5feFziD6XIHpAkAatVgUtYrzRX1usQ68efikeURcN9MnxxtxeUMJHgHhx8or+w9lNe3ajdEa4vty/s/zkvJOjVjFPKFkTsewB2HtAkEAxfxQuKBR6HYKDqDQHqXdZZtf2J77G65Jt5LSnuUfTh2tlpsYo4ur2+qeUuc4FjS99/gf2FXT7PFNu7MmsTPQ/A==";
$private_key = chunk_split($private_key, 64, "\n");
$private_key = "-----BEGIN RSA PRIVATE KEY-----\n{$private_key}-----END RSA PRIVATE KEY-----\n";
$priKey = \openssl_get_privatekey($private_key);
$data = '123456';
\openssl_sign(($data), $sign, $priKey, OPENSSL_ALGO_MD5);
echo bin2hex($sign);
68b95f71b0eedd5fac4c9b7dbdd9232d567ce895696e2ceda9f793d35fc69e128610fc02fa518619cd7962711cc2a949ef5a8550e337ae1653b6ff115e07645a7acce2f9b106231a7991715aeca68555063e6d3bd5a82dc8ee0bcbb8766b294b5d7fc1993022b17c450a74ee26bce81aa4d4b9c4d2806ecf8f14f18fd08f1157 
2个月前 评论
抄你码科技有限公司 (作者) 2个月前
renxiaotu 2个月前
renxiaotu 2个月前
deatil 2个月前
蒋蒋蒋蒋 (楼主) 2个月前
蒋蒋蒋蒋 (楼主) 2个月前
抄你码科技有限公司 (作者) 2个月前
讨论数量: 49

我感觉前几步应该没问题,大概率是最后一步引起的,但是我不知道用php该如何实现

2个月前 评论

把两个的签名结果贴出来看看

2个月前 评论
蒋蒋蒋蒋 (楼主) 2个月前
renxiaotu (作者) 2个月前
蒋蒋蒋蒋 (楼主) 2个月前
renxiaotu (作者) 2个月前
蒋蒋蒋蒋 (楼主) 2个月前
黑将军

去掉 bin2hex 试试,之前跟招行对接的时候也遇到相同的情况,后面我直接把签名返回就可以,没有再进行转换

2个月前 评论
蒋蒋蒋蒋 (楼主) 2个月前

utf8_encode 这个去掉下看看,文件为utf8的话,字符该说默认就是utf8

2个月前 评论
蒋蒋蒋蒋 (楼主) 2个月前
deatil (作者) 2个月前
小李世界 2个月前
deatil (作者) 2个月前
deatil (作者) 2个月前
蒋蒋蒋蒋 (楼主) 2个月前
蒋蒋蒋蒋 (楼主) 2个月前

不如把你得测试私钥发出来 试试

2个月前 评论
蒋蒋蒋蒋 (楼主) 2个月前
heroColin (作者) 2个月前
ruke

有些java里面的加密PHP是没有实行方案的,之前我们对接中国银行也是,着了一晚上都没找到解决办法,最后是通过php桥接java,在php里面调用java的包来解密

2个月前 评论
还不出来 2个月前
ruke

博客:PHP通往JAVA的桥接器使用

这篇文章里也介绍了怎么使用桥接

2个月前 评论

起码有一步是错的,最后的一步bin2hex的前提是bin,你的不是,你先要string转bin

https://blog.csdn.net/xonmao2008/article/details/103508054试试这个

2个月前 评论

可以一步一步的加print,然后对比哪里不一样。

2个月前 评论
蒋蒋蒋蒋 (楼主) 2个月前

刚好最近做的 已上线

    public function rsaPrivateSign(array $parameters) :string
    {
        $char = self::rasCharImplode($parameters);
        openssl_sign($char, $signature, $this->getPrivateKey(), OPENSSL_ALGO_SHA256);
        openssl_free_key($this->privateKeyId);
        return bin2hex($signature);
    }

getPrivateKey 是:

    private function getPrivateKey()
    {
        $private_key = chunk_split($this->config['private_key'], 64, "\n");
        $private_key = "-----BEGIN RSA PRIVATE KEY-----\n{$private_key}-----END RSA PRIVATE KEY-----\n";
        return $this->privateKeyId = openssl_get_privatekey($private_key);
    }
2个月前 评论
蒋蒋蒋蒋 (楼主) 2个月前
boolstone (作者) 2个月前
蒋蒋蒋蒋 (楼主) 2个月前
抄你码科技有限公司 2个月前
boolstone (作者) 2个月前
抄你码科技有限公司

用java+你贴私钥+你提供的$sign_data跑了下输出是这个

package com.jiangslee;

import org.apache.commons.codec.binary.Hex;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.Signature;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.*;
import java.nio.charset.StandardCharsets;

class Demo {
    public static void main(String[] args){
        String signData = "<Message><TransId>TJSK</TransId><MerchantId>95162014214P003</MerchantId><OrderId>220422154103083890517</OrderId><Subject>解忧杂货铺    收款0.01元</Subject><MerTransDateTime>20220422154103</MerTransDateTime><MerTransDate>20220422</MerTransDate><TransAmount>0.01</TransAmount><termId>5006A01V</termId><MerchantIP>100.120.72.108</MerchantIP><qrCode>133664342880547822</qrCode><MerchantUrl>https://www.weiwoju.com/PayNotify/gunx</MerchantUrl><areaInfo>1568200</areaInfo><consignee>CDS</consignee><SignType>RSA</SignType></Message>";

        try {
            System.out.println(signPaygateData(signData));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String signPaygateData(String plain) throws Exception {
        String signatureAlgorithm = "MD5withRSA";
        byte[] signature = null;
        String privateString = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAKTLTFQIJnUGk+XFRwu6t+hPh60T/NZQVVAwWKOjhwz1EuhWSMVJiHFya7rVsXxhmmIz+RzBDO5Ce4xi0+4lRIPvGPa2mUNG8fcPjCtXC8BQyicfvAWCqiyKpPekN7grczt3HYV2/Y+DhXKXuulVkNqBn5Bxf8xLuw9kpt6++/hfAgMBAAECgYAVEGtkPlp0EPAsqYfIRwik6C2GgQyLdF9DTbq2x/tGO7k7+3N3zEtlfOOqURBLvsoWDWClPUPi3JqgOi2uvhLAub/ooolTbfqWOv/C/3FI0BLatMgkuQg+3jtKhFdveblrLJLtZb80CLkJjCdyR4/yJOkkGUiT5EA2uzLBstnO+QJBAOEJnq9CKybIvymkr2bRxoIOkbIyVg4AHbqfmb/aH6Q/NAsGBmP5Yo+WveHMkt6W2XywBg8xkTIZmQyzv1ho31sCQQC7d8FTCcdgjNZNcPka+Jm3ZSrI+kCj6SrCoYE7ARjAoOLnRodlgnSmCH17ryBvRMMrpXaReCdVR8UFyjTGJn5NAkBJMZvPXTIz1y92F/6E7s4mFx8EWqLqwtbokGitogCQp44B19/UPJT2Jw1zl9pdU6tBhu8xmemj5feFziD6XIHpAkAatVgUtYrzRX1usQ68efikeURcN9MnxxtxeUMJHgHhx8or+w9lNe3ajdEa4vty/s/zkvJOjVjFPKFkTsewB2HtAkEAxfxQuKBR6HYKDqDQHqXdZZtf2J77G65Jt5LSnuUfTh2tlpsYo4ur2+qeUuc4FjS99/gf2FXT7PFNu7MmsTPQ/A==";
        PrivateKey privateKey = generatePrivateKey(privateString);
        signature = sign(privateKey, signatureAlgorithm, plain.getBytes(StandardCharsets.UTF_8));
        System.out.println(signature);
        return Hex.encodeHexString(signature);
    }

    public static byte[] sign(PrivateKey paramPrivateKey, String paramString, byte[] paramArrayOfByte) throws Exception {
        Signature localSignature = Signature.getInstance(paramString);
        localSignature.initSign(paramPrivateKey);
        localSignature.update(paramArrayOfByte);
        return localSignature.sign();
    }

    public static PrivateKey generatePrivateKey(String privateKey) throws Exception {

        byte[] encodedPv = Base64.getDecoder().decode(privateKey);

        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(encodedPv);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        return keyFactory.generatePrivate(keySpec);
    }
}

结果:

6dc9cbdc903128c9f083061bf8f2ee0a5f1d324cafef71d8d8816480d5565a482040fac0db258885e0b79c88c43a49eb3fb8ed2f27575bc31f4256110309eaa53ecab595ce442eca54ee3c415c81befd41eb9727c8371cad8cd7114ce0dade677c0e96bfabec9adf5759b63971c8e90ab6d811accbd845962962eef003584444

与你的也不一致:

java:1fb08bba187d6e41950eac7ca9d93ae4c18de1f974ab022884e88455e5a0dda22f19a6236f17272ffe46870783ccae310756a74b09423d85c3843dc1f6e2df0768fdcac480099222c1c85609f568ed5df4fddd7b4fd17b734c1a841be22dae0ad86b695b4b1eaa6cc74d68ee5ccd63fb71dab3fe3c5fbdd5efdb091cd4cce2d3

用123456与PHP进行验证

$signData=”123456”;
java结果:

68b95f71b0eedd5fac4c9b7dbdd9232d567ce895696e2ceda9f793d35fc69e128610fc02fa518619cd7962711cc2a949ef5a8550e337ae1653b6ff115e07645a7acce2f9b106231a7991715aeca68555063e6d3bd5a82dc8ee0bcbb8766b294b5d7fc1993022b17c450a74ee26bce81aa4d4b9c4d2806ecf8f14f18fd08f1157 

php结果:

$private_key = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAKTLTFQIJnUGk+XFRwu6t+hPh60T/NZQVVAwWKOjhwz1EuhWSMVJiHFya7rVsXxhmmIz+RzBDO5Ce4xi0+4lRIPvGPa2mUNG8fcPjCtXC8BQyicfvAWCqiyKpPekN7grczt3HYV2/Y+DhXKXuulVkNqBn5Bxf8xLuw9kpt6++/hfAgMBAAECgYAVEGtkPlp0EPAsqYfIRwik6C2GgQyLdF9DTbq2x/tGO7k7+3N3zEtlfOOqURBLvsoWDWClPUPi3JqgOi2uvhLAub/ooolTbfqWOv/C/3FI0BLatMgkuQg+3jtKhFdveblrLJLtZb80CLkJjCdyR4/yJOkkGUiT5EA2uzLBstnO+QJBAOEJnq9CKybIvymkr2bRxoIOkbIyVg4AHbqfmb/aH6Q/NAsGBmP5Yo+WveHMkt6W2XywBg8xkTIZmQyzv1ho31sCQQC7d8FTCcdgjNZNcPka+Jm3ZSrI+kCj6SrCoYE7ARjAoOLnRodlgnSmCH17ryBvRMMrpXaReCdVR8UFyjTGJn5NAkBJMZvPXTIz1y92F/6E7s4mFx8EWqLqwtbokGitogCQp44B19/UPJT2Jw1zl9pdU6tBhu8xmemj5feFziD6XIHpAkAatVgUtYrzRX1usQ68efikeURcN9MnxxtxeUMJHgHhx8or+w9lNe3ajdEa4vty/s/zkvJOjVjFPKFkTsewB2HtAkEAxfxQuKBR6HYKDqDQHqXdZZtf2J77G65Jt5LSnuUfTh2tlpsYo4ur2+qeUuc4FjS99/gf2FXT7PFNu7MmsTPQ/A==";
$private_key = chunk_split($private_key, 64, "\n");
$private_key = "-----BEGIN RSA PRIVATE KEY-----\n{$private_key}-----END RSA PRIVATE KEY-----\n";
$priKey = \openssl_get_privatekey($private_key);
$data = '123456';
\openssl_sign(($data), $sign, $priKey, OPENSSL_ALGO_MD5);
echo bin2hex($sign);
68b95f71b0eedd5fac4c9b7dbdd9232d567ce895696e2ceda9f793d35fc69e128610fc02fa518619cd7962711cc2a949ef5a8550e337ae1653b6ff115e07645a7acce2f9b106231a7991715aeca68555063e6d3bd5a82dc8ee0bcbb8766b294b5d7fc1993022b17c450a74ee26bce81aa4d4b9c4d2806ecf8f14f18fd08f1157 
2个月前 评论
抄你码科技有限公司 (作者) 2个月前
renxiaotu 2个月前
renxiaotu 2个月前
deatil 2个月前
蒋蒋蒋蒋 (楼主) 2个月前
蒋蒋蒋蒋 (楼主) 2个月前
抄你码科技有限公司 (作者) 2个月前

好巧,我最近也在对接一个银行的项目,要求php使用sm2国密加密,最终还是用了java实现

2个月前 评论
justmd5 2个月前

额,你这种情况我也遇到类似的情况。我之前对接的邮政的接口,对接的也是java,也是验签不过。后面发现对面的base64是调用的sun.misc.BASE64实现的,但是这个实现和php的base64实现是不一样的。后面就找了java的同事要了这个jar包,然后看这个包的源码,重写了个php版的。我觉得你这个情况和我上次遇到的很像,给你参考下 :joy:

2个月前 评论

参考一下我的部署 Java 环境的博客,搭建起来了自己把每一个处理的结果输出一遍就明白哪里不对了 博客:Windows10 Java Spring boot 开发环境搭建

2个月前 评论

去年搞了,php 测不通,转战java 接口进行加解密

2个月前 评论
蒋蒋蒋蒋 (楼主) 2个月前
Yoooooo 2个月前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!