dcsimg
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

A Guide to Cryptography in PHP : Page 4

This comprehensive guide discusses where to get, how to install, and how to use the various cryptography packages available to enhance the security of your PHP applications.


advertisement
Encrypt Data Using an Arbitrary Key Length with the Crypt_RSA PEAR Package
This package supports two-way encryption; it's based on the RSA block cipher. Crypt_RSA supports encryption and decryption using an arbitrary key length. You can download the latest version (1.0.0, stable) and install it like any other PEAR package:

   > pear install pear_package_name
Crypt_RSA performs intensive math calculations, for which it uses one of the following extensions:

Here's an example of using this package:
   <?php
   
   require_once 'Crypt/RSA.php';
   
   //Generates the pair keys
   function generate_key_pair()
   {
      global $public_key,$private_key;
   
      $key_pair = new Crypt_RSA_KeyPair(32);
   
      //Returns public key from the pair
      $public_key = $key_pair->getPublicKey();
   
      //Returns private key from the pair
      $private_key = $key_pair->getPrivateKey();
   }
   
   //Check runtime errors
   function check_error(&$obj)
   {
      if ($obj->isError()){
         $error = $obj->getLastError();
         switch ($error->getCode()) {
         case CRYPT_RSA_ERROR_WRONG_TAIL :
            // nothing to do
            break;
         default:
            // echo error message and exit
            echo 'error: ', $error->getMessage();
            exit;
         }
      }
   }
      
   $file = 'textfile.txt';
   
   generate_key_pair();  
   $plain_text = file_get_contents($file);
      
   //get string represenation of the public key
   $key = Crypt_RSA_Key::fromString($public_key->toString()); 
      
   $rsa_obj = new Crypt_RSA;
   check_error($rsa_obj);
      
   //Ecnrypts $plain_text by the key $key.
   $encrypted = $rsa_obj->encrypt($plain_text, $key);
      
   $encrypted_file = @fopen('encrypted.txt','w');
   $ok_encrypt = fwrite($encrypted_file,$encrypted);
   if($ok_encrypt){
      echo 'The encrypted code was succesfully created '.
      'in encrypted_file.txt!!!'.'<br />';
   }
   else{
      echo ("The write of this file failed!");
   }
   @fclose($encrypted_file);
      
   $enc_text = $encrypted;
     
   //Get string represenation of the private key
   $key2 = Crypt_RSA_Key::fromString($private_key->toString());
   check_error($key2);
      
   //Check encrypting/decrypting function's behaviour
   $rsa_obj->setParams(array('dec_key' => $key2));
   check_error($rsa_obj);
   
   //Decrypts $enc_text
   $decrypted = $rsa_obj->decrypt($enc_text);
      
   $newfile = @fopen('newfile.txt','w');
   $ok_decrypt = @fwrite($newfile,$decrypted);
   if($ok_decrypt){
      echo 'The decrypted code was succesfully created '.
      'in newfile.txt!!!'.'<br />';
   }
   else{
       echo ("The write of this file failed!");
   }
   @fclose($newfile);
    
   ?>   
Generating Hashes with Crypt_HMAC
The Crypt_HMAC PEAR package contains a class you can use to calculate RFC 2104-compliant hashes. Crypt_HMAC is easy to use; you need only provide your secret key, the hash method you want to use, and the plaintext string. Crypt_HMAC supports both MD5 and SHA-1 algorithms. The latest stable released version is 1.0.0. You install it just like any other PEAR package:

   > pear install pear_package_name
And here's a simple example that creates a hash using Crypt_HMAC:

   <?php
      
   require_once 'Crypt/HMAC.php';
   
   //Creating a key by repeating the "0x0b" character for 20 times 
   $key = str_repeat(chr(0x0b), 20);
   
   //Creating an instance of the Crypt_HMAC class
   //$crypt = new Crypt_HMAC($key, 'md5');
   $crypt = new Crypt_HMAC($key, 'md5');
     
   //Hashing function
   echo $crypt->hash('Hello')."<br />";
   
   $key = str_repeat(chr(0xaa), 10);
   $data = str_repeat(chr(0xdd), 50);
   
   //Sets key to use with hash
   $crypt->setKey($key);
   echo $crypt->hash($data)."\n";
   ?>


Thanks for your registration, follow us on our social networks to keep up-to-date