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

Source for file crypt.php

Documentation is available at crypt.php

  1. <?php
  2. /**
  3.  * @copyright Copyright 2005-2010 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. // RIJNDAEL Crypt Functions (AES)
  27.  
  28. // Constant to encrypt and decrypt data with openssl
  29. define('PADDINGSIZE'11);
  30.  
  31. // PHP 5.3 already has this method, for the rest of us (legacy purposes), we
  32. // redefine the method and call
  33. // Please note that papi_openssl_encrypt and papi_openssl_decrypt
  34. // should be used from now on...
  35. if (!function_exists('openssl_encrypt')) {
  36.     function openssl_encrypt($in$key$key_bits{
  37.         return papi_openssl_encrypt($in$key$key_bits);
  38.     }
  39. }
  40. if (!function_exists('openssl_decrypt') ){
  41.     function openssl_decrypt($in$key$key_bits{
  42.         return papi_openssl_decrypt($in$key$key_bits);
  43.     }
  44. }
  45.  
  46. function encrypt_AES($input,$key{
  47.  
  48.     $td mcrypt_module_open(MCRYPT_RIJNDAEL_128,'',MCRYPT_MODE_ECB,'');
  49.     $iv mcrypt_create_iv (mcrypt_enc_get_iv_size($td)MCRYPT_RAND);
  50.  
  51.     $key substr($key0mcrypt_enc_get_key_size($td));
  52.  
  53.     if (mcrypt_generic_init($td$key$iv!= -1{
  54.     // Encrypt the text
  55.         $crypttext mcrypt_generic($td$input);
  56.         mcrypt_generic_deinit($td);
  57.     }
  58.  
  59.     mcrypt_module_close($td);
  60.  
  61.     // Encode the encrypted text
  62.     $crypttext base64_encode($crypttext);
  63.  
  64.     return $crypttext;
  65. }
  66.  
  67. function decrypt_AES($input,$key{
  68.  
  69. // Decode the encrypted text
  70.     $input base64_decode($input);
  71.  
  72.     $td mcrypt_module_open(MCRYPT_RIJNDAEL_128,'',MCRYPT_MODE_ECB,'');
  73.     $iv mcrypt_create_iv (mcrypt_enc_get_iv_size($td)MCRYPT_RAND);
  74.  
  75.     $key substr($key0mcrypt_enc_get_key_size($td));
  76.  
  77.     if (mcrypt_generic_init($td$key$iv!= -1{
  78.     // Decrypt the text
  79.         $decrypttext mdecrypt_generic($td$input);
  80.         mcrypt_generic_deinit($td);
  81.     }
  82.  
  83.     mcrypt_module_close($td);
  84.     $decrypttext trim ($decrypttext);
  85.  
  86.     return $decrypttext;
  87. }
  88.  
  89.  
  90. // 3DES Crypt Functions (Not used in phpPoA)
  91.  
  92. function encrypt_3DES($input,$key{
  93.  
  94.     $td mcrypt_module_open('tripledes''''ecb''');
  95.     $iv mcrypt_create_iv (mcrypt_enc_get_iv_size($td)MCRYPT_RAND);
  96.  
  97.     $key substr($key0mcrypt_enc_get_key_size($td));
  98.  
  99.     if (mcrypt_generic_init($td$key$iv!= -1{
  100.     // Encrypt the text
  101.         $crypttext mcrypt_generic($td$input);
  102.         mcrypt_generic_deinit($td);
  103.     }
  104.  
  105.     mcrypt_module_close($td);
  106.  
  107.     // Encode the encrypted text
  108.     $crypttext base64_encode($crypttext);
  109.  
  110.     return $crypttext;
  111. }
  112.  
  113.  
  114. function decrypt_3DES($input,$key{
  115.  
  116. // Decode the encrypted text
  117.     $input base64_decode($input);
  118.  
  119.     $td mcrypt_module_open('tripledes''''ecb''');
  120.     $iv mcrypt_create_iv (mcrypt_enc_get_iv_size($td)MCRYPT_RAND);
  121.  
  122.     $key substr($key0mcrypt_enc_get_key_size($td));
  123.  
  124.     if (mcrypt_generic_init($td$key$iv!= -1{
  125.     // Decrypt the text
  126.         $decrypttext mdecrypt_generic($td$input);
  127.         mcrypt_generic_deinit($td);
  128.     }
  129.  
  130.     mcrypt_module_close($td);
  131.     $decrypttext trim ($decrypttext);
  132.  
  133.     return $decrypttext;
  134. }
  135.  
  136. // Openssl Crypt Functions (Not used in phpPoA)
  137.  
  138. //////////////////////////////////////////////////////////////////////////////////////
  139. // Openssl Crypt Functions 
  140. //////////////////////////////////////////////////////////////////////////////////////
  141. //&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  142. // funcion papi_openssl_encrypt (Not used in phpPoA)
  143. //&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  144. function papi_openssl_encrypt($in$key$key_bits 0{
  145. // Get the byte size of data string 
  146.     $inputSize strlen($in);
  147.  
  148.     // Get details of the key
  149.     $res openssl_get_privatekey($key);
  150.     if ($key_bits==0{
  151.         $key_details openssl_pkey_get_details($res);
  152.     }
  153.     else {
  154.         $key_details=array('bits' => $key_bits);
  155.     }
  156.  
  157.     // Get the output block maximun size in Bytes
  158.     $outputBlockSize =     $key_details['bits']/8;
  159.  
  160.     // Total number of blocks
  161.     $inputBlockSize $outputBlockSize PADDINGSIZE;
  162.     $numBlocks ceil($inputSize/$inputBlockSize);
  163.  
  164.     // Start to encrypt.
  165.     $blockCount 0;
  166.     $cryptBuffer array();
  167.  
  168.     while ($blockCount $numBlocks{
  169.         $index $blockCount $inputBlockSize;
  170.         $block substr($in$index$inputBlockSize);
  171.         openssl_private_encrypt($block$crypttext$key);
  172.         $cryptBuffer[$blockCount$crypttext;
  173.         $blockCount++;
  174.     }
  175.     // Now joint the array with the blocks string encripted
  176.     $cryptData join(""$cryptBuffer);
  177.  
  178.     $base64CryptData base64_encode($cryptData);
  179.  
  180.     // Return the encrypted, joined and base64 encode data string.
  181.     return $base64CryptData;
  182. }
  183.  
  184. //&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  185. // funcion papi_openssl_decrypt
  186. //&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  187. function papi_openssl_decrypt($in$key$key_bits 0{
  188. // Decode the base64 input string
  189.     $in base64_decode($in);
  190.  
  191.     // Get the byte size of data string
  192.     $inputSize strlen($in);
  193.  
  194.     // Get details of the key
  195.     $res openssl_get_publickey($key);
  196.     if ($key_bits==0{
  197.         $key_details openssl_pkey_get_details($res);
  198.     }
  199.     else {
  200.         $key_details=array('bits' => $key_bits);
  201.     }
  202.  
  203.     // Get the output block maximun size in Bytes
  204.     $outputBlockSize =     $key_details['bits']/8;
  205.  
  206.     //$inputBlockSize = $outputBlockSize - PADDINGSIZE;
  207.     $inputBlockSize $outputBlockSize;
  208.     $numBlocks ceil($inputSize/$inputBlockSize);
  209.  
  210.     // Start to decrypt.
  211.     $blockCount 0;
  212.     $decryptBuffer array();
  213.  
  214.  
  215.  
  216.     while ($blockCount $numBlocks{
  217.         $index $blockCount $inputBlockSize;
  218.         $block substr($in$index$inputBlockSize);
  219.         // Decrypt the text
  220.         if (!openssl_public_decrypt($block$decrypttext$key)) {
  221.             // Cannot decrypt!
  222.             return false;
  223.         }
  224.         $decryptBuffer[$blockCount$decrypttext;
  225.         $blockCount++;
  226.     }
  227.  
  228.     // Now joint the array with the blocks string encripted
  229.     $decryptData join("",$decryptBuffer);
  230.  
  231.     // Return the base64 dencode, decrypted and joined data string.
  232.  
  233.     return $decryptData;
  234. }
  235.  
  236. ?>

Documentation generated on Wed, 13 Oct 2010 15:06:16 +0200 by phpDocumentor 1.4.3