/lib/jelix/utils/jCrypt.class.php

https://github.com/foxmask/Booster · PHP · 143 lines · 72 code · 8 blank · 63 comment · 11 complexity · 34bb4ceadbb1a12f19955a0a5f1ba361 MD5 · raw file

  1. <?php
  2. /**
  3. * @package jelix
  4. * @subpackage utils
  5. * @author Antoine Detante
  6. * @contributor Laurent Jouanneau
  7. * @contributor Hadrien Lanneau <hadrien at over-blog dot com>
  8. * @copyright 2007 Antoine Detante, 2009 Laurent Jouanneau
  9. * @link http://www.jelix.org
  10. * @licence GNU Lesser General Public Licence see LICENCE file or http://www.gnu.org/licenses/lgpl.html
  11. **/
  12. /**
  13. * Static methods help to encrypt and decrypt string. mCrypt is used if it is
  14. * installed, else a basic algorithm is used.
  15. * @package jelix
  16. * @subpackage utils
  17. */
  18. class jCrypt {
  19. /**
  20. * Decrypt a string with a specific key.
  21. *
  22. * Use mCrypt if it is installed, else a basic algorithm.
  23. * @param string $string the string to decrypt
  24. * @param string $key the key used to decrypt. If not given, use the key indicated in the configuration
  25. * @return string decrypted string
  26. */
  27. public static function decrypt($string,$key=''){
  28. $decrypted=null;
  29. $decodedString=base64_decode($string);
  30. // Check if mcrypt is installed, and if WAKE algo exists
  31. if(function_exists("mcrypt_generic")&&mcrypt_module_self_test(MCRYPT_WAKE))
  32. $decrypted=jCrypt::mcryptDecrypt($decodedString,$key);
  33. else
  34. $decrypted=jCrypt::simpleCrypt($decodedString,$key);
  35. return $decrypted;
  36. }
  37. /**
  38. * Encrypt a string with a specific key
  39. *
  40. * Use mCrypt if it is installed, else a basic algorithm.
  41. * @param string $string the string to encrypt
  42. * @param string $key the key used to encrypt. If not given, use the key indicated in the configuration
  43. * @return string encrypted string
  44. */
  45. public static function encrypt($string,$key=''){
  46. $encrypted=null;
  47. // Check if mcrypt is installed, and if WAKE algo exists
  48. if(function_exists("mcrypt_generic")&&mcrypt_module_self_test(MCRYPT_WAKE))
  49. $encrypted=jCrypt::mcryptEncrypt($string,$key);
  50. else
  51. $encrypted=jCrypt::simpleCrypt($string,$key);
  52. return base64_encode($encrypted);
  53. }
  54. /**
  55. * Encrypt a string with mCrypt.
  56. * @param string $string the string to encrypt
  57. * @param string $key the key used to encrypt string. If not given, use
  58. * the key indicated in the configuration
  59. * @return string encrypted string
  60. */
  61. public static function mcryptEncrypt($string, $key='') {
  62. if ($key=='')
  63. throw new jException('jelix~auth.error.key.empty');
  64. if (strlen($key)<15)
  65. throw new jException('jelix~auth.error.key.tooshort',15);
  66. $td = mcrypt_module_open(MCRYPT_WAKE, '', MCRYPT_MODE_STREAM, '');
  67. $ks = mcrypt_enc_get_key_size($td);
  68. $key = substr($key, 0, $ks);
  69. mcrypt_generic_init($td, $key, null);
  70. $encrypted = mcrypt_generic($td, $string);
  71. mcrypt_generic_deinit($td);
  72. mcrypt_module_close($td);
  73. return $encrypted;
  74. }
  75. /**
  76. * Decrypt a string with mCrypt.
  77. * @param string $string the string to decrypt
  78. * @param string $key the key used to decrypt string. If not given, use
  79. * the key indicated in the configuration
  80. * @return string decrypted string
  81. */
  82. public static function mcryptDecrypt($string,$key=''){
  83. if($key=='')
  84. throw new jException('jelix~auth.error.key.empty');
  85. $td = mcrypt_module_open(MCRYPT_WAKE, '', MCRYPT_MODE_STREAM, '');
  86. $ks = mcrypt_enc_get_key_size($td);
  87. $key = substr($key, 0, $ks);
  88. mcrypt_generic_init($td, $key, null);
  89. $decrypted = mdecrypt_generic($td, $string);
  90. mcrypt_generic_deinit($td);
  91. mcrypt_module_close($td);
  92. return $decrypted;
  93. }
  94. /**
  95. * Basic encrypt/decrypt algorithm.
  96. * @author halojoy
  97. * @see http://www.phpbuilder.com/board/showthread.php?t=10326721
  98. * @param string $str the string to encrypt/decrypt
  99. * @param string $key the key used to encrypt/decrypt string (must be >= 8 characters).
  100. * If not given, use the key indicated in the configuration
  101. * @return string encrypted/decrypted string
  102. */
  103. protected static function simpleCrypt($str,$key=''){
  104. if($key=='')
  105. throw new jException('jelix~auth.error.key.empty');
  106. $key=str_replace(chr(32),'',$key);
  107. if(strlen($key)<8)
  108. throw new jException('jelix~auth.error.key.tooshort',8);
  109. $kl=strlen($key)<32?strlen($key):32;
  110. $k=array();
  111. for($i=0;$i<$kl;$i++){
  112. $k[$i]=ord($key[$i])&0x1F;
  113. }
  114. $j=0;
  115. for($i=0;$i<strlen($str);$i++){
  116. $e=ord($str[$i]);
  117. $str[$i]=$e&0xE0?chr($e^$k[$j]):chr($e);
  118. $j++;$j=$j==$kl?0:$j;
  119. }
  120. return $str;
  121. }
  122. /**
  123. * Get default key in config
  124. *
  125. * @return string
  126. * @author Hadrien Lanneau <hadrien at over-blog dot com>
  127. **/
  128. private static function _getDefaultKey() {
  129. global $gJConfig;
  130. if (isset($gJConfig->jcrypt['defaultkey']) && $gJConfig->jcrypt['defaultkey'] !='') {
  131. return $gJConfig->jcrypt['defaultkey'];
  132. }
  133. throw new jException('jelix~auth.error.key.empty');
  134. }
  135. }