Use this for simple public/private key encryption.
<?php
define("OPEN_SSL_CONF_PATH", "/usr/share/ssl/openssl.cnf");
define("OPEN_SSL_CERT_DAYS_VALID", 365);
define("OPEN_SSL_PASSPHRASE", "lkdfjbjeyrasdfvkajwdeblsolkdkdjfbvzslalsmdbfvksb");
define("OPEN_SSL_PUBKEY_PATH", "/docs/domains/mywebsite.com/docs/key.pem"); class OpenSSL {
public $privatekey;
public $publickey;
public $csr;
public $crypttext;
public $ekey;
public function encrypt($plain) {
$publickey = openssl_get_publickey(is_file(OPEN_SSL_PUBKEY_PATH)? file_get_contents(OPEN_SSL_PUBKEY_PATH) : OPEN_SSL_PUBKEY_PATH);
openssl_seal($plain, $crypttext, $ekey, array($publickey));
openssl_free_key($publickey);
$this->crypttext = $crypttext;
$this->ekey = $ekey[0];
}
public function decrypt($crypt, $privatekey, $ekey="") {
$privatekey = openssl_get_privatekey((is_file($privatekey)? file_get_contents($privatekey) : $privatekey), OPEN_SSL_PASSPHRASE);
openssl_open($crypt, $plaintext, $ekey, $privatekey);
openssl_free_key($privatekey);
return $plaintext;
}
public function do_csr(
$countryName = "UK",
$stateOrProvinceName = "London",
$localityName = "Blah",
$organizationName = "Blah1",
$organizationalUnitName = "Blah2",
$commonName = "Joe Bloggs",
$emailAddress = "openssl@domain.com"
) {
$dn = array(
"countryName" => $countryName,
"stateOrProvinceName" => $stateOrProvinceName,
"localityName" => $localityName,
"organizationName" => $organizationName,
"organizationalUnitName" => $organizationalUnitName,
"commonName" => $commonName,
"emailAddress" => $emailAddress
);
$config = array(
"config" => OPEN_SSL_CONF_PATH
);
$privkey = openssl_pkey_new();
$csr = openssl_csr_new($dn, $privkey, $config);
$sscert = openssl_csr_sign($csr, null, $privkey, OPEN_SSL_CERT_DAYS_VALID, $config);
openssl_x509_export($sscert, $this->publickey);
openssl_pkey_export($privkey, $this->privatekey, OPEN_SSL_PASSPHRASE, $config);
openssl_csr_export($csr, $this->csr);
}
}
?>