/sftPAPIPlugin/lib/core/PAPIASCrypto.class.php

https://github.com/juanda/symfonite · PHP · 217 lines · 125 code · 50 blank · 42 comment · 15 complexity · b3b6be3867a538733eec31dc852cace0 MD5 · raw file

  1. <?php
  2. // Copyright (c) 2005, RedIRIS. All Rights Reserved.
  3. //
  4. // You may distribute under the terms of the GNU General Public License,
  5. // as specified in the LICENSE file that was shipped with this distribution
  6. // RIJNDAEL Crypt Functions (AES)
  7. // Constant for encrypt and decrypt data with openssl
  8. class PAPIASCrypto
  9. {
  10. const PADDINGSIZE = 11;
  11. public static function encrypt_AES($input, $key)
  12. {
  13. $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_ECB, '');
  14. $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
  15. $key = substr($key, 0, mcrypt_enc_get_key_size($td));
  16. if (mcrypt_generic_init($td, $key, $iv) != -1)
  17. {
  18. // Encrypt the text
  19. $crypttext = mcrypt_generic($td, $input);
  20. mcrypt_generic_deinit($td);
  21. }
  22. mcrypt_module_close($td);
  23. // Encode the encrypted text
  24. $crypttext = base64_encode($crypttext);
  25. return $crypttext;
  26. }
  27. public static function decrypt_AES($input, $key)
  28. {
  29. // Decode the encrypted text
  30. $input = base64_decode($input);
  31. $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_ECB, '');
  32. $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
  33. $key = substr($key, 0, mcrypt_enc_get_key_size($td));
  34. if (mcrypt_generic_init($td, $key, $iv) != -1)
  35. {
  36. // Decrypt the text
  37. $decrypttext = mdecrypt_generic($td, $input);
  38. mcrypt_generic_deinit($td);
  39. }
  40. mcrypt_module_close($td);
  41. $decrypttext = trim($decrypttext);
  42. return $decrypttext;
  43. }
  44. // 3DES Crypt Functions (Not used in phpPoA)
  45. public static function encrypt_3DES($input, $key)
  46. {
  47. $td = mcrypt_module_open('tripledes', '', 'ecb', '');
  48. $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
  49. $key = substr($key, 0, mcrypt_enc_get_key_size($td));
  50. if (mcrypt_generic_init($td, $key, $iv) != -1)
  51. {
  52. // Encrypt the text
  53. $crypttext = mcrypt_generic($td, $input);
  54. mcrypt_generic_deinit($td);
  55. }
  56. mcrypt_module_close($td);
  57. // Encode the encrypted text
  58. $crypttext = base64_encode($crypttext);
  59. return $crypttext;
  60. }
  61. public static function decrypt_3DES($input, $key)
  62. {
  63. // Decode the encrypted text
  64. $input = base64_decode($input);
  65. $td = mcrypt_module_open('tripledes', '', 'ecb', '');
  66. $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
  67. $key = substr($key, 0, mcrypt_enc_get_key_size($td));
  68. if (mcrypt_generic_init($td, $key, $iv) != -1)
  69. {
  70. // Decrypt the text
  71. $decrypttext = mdecrypt_generic($td, $input);
  72. mcrypt_generic_deinit($td);
  73. }
  74. mcrypt_module_close($td);
  75. $decrypttext = trim($decrypttext);
  76. return $decrypttext;
  77. }
  78. // Openssl Crypt Functions (Not used in phpPoA)
  79. //////////////////////////////////////////////////////////////////////////////////////
  80. // Openssl Crypt Functions
  81. //////////////////////////////////////////////////////////////////////////////////////
  82. //&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  83. // funcion openssl_encrypt (Not used in phpPoA)
  84. //&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  85. public static function openssl_encrypt($in, $key, $key_bits = 0)
  86. {
  87. // Get the byte size of data string
  88. $inputSize = strlen($in);
  89. // Get details of the key
  90. $res = openssl_get_privatekey($key);
  91. if ($key_bits == 0)
  92. {
  93. $key_details = openssl_pkey_get_details($res);
  94. } else
  95. {
  96. $key_details = array('bits' => $key_bits);
  97. }
  98. // Get the output block maximun size in Bytes
  99. $outputBlockSize = $key_details['bits'] / 8;
  100. // Total number of blocks
  101. $inputBlockSize = $outputBlockSize - PAPIASCrypto::PADDINGSIZE;
  102. $numBlocks = ceil($inputSize / $inputBlockSize);
  103. // Start to encrypt.
  104. $blockCount = 0;
  105. $cryptBuffer = array();
  106. while ($blockCount < $numBlocks)
  107. {
  108. $index = $blockCount * $inputBlockSize;
  109. $block = substr($in, $index, $inputBlockSize);
  110. openssl_private_encrypt($block, $crypttext, $key);
  111. $cryptBuffer[$blockCount] = $crypttext;
  112. $blockCount++;
  113. }
  114. // Now joint the array with the blocks string encripted
  115. $cryptData = join("", $cryptBuffer);
  116. $base64CryptData = base64_encode($cryptData);
  117. // Return the encrypted, joined and base64 encode data string.
  118. return $base64CryptData;
  119. }
  120. //&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  121. // funcion openssl_decrypt
  122. //&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  123. public static function openssl_decrypt($in, $key, $error_log, $key_bits = 0)
  124. {
  125. // Decode the base64 input string
  126. $in = base64_decode($in);
  127. // Get the byte size of data string
  128. $inputSize = strlen($in);
  129. // Get details of the key
  130. $res = openssl_get_publickey($key);
  131. if ($key_bits == 0)
  132. {
  133. $key_details = openssl_pkey_get_details($res);
  134. } else
  135. {
  136. $key_details = array('bits' => $key_bits);
  137. }
  138. // Get the output block maximun size in Bytes
  139. $outputBlockSize = $key_details['bits'] / 8;
  140. //$inputBlockSize = $outputBlockSize - PADDINGSIZE;
  141. $inputBlockSize = $outputBlockSize;
  142. $numBlocks = ceil($inputSize / $inputBlockSize);
  143. // Start to decrypt.
  144. $blockCount = 0;
  145. $decryptBuffer = array();
  146. while ($blockCount < $numBlocks)
  147. {
  148. $index = $blockCount * $inputBlockSize;
  149. $block = substr($in, $index, $inputBlockSize);
  150. // Decrypt the text
  151. if (!openssl_public_decrypt($block, $decrypttext, $key))
  152. {
  153. // Cannot decrypt!
  154. $error_message = date("d-M-Y H:i:s ") . "openssl_decrypt() Function: Cannot decrypt response, check GPoA public key.";
  155. error_log($error_message . "\n", 3, $error_log);
  156. return 0;
  157. }
  158. $decryptBuffer[$blockCount] = $decrypttext;
  159. $blockCount++;
  160. }
  161. // Now joint the array with the blocks string encripted
  162. $decryptData = join("", $decryptBuffer);
  163. // Return the base64 dencode, decrypted and joined data string.
  164. return $decryptData;
  165. }
  166. }
  167. ?>