class Rsa2{ private static $PRIVATE_KEY = 'rsa_private_key.pem 内容'; private static $PUBLIC_KEY = 'rsa_public_key.pem 内容'; /** * 获取私钥 * @return bool|resource */ private static function getPrivateKey() { $privKey = self::$PRIVATE_KEY; return openssl_pkey_get_private($privKey); } /** * 获取公钥 * @return bool|resource */ private static function getPublicKey() { $publicKey = self::$PUBLIC_KEY; return openssl_pkey_get_public($publicKey); } /** * 创建签名 * @param string $data 数据 * @return null|string */ public function createSign($data = '') { if (!is_string($data)) { return null; } return openssl_sign( $data, $sign, self::getPrivateKey(), OPENSSL_ALGO_SHA256 ) ? base64_encode($sign) : null; } /** * 验证签名 * @param string $data 数据 * @param string $sign 签名 * @return bool */ public function verifySign($data = '', $sign = '') { if (!is_string($sign) || !is_string($sign)) { return false; } return (bool)openssl_verify( $data, base64_decode($sign), self::getPublicKey(), OPENSSL_ALGO_SHA256 ); } } // PHP 调用 require_once "Rsa2.php";$rsa2 = new Rsa2();$data = 'my data'; //待签名字符串$strSign = $rsa2->createSign($data); //生成签名var_dump($strSign);$is_ok = $rsa2->verifySign($data, $sign); //验证签名var_dump($is_ok);