List Info

Thread: note 77439 added to ref.openssl




note 77439 added to ref.openssl
user name
2007-08-29 16:16:52
Use this for simple public/private key encryption.

<?php

/**
 * Point to your config file
 *
 */
define("OPEN_SSL_CONF_PATH",
"/usr/share/ssl/openssl.cnf");
/**
 * Length of time certificate is valid (in days)
 *
 */
define("OPEN_SSL_CERT_DAYS_VALID", 365);
/**
 * Passphrase required with private key
 *
 */
define("OPEN_SSL_PASSPHRASE",
"lkdfjbjeyrasdfvkajwdeblsolkdkdjfbvzslalsmdbfvksb"
);
/**
 * Enter description here...
 *
 */
define("OPEN_SSL_PUBKEY_PATH",
"/docs/domains/mywebsite.com/docs/key.pem"); //
Public key path

/**
 * A wrapper class for a simple subset of the PHP OpenSSL
functions. Use for public key encryption jobs.
 * 
 * <code>
 * 
 * // To configure
 * // 1. Set OPEN_SSL_CONF_PATH to the path of your
openssl.cnf file.
 * // 2. Set OPEN_SSL_PASSPHRASE to any passphrase.
 * // 3. Use the OpenSSL::do_csr method to generate your
private and public keys (see next section).
 * // 4. Save the private key somewhere offline and save
your public key somewhere on this machine.
 * // 5. Set OPEN_SSL_PUBKEY_PATH to the public key's path.
 * 
 * // To generate keys
 * $ossl = new OpenSSL;
 * $ossl->do_csr();
 * $privatekey = $ossl->privatekey;
 * $publickey = $ossl->publickey;
 * unset($ossl);
 * 
 * // Encrypt
 * $text = "Secret text";
 * $ossl = new OpenSSL;
 * $ossl->encrypt($text);
 * $encrypted_text = $ossl->crypttext;
 * $ekey = $ossl->ekey;
 * unset($ossl);
 * 
 * // Decrypt
 * $ossl = new OpenSSL;
 * $decrypted_text = $ossl->decrypt($encrypted_text,
$privatekey, $ekey);
 * unset($ossl);
 * 
 * author Matt Alexander (mattalexxgmail.com) [based on code by Alex Poole (phpwwwcrm.com)]
 * copyright 2007
 * 
 */
class OpenSSL {
   
   public $privatekey;
   public $publickey;
   public $csr;
   public $crypttext;
   public $ekey;
    
   public function encrypt($plain) {
      
      // Turn public key into resource
      $publickey =
openssl_get_publickey(is_file(OPEN_SSL_PUBKEY_PATH)?
file_get_contents(OPEN_SSL_PUBKEY_PATH) :
OPEN_SSL_PUBKEY_PATH);
      
      // Encrypt
      openssl_seal($plain, $crypttext, $ekey,
array($publickey));
      openssl_free_key($publickey);
      
      // Set values
      $this->crypttext = $crypttext;
      $this->ekey = $ekey[0];
   }
 
   public function decrypt($crypt, $privatekey,
$ekey="") {
   
      // Turn private key into resource
      $privatekey =
openssl_get_privatekey((is_file($privatekey)?
file_get_contents($privatekey) : $privatekey),
OPEN_SSL_PASSPHRASE);
      
      // Decrypt
      openssl_open($crypt, $plaintext, $ekey, $privatekey);
      openssl_free_key($privatekey);
      
      // Return value
      return $plaintext;
   }

   public function do_csr( 
   $countryName = "UK",
   $stateOrProvinceName = "bond",
   $localityName = "Blah",
   $organizationName = "Blah1",
   $organizationalUnitName = "Blah2",
   $commonName = "Joe Bloggs",
   $emailAddress = "openssldomain.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);
   }
   
}

?>
----
Server IP: 209.41.74.194
Probable Submitter: 209.188.114.2
----
Manual Page -- http://w
ww.php.net/manual/en/ref.openssl.php
Edit        -- https://master
.php.net/note/edit/77439
Del: integrated  -- h
ttps://master.php.net/note/delete/77439/integrated
Del: useless     -- http
s://master.php.net/note/delete/77439/useless
Del: bad code    -- htt
ps://master.php.net/note/delete/77439/bad+code
Del: spam        -- https:/
/master.php.net/note/delete/77439/spam
Del: non-english -- 
https://master.php.net/note/delete/77439/non-english
Del: in docs     -- http
s://master.php.net/note/delete/77439/in+docs
Del: other reasons-- https://mast
er.php.net/note/delete/77439
Reject      -- https://mast
er.php.net/note/reject/77439
Search      -- https://
master.php.net/manage/user-notes.php

-- 
PHP Notes Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub
.php


[1]

about | contact  Other archives ( Real Estate discussion Medical topics )