在进行数据请求的时候,为了防止数据在传输的过程中被截取或篡改,可以使用签名认证,而RSA2就可以胜任这个工作。原理就是数据发送端根据发送的数据使用RSA2加密,然后将加密后的字符串发送给接受端,接受端解密验证即可!

注意,RSA2进行数据签名时应在数据发送方完成,并使用RSA私钥进行签名,在接受数据端使用公钥进行验签即可!

PHP RSA2获取签名的方法

php示例:

//私钥
$privateKey = '-----BEGIN PRIVATE KEY-----
MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCa8MILC7Pa2YFu
cEXVWWZCvDDfHI6lmZGOxEu4akBxG4fsDmEOJL1OyXGhV4XzJkcgWzE8QQVi+UTf
0XGsF3pP9SNKwSgrkQlbL1hhvJxYDXeABMLDSOgkcYpLZ3+wBVwJ7C9V7i86LDIs
MwpqXBXhMR8E/h1jJK8c8yawIsZRVfHnKAlOfR7NrSZyF2dmxUOg7WocY/UWP/z1
4JU62UwTiYwkP64JcW2Tivx+cglhGbgPfhYtrZsj/XUHTz0VRydV7zdTOIQ0c9Sq
LxSqJskdAaSr3xmcda/hWg0rKeLHNqdsQTwK77OZteWJptzmoAwHQ33vi4YI2Peh
JrddPJKJAgMBAAECggEADwsPG/lv5fC2Q129OgCrEBGrgQ4Jv4RQgJcIbwhJ53Fv
2fhvbesdQjgGw053H+t4fmi8OAhsGHbxO/Xe3oGNC5RWb9yfJNO5H9RR9XNVjqsI
DjLoIgPDHj6z2axb1mA+1mvLEMHGbhR6bpYqDbeRHEMjqt1tRwH5yorFF9bYgG78
FYGrGgBQRA1lY9Pg8s7HxzvNeNjMjkblarMWKZulmLQWVzP/PFLhhxOt7M6UrvDp
iem+/+XOd0gjLXUsMx0A1umga9DGqPdW+vQ4Ot3OiYAzV7jVpXoUvs6sElAxFbhH
AV3JSATKZCrdUcZBF7QqiFGyHRyxpwk5v8rEBzBn8QKBgQDL2ewDea7/4fp3xH1x
Fl3l0R7hQcrrCkNAnAQyjutaLp350InL4CznmDfY+3q8j29b0S2o8yfz2xOCL1KR
6TBGtjuLwM67PvzZNlZeipQUETZ/h2kE/pF2BOeUK2spH9Bq02qcxBwhQiRNFmu+
174EkTGHl7CxXIJ38iaifr5D9QKBgQDCk7OmabB1Z/MoStHQYym/LI9NAHxq7IHW
ZdR3FPG38ffhiIW1Fnlvabb0qUmOVj+GAwpDuQ+3k1HkIoYd8b5ytjadJp9GdGYL
5VWqnQROzXvaWmfxrq3xfLeOkHN5Z/Af9zt/3OQhiBQ2DkOm7l1VIX34KVA9KwkE
+TVv6tPLxQKBgF4RJ6nK4whNHSlbUI94xMeiIl/L3nlrePE9jWpPLSemT1vR8WRV
BCOyyEKmeVK91aNG+Yz29769n1j5wV53fsGCoiOxSzR4snEBdAWYaRCBiOZ8c+fM
GQ+e38Yt6vtFJQXGnXHNxTVLsjioWfvszlCJN5PcWbred44l/dIKLn5pAoGALsFX
NfHHmS61W0U/TZfGfE200YgAXaEfQemOOF8sBkt6iRNJLc6Mt5zrfbcAGdfXw9gP
K17Yy//2eZ9iIufoH6BxVynktAVcUBd3klvQoS73R+zZdVXILnMNmJBVbyLurH+4
KG1n1DbixGqBDyfP/ZjisDMdONWSgUNw98exxRECgYBiClGPWa4OQAl7FGD7vfp8
0Jz0JKf9DKimXO6i8HFnapO6Fko0nVn2MjaPeF93smV3Kosx+pZuqOzK5aEE7rLC
7Qpc9qoiVMD2SJJvZAuL1kKOHyBbZs8fpaHJcPN3aIIjkDphgaeYokJQ5qixB39u
PNOOMZ+rikAEshVb201T5A==
-----END PRIVATE KEY-----';
//下面是要加密的数据
$data = array(
    'uid' => '10',
    'name' => 'mochu',
    'host' => 'feiniaomy.com',
);
$privateKey = openssl_pkey_get_private($privateKey);
$sign_str = openssl_sign(json_encode($data), $sign, $privateKey, OPENSSL_ALGO_SHA256) ? base64_encode($sign) : null;
//输入签名
echo $sign_str;

输出签名:

WzCuHwFZo5qu9AyiReoq2eoLbbHcBDjrcJt1i0wG3LycWSU5TXThj9mnzYi5FSLUrPDjgcALn0mPpx5eU9LwCDnVyGYC6WGhhAGndeZtdmBbzReTf2UdqT9Eqro8ccfuYU3M07Z4MDezxl7x4l96w8PuMBLgs9NYaXf9p1ak4gQU/8Gl+coLXB9f3JjGuaswnSnzMS69I8Tpxlk3i/g9Ai2UvKQf7reYdaP8mv3sijXCIe0NazqVJSxXwvBWA9EsCFnohHzvaudBdYbliVb4wWe5aJFioNvBQVqxXbgb2woQiK4uLLjNA/wM4KuTNtiHBBY9WkSJKumwPG5g0kUNyQ==

PHP RSA2验证签名的方法

php示例代码

//上面加密后的数据
$sign_str='WzCuHwFZo5qu9AyiReoq2eoLbbHcBDjrcJt1i0wG3LycWSU5TXThj9mnzYi5FSLUrPDjgcALn0mPpx5eU9LwCDnVyGYC6WGhhAGndeZtdmBbzReTf2UdqT9Eqro8ccfuYU3M07Z4MDezxl7x4l96w8PuMBLgs9NYaXf9p1ak4gQU/8Gl+coLXB9f3JjGuaswnSnzMS69I8Tpxlk3i/g9Ai2UvKQf7reYdaP8mv3sijXCIe0NazqVJSxXwvBWA9EsCFnohHzvaudBdYbliVb4wWe5aJFioNvBQVqxXbgb2woQiK4uLLjNA/wM4KuTNtiHBBY9WkSJKumwPG5g0kUNyQ==';
//公钥
$publicKey = '-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAmvDCCwuz2tmBbnBF1Vlm
Qrww3xyOpZmRjsRLuGpAcRuH7A5hDiS9TslxoVeF8yZHIFsxPEEFYvlE39FxrBd6
T/UjSsEoK5EJWy9YYbycWA13gATCw0joJHGKS2d/sAVcCewvVe4vOiwyLDMKalwV
4TEfBP4dYySvHPMmsCLGUVXx5ygJTn0eza0mchdnZsVDoO1qHGP1Fj/89eCVOtlM
E4mMJD+uCXFtk4r8fnIJYRm4D34WLa2bI/11B089FUcnVe83UziENHPUqi8UqibJ
HQGkq98ZnHWv4VoNKynixzanbEE8Cu+zmbXliabc5qAMB0N974uGCNj3oSa3XTyS
iQIDAQAB
-----END PUBLIC KEY-----';
$data = array(
    'uid' => '10',
    'name' => 'mochu',
    'host' => 'feiniaomy.com',
);
$publicKey = openssl_pkey_get_public($publicKey);
$rs = openssl_verify(json_encode($data), base64_decode($sign_str), $publicKey, OPENSSL_ALGO_SHA256);
if ($rs) {
    echo '验签成功';
} else {
    echo '验签失败';
}