php openssl 公钥加密 私钥解密 以及 私钥加密 公钥解密

$public_key = '-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDIIqfidJQ4FrA9eMT6X5dVfoYI
HM7mOZkBG11f4lN7Z/RbAKIRFoMPQIeGkgJwUZNoZiTm2hmXQhPJz0urPIhz+Rq8
5OeodnGu8upDiFIZNzB4Do25E3fjdGFoWPNSg7RVex6EMcC85ToHJwB+qGr8Uxs7
m7FPTn2aVonQfjnVLQIDAQAB
-----END PUBLIC KEY-----';



$private_key = '-----BEGIN PRIVATE KEY-----
MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAMgip+J0lDgWsD14
xPpfl1V+hggczuY5mQEbXV/iU3tn9FsAohEWgw9Ah4aSAnBRk2hmJObaGZdCE8nP
S6s8iHP5Grzk56h2ca7y6kOIUhk3MHgOjbkTd+N0YWhY81KDtFV7HoQxwLzlOgcn
AH6oavxTGzubsU9OfZpWidB+OdUtAgMBAAECgYAev5xVoZ2oJGw9dZGl+gvROdgz
5OEw5AaK8f5dnfM/kZAsbyN3RqMqM7rexQ4U3aOtNYxT34xw/dj2d5/jljBMg4Ab
kk8BuEM16+9egsZqMCP4ZW/TecC9c8PcVqRez/FBbLm8g6C4dhiaA+Ch7g72C+wQ
qzBU9NYKc8tt46UcjQJBAO8KO9qViOAOIXwf3BL2J6P49swGJzhXva3UcpuJm60w
Ps8pMlO5N20JJnBFqY2/Zq3im88wJaSCDKVVb3H8Df8CQQDWVcjAoC0UwDHujvM9
Mrs6iCnXAN9i3KPeXZDeKs1hsDdeQbbrs6s6yftSNcPfyirqgBPhh9iBJp2JMHjE
XbTTAkAaBjQz/Pm04vT0TYepLNFuAank1nqooKzO4wVDN70SIuGDrkmDYfPN1CQy
LvNhskfcgIJUETrX6Sy1R1LlxvpDAkEAtaWiK84UJr/0EU/JSZZTIvn/K54rs+d2
YaA+hGsWALKNWi6rTfrJJVvEyRmT1XfLA05kiwDMNjh5zqjr0M/xzQJBAIif6btY
fempV6+kLMt4gVkjD1PnKkllV28SvQcKlMK6aO9lamlA8cFVjTfW1NyTcY4BNHKo
YpsCX3IDnFzmLYE=
-----END PRIVATE KEY-----';


$puKey = openssl_pkey_get_public($public_key);
print_r($puKey);

$piKey = openssl_pkey_get_private($private_key);
print_r($piKey);

$data = json_encode(["name" => "lilei", "sex"=>"man"]);

openssl_public_encrypt($data,$encrypted,$puKey);//公钥加密
$encrypted = base64_encode($encrypted);
echo "公钥加密" . $encrypted,"\n";

openssl_private_decrypt(base64_decode($encrypted),$decrypted,$piKey);//私钥解密
echo "私钥解密" . $decrypted,"\n";

openssl_private_encrypt($data,$encrypted,$piKey);//私钥加密
$encrypted = base64_encode($encrypted);
echo "私钥加密" . $encrypted,"\n";

openssl_public_decrypt(base64_decode($encrypted),$decrypted,$puKey);
echo "公钥解密" . $decrypted,"\n";

 

你可能感兴趣的:(php,加密,php)