RSA加密

RSA加密

1. 生成私钥和公钥



# 私钥
openssl genrsa -out rsa_private_key.pem 1024

# 公钥
openssl rsa -in rsa_private_key.pem -pubout -out rsa_public_key.pem

2. 使用


$data = 'wogaslfgnladfghlanflgnaldnglanfhlnlabh';

$public_key = file_get_contents('rsa_public_key.pem');
$public = openssl_pkey_get_public($public_key);

$encode = '';

// 公钥加密
openssl_public_encrypt($data, $encode, $public);

$private_key = file_get_contents('rsa_private_key.pem');
$private = openssl_pkey_get_private($private_key);

$decode = '';

// 私钥解密
openssl_private_decrypt($encode, $decode, $private);

echo $decode;