php rsa
<?php $res=openssl_pkey_new(); // Get private key $ok = openssl_pkey_export($res, $privkey); // Get public key $pubkey=openssl_pkey_get_details($res); $pubkey=$pubkey["key"]; //var_dump($privkey, $pubkey); var_dump($ok); $data = '我用iphone手机!'; //可以用base64_decode用于url传输, 或者用 bin2hex($out); /* //公钥加密给私钥解密 openssl_public_encrypt($data, $encrypted, $pubkey); var_dump('公钥加密', $encrypted); //私钥解密 openssl_private_decrypt($encrypted, $decrypted, $privkey); var_dump($decrypted); //私钥加密,给公钥解密 openssl_private_encrypt($data, $encrypted, $privkey); var_dump('私钥加密', $encrypted); //给公钥解密 openssl_public_decrypt($encrypted, $decryptedPub, $pubkey); var_dump($decryptedPub); */ //效验方式 //$privkey = openssl_pkey_get_private($privkey); //如果是不信任的字符串,可以通过此函数验证,通过返回密钥 openssl_sign($data, $signature, $privkey, OPENSSL_ALGO_SHA256); //echo bin2hex($signature); //save for later //file_put_contents('private_key.pem', $private_key_pem); //file_put_contents('public_key.pem', $public_key_pem); //file_put_contents('signature.dat', $signature); //verify signature //hex2bin($signature); //$pubkey = openssl_pkey_get_public($pubkey); //如果是不信任的字符串,可以通过此函数验证,通过返回密钥 $r = openssl_verify($data, $signature, $pubkey, "sha256WithRSAEncryption"); var_dump($signature, $r);
如果公钥、私钥生成失败,则是openssl.cnf路径配置失败。