使用方法:
加密
点击复制代码 PHP
$txt="78moban";
$pubkey="公钥"
$macdata = RSAEncrypt::encrypt($txt, $pubkey);
解密
点击复制代码 PHP
$txt="78moban";
$pubkey="私钥"
$macdata = RSAEncrypt::decrypt($txt, $pubkey);
类文件
点击复制代码 PHP
class RSAEncrypt {
public static function encrypt($str, $publicKey) {
$pubKey = openssl_pkey_get_public($publicKey);
openssl_public_encrypt($str, $outStr, $pubKey);
return base64_encode($outStr);
}
public static function decrypt($str, $privateKey) {
$inputByte = base64_decode($str);
$decoded = base64_decode($privateKey);
$priKey = openssl_pkey_get_private($decoded);
openssl_private_decrypt($inputByte, $outStr, $priKey);
return $outStr;
}
}