<?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路徑配置失敗。php