phpPoA2
[ class tree: phpPoA2 ] [ index: phpPoA2 ] [ all elements ]

Source for file PAPICrypt.php

Documentation is available at PAPICrypt.php

  1. <?php
  2. /**
  3.  * @copyright Copyright 2005-2011 RedIRIS, http://www.rediris.es/
  4.  *
  5.  *  This file is part of phpPoA2.
  6.  *
  7.  *  phpPoA2 is free software: you can redistribute it and/or modify
  8.  *  it under the terms of the GNU General Public License as published by
  9.  *  the Free Software Foundation, either version 3 of the License, or
  10.  *  (at your option) any later version.
  11.  *
  12.  *  phpPoA2 is distributed in the hope that it will be useful,
  13.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  *  GNU General Public License for more details.
  16.  *
  17.  *  You should have received a copy of the GNU General Public License
  18.  *  along with phpPoA2. If not, see <http://www.gnu.org/licenses/>.
  19.  *
  20.  * @license http://www.gnu.org/licenses/gpl.html GNU General Public License
  21.  * @version 2.0
  22.  * @package phpPoA2
  23.  * @filesource
  24.  */
  25.  
  26. /**
  27.  * Cryptographic routines for PAPI protocol v1.
  28.  */
  29. class PAPICrypt {
  30.  
  31.     // asymetric
  32.     protected $pubkey;
  33.     protected $pubkeylen;
  34.     protected $privkey;
  35.     protected $privkeylen;
  36.  
  37.     // symmetric
  38.     protected $symmetric;
  39.  
  40.     /**
  41.      * 
  42.      */
  43.     public function __construct($symmetric ""$pubkey ""$privkey ""$passphrase ""{
  44.         if (!empty($symmetric)) $this->setSymmetricKey($symmetric);
  45.  
  46.         if (!empty($pubkey)) $this->setPublicKey($pubkey);
  47.  
  48.         if (!empty($privkey)) $this->setPrivateKey($privkey$passphrase);
  49.     }
  50.  
  51.     /**
  52.      * 
  53.      */
  54.     public function setSymmetricKey($symmetric{
  55.         $this->symmetric = $symmetric;
  56.     }
  57.  
  58.     /**
  59.      * 
  60.      */
  61.     public function setPublicKey($pubkey{
  62.         // configure pubkey
  63.         $this->pubkey = file_get_contents($pubkey);
  64.         if (!$this->pubkey{
  65.             throw new PoAException('pubkey-error'E_USER_ERRORarray($pubkey));
  66.         }
  67.  
  68.         // calculate key length
  69.         if (function_exists("openssl_pkey_get_details")) {
  70.             $resource openssl_pkey_get_public($this->pubkey);
  71.             if (!$resource{
  72.                 throw new PoAException('pubkey-error'E_USER_ERRORarray($pubkey));
  73.             }
  74.             $details openssl_pkey_get_details($resource);
  75.             $this->pubkeylen = $details['bits'];
  76.         else {
  77.             $key new RSAPublicKey($this->pubkey);
  78.             $this->pubkeylen = $key->getBits();
  79.         }
  80.    }
  81.  
  82.     /**
  83.      * 
  84.      */
  85.     public function setPrivateKey($privkey$passphrase ""{
  86.         // configure privkey
  87.         $this->privkey = file_get_contents($privkey);
  88.         if (!$this->privkey{
  89.             throw new PoAException('privkey-error'E_USER_ERRORarray($privkey));
  90.         }
  91.  
  92.         // calculate key length
  93.         if (function_exists("openssl_pkey_get_details")) {
  94.             $resource openssl_pkey_get_private($this->privkey$passphrase);
  95.             if (!$resource{
  96.                 throw new PoAException('privkey-error'E_USER_ERRORarray($privkey));
  97.             }
  98.             $details openssl_pkey_get_details($resource);
  99.             $this->privkeylen = $details['bits'];
  100.         else {
  101.             $key new RSAPrivateKey($this->privkey);
  102.             $this->privkeylen = $key->getBits();
  103.         }
  104.     }
  105.  
  106.     /**
  107.      * 
  108.      */
  109.     protected function initializeAES({
  110.         $resource false;
  111.  
  112.         // configure
  113.         $module mcrypt_module_open(MCRYPT_RIJNDAEL_128''MCRYPT_MODE_ECB'');
  114.         $iv     mcrypt_create_iv(mcrypt_enc_get_iv_size($module)MCRYPT_RAND);
  115.         $key    substr($this->symmetric0mcrypt_enc_get_key_size($module));
  116.  
  117.         // load
  118.         if (mcrypt_generic_init($module$key$iv=== 0{
  119.             $resource $module;
  120.         }
  121.  
  122.         return $resource;
  123.     }
  124.  
  125.     /**
  126.      * 
  127.      */
  128.     protected function initialize3DES({
  129.         $resource false;
  130.  
  131.         // configure
  132.         $module mcrypt_module_open('tripledes''''ecb''');
  133.         $iv     mcrypt_create_iv(mcrypt_enc_get_iv_size($module)MCRYPT_RAND);
  134.         $key    substr($this->symmetric0mcrypt_enc_get_key_size($module));
  135.  
  136.         // load
  137.         if (mcrypt_generic_init($module$key$iv=== 0{
  138.             $resource $module;
  139.         }
  140.  
  141.         return $resource;
  142.     }
  143.  
  144.     /**
  145.      * 
  146.      */
  147.     protected function endSymmetric($module{
  148.         mcrypt_generic_deinit($module);
  149.         mcrypt_module_close($module);
  150.     }
  151.  
  152.     /**
  153.      * 
  154.      */
  155.     public function encryptAES($input{  
  156.         $result '';
  157.         $module $this->initializeAES();
  158.  
  159.         // encrypt the text
  160.         if ($module{
  161.             $result mcrypt_generic($module$input);
  162.             $this->endSymmetric($module);
  163.         }
  164.  
  165.         // encode and return
  166.         return base64_encode($result);
  167.     }
  168.  
  169.     /**
  170.      * 
  171.      */
  172.     public function decryptAES($input{
  173.         $result "";
  174.         $module $this->initializeAES();
  175.  
  176.         // decrypt the text
  177.         if ($module{
  178.             $result mdecrypt_generic($modulebase64_decode($input));
  179.             $this->endSymmetric($module);
  180.         }
  181.  
  182.         // trim and return
  183.         return trim($result);
  184.     }
  185.  
  186.     /**
  187.      * 
  188.      */
  189.     public function encrypt3DES($input{
  190.         $result "";
  191.         $module $this->initialize3DES();
  192.  
  193.         // encrypt the text
  194.         if ($module{
  195.             $result mcrypt_generic($module$input);
  196.             $this->endSymmetric($module);
  197.         }
  198.  
  199.         // encode and return
  200.         return base64_encode($result);
  201.     }
  202.  
  203.     /**
  204.      * 
  205.      */
  206.     public function decrypt3DES($input{
  207.         $result "";
  208.         $module $this->initialize3DES();
  209.  
  210.         // decrypt the text
  211.         if ($module{
  212.             $result mdecrypt_generic($modulebase64_decode($input));
  213.             $this->endSymmetric($module);
  214.         }
  215.  
  216.         // trim and return
  217.         return trim($result);
  218.     }
  219.  
  220.     /**
  221.      * 
  222.      */
  223.     public function encrypt($input{
  224.         $result '';
  225.  
  226.         // get the block size
  227.         $block_size $this->privkeylen / 8;
  228.  
  229.         // get total no. of blocks
  230.         $block_total ceil(strlen($input$block_size);
  231.  
  232.         // encrypt all blocks
  233.         for ($i 0$i $block_total$i++{
  234.             $encrypted '';
  235.             $index $i $block_size;
  236.             $block substr($input$index$block_size);
  237.             if (!openssl_private_encrypt($block$encrypted$this->privkey)) continue;
  238.             $result .= $encrypted;
  239.         }
  240.  
  241.         // encode and return
  242.         return base64_encode($result);
  243.     }
  244.  
  245.     /**
  246.      * 
  247.      */
  248.     public function decrypt($input{
  249.         $result '';
  250.         $input base64_decode($input);
  251.  
  252.  
  253.         // get the block size
  254.         $block_size $this->pubkeylen / 8;
  255.  
  256.         // get expected no. of blocks
  257.         $block_total ceil(strlen($input$block_size);
  258.  
  259.         // decrypt all blocks
  260.         for ($i 0$i $block_total$i++{
  261.              $decrypted '';
  262.              $index $i $block_size;
  263.              $block substr($input$index$block_size);
  264.              if (!openssl_public_decrypt($block$decrypted$this->pubkey)) continue;
  265.              $result .= $decrypted;
  266.         }
  267.  
  268.         return $result;
  269.     }
  270.  
  271. }
  272.  
  273. ?>

Documentation generated on Mon, 20 Feb 2012 12:07:13 +0100 by phpDocumentor 1.4.3