1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
<?php
class Crypt
{
private $privKey=null;
private $pubKey=null;
public function __construct()
{
$res = openssl_pkey_new(
array(
"digest_alg" => "sha512",
"private_key_bits" => 4096,
"private_key_type" => OPENSSL_KEYTYPE_RSA,
)
);
openssl_pkey_export($res, $privKey);
$this->privKey=$privKey;
$this->pubKey = openssl_pkey_get_details($res)["key"];
var_dump($this->privKey);
var_dump($this->pubKey);
}
public function crypt($data)
{
openssl_public_encrypt($data, $encrypted, $this->pubKey);
return base64_encode($encrypted);
}
public function decrypt($data)
{
openssl_private_decrypt(base64_decode($data), $decrypted, $this->privKey);
return $decrypted;
}
}
$crypt=new Crypt();
$a = 'This is a test';
var_dump($a);
$b=$crypt->crypt($a);
var_dump($b);
$c=$crypt->decrypt($b);
var_dump($c); |
Partager