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WwNfTi/VdAGLN0nxiHlX
4+czjNHA3pCSQ9hCzwIDAQAB
-----END PUBLIC KEY-----";
$rsa = new mycrypt($pub,$pri);
$encryptContent = $rsa -> encrypt(‘hankangkang‘);
echo $encryptContent.‘<br>‘;
$decryptContent = $rsa -> decrypt($encryptContent);
echo $decryptContent.‘<br>‘;
class mycrypt {
public $pubkey;
public $privkey;
function __construct($pub,$pri) {
$this->pubkey = $pub;
$this->privkey = $pri;
}
public function encrypt($data) {
if (openssl_public_encrypt($data, $encrypted, $this->pubkey))
$data = base64_encode($encrypted);
else
throw new Exception(‘Unable to encrypt data. Perhaps it is bigger than the key size?‘);
return $data;
}
public function decrypt($data) {
if (openssl_private_decrypt(base64_decode($data), $decrypted, $this->privkey))
$data = $decrypted;
else
$data = ‘‘;
return $data;
}
}