1.RSA秘钥
接收小7请求时,使用当前游戏在小7开放平台所生成的公钥进行解密。私钥加密,公钥解密,超出解析长度,分段解析。
2. 数据签名加密
将请求报文体加密数据和RSA私钥使用openssl算法进行加密,并将加密结果进行base64编码,最终得到data的值
3. 签名验签
验签为签名的逆运算,首先将接收到的data数据进行base64解码,再使用RSA公钥进行openssl签名解析
4. PHP代码参考
<?php/** * 数据加解密PHP代码参考 */class Signature{ /** * 数据加密 * @param $payload * @param $rsaPrivateKey * @return string */ public static function encrypt($payload, $rsaPrivateKey) { $encrypted = ''; $rsaPrivateKey = self::formatRsaPrivateKey($rsaPrivateKey); $priId = openssl_get_privatekey($rsaPrivateKey); $keyLen = openssl_pkey_get_details($priId)['bits']; $partLen = $keyLen / 8 - 11; $parts = str_split($payload, $partLen); foreach ($parts as $part) { $encryptedTemp = ''; openssl_private_encrypt($part, $encryptedTemp, $rsaPrivateKey); $encrypted .= $encryptedTemp; } return base64_encode($encrypted); } /** * 数据解密 * @param $encrypt * @param $rsaPublicKey * @return string */ public static function decrypt($encrypt, $rsaPublicKey) { $str = str_replace(' ', '+', $encrypt); $decrypted = ""; $rsaPublicKey = self::formatRsaPublicKey($rsaPublicKey); $priId = openssl_get_publickey($rsaPublicKey); $keyLen = openssl_pkey_get_details($priId)['bits']; $partLen = $keyLen / 8; $base64Decoded = base64_decode($str); $parts = str_split($base64Decoded, $partLen); foreach ($parts as $part) { $decryptedTemp = ''; openssl_public_decrypt($part, $decryptedTemp, $rsaPublicKey); $decrypted .= $decryptedTemp; } return $decrypted; } /** * 格式化公钥 * @param string $publicKey * @return string */ public static function formatRsaPublicKey($publicKey) { return "-----BEGIN PUBLIC KEY-----\r\n" . wordwrap($publicKey, 64, "\r\n", TRUE) . "\r\n-----END PUBLIC KEY-----"; } /** * 格式化私钥 * @param string $privateKey * @return string */ public static function formatRsaPrivateKey($privateKey) { return "-----BEGIN RSA PRIVATE KEY-----\r\n" . wordwrap($privateKey, 64, "\r\n", TRUE) . "\r\n-----END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C6OArB0Id/u2cVSex/JW3arQt2jRk5JCSDtWn7k1cB9qPvckUbuQKBgA3nYSQtacIOyjuv5J+0oz/FIjGy2Npsf4TP2n0kLB5oIXd5mtq7fzXv18ki8HM1gz4sjNhdw0Pc1YK/8/QPgA5KerTanNTutqbTkAXX6jN2yXs+pB/cnX1RoZ2dFsXwTQl8NbRfGCD6/Mnd8og+oTaOnGlgCQQ2qeBkjakJZETpAoGBAIu/FAHHf8Y9T/SVJmexDRPDZ4JI/jDU4sZoEiTTlZ3lYc6ZwfL1118c+ggbd+46FlEvMNGkq1zmzplHP6k2lg7EKhmfOj1GG4yDB9FOmR8fhRCXbpKe+KhHPK+JcqkrXdiJ2VJOpIiaTBFoona3OwtE5LCMgx8RUqjZ+5ezXh9BAoGBAIz5//GXs1kJoGQatr0rvE4CvVLVbKHaVaXy7MAcp4uya3g1cZh3Swje8e4RD6UfiKmx+aPETfw9IkXbrPTYmXCNDx5bSbbPL+WMY8c7F7K0krOfl8Vtzg5cCfo45+IsLCM16sDT0MhLCPUMWj0kcd4bKcAc3CENaL3U03oLE58r';//公钥(测试密钥)$publicKey = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAw8UabiiHWEN5UR5SByS28CVCiVUlTkWRCEsP35SZs9a1wE7boirj0hpg7wyu6heydIpl94BewzwM3JHe45S/UYveYQo/zr3Jb46vpoJMQPikNyZSORxmjPlozIuAlYW56X3Aere+YkH1uCFeeeqW0BcT4Nvljn3vyJGYlzO3kWCuinZH0whrxStiyb5SJpjlgxz+MVUuBAJCDYARo6WCuCATTud+/97wspBCecs/ltvUQPaGpehChbC24U3VBmLsSCmxjAPLQJIukH6sy6tVoK5jmjNnFsCrWbkd68H6tsm9dGPcUMgNWxeahU/8tSOtX94AaTfU4cp846Yn7dH4JQIDAQAB';//小7发送给厂商的数据(测试数据)$testData = array( "collections" => array( array( "no" => 1, "si" => "2387337ba1e0b0249ba90f55b2ba2521", "bt" => 0, "ot" => 1617079205, "ct" => 0, "di" => "cf79ae6addba60ad018347359bd144d2",//此数据在ct为0时为非必填 "pi" => "1fffbjzos82bs9cnyj1dna7d6d29zg4esnh99u" ), array( "no" => 2, "si" => "2387337ba1e0b0249ba90f55b2ba2521", "bt" => 1, "ot" => 1617080905, "ct" => 0, "di" => "cf79ae6addba60ad018347359bd144d2",//此数据在ct为0时为非必填 "pi" => "1fffbjzos82bs9cnyj1dna7d6d29zg4esnh99u" ), ));//将发送数据转化成json格式$testData = json_encode($testData, 1);//小7进行数据加密(私钥加密)$encrypted = Signature::encrypt($testData, $privateKey);//厂商进行数据解密(公钥解密)$decrypted = Signature::decrypt($encrypted, $publicKey);echo "--------------------------------------------------------------", PHP_EOL;echo "原始数据:" . $testData, PHP_EOL;echo "--------------------------------------------------------------", PHP_EOL;echo "加密后的数据:" . $encrypted, PHP_EOL;echo "--------------------------------------------------------------", PHP_EOL;echo "解密后的数据:" . $decrypted, PHP_EOL;