/library/massiveart/utilities/crypt.class.php

https://github.com/massiveart-webservices/ZOOLU · PHP · 93 lines · 31 code · 5 blank · 57 comment · 0 complexity · 7f698c9d07326b8a16e1a9b2c3051e40 MD5 · raw file

  1. <?php
  2. /**
  3. * ZOOLU - Content Management System
  4. * Copyright (c) 2008-2012 HID GmbH (http://www.hid.ag)
  5. *
  6. * LICENSE
  7. *
  8. * This file is part of ZOOLU.
  9. *
  10. * ZOOLU is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * ZOOLU is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with ZOOLU. If not, see http://www.gnu.org/licenses/gpl-3.0.html.
  22. *
  23. * For further information visit our website www.getzoolu.org
  24. * or contact us at zoolu@getzoolu.org
  25. *
  26. * @category ZOOLU
  27. * @package library.massiveart.utilities
  28. * @copyright Copyright (c) 2008-2012 HID GmbH (http://www.hid.ag)
  29. * @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License, Version 3
  30. * @version $Id: version.php
  31. */
  32. /**
  33. * Crypt
  34. *
  35. * Version history (please keep backward compatible):
  36. * 1.0, 2010-04-06: Thomas Schedler
  37. *
  38. * @author Thomas Schedler <tsh@massiveart.com>
  39. * @version 1.0
  40. * @package massiveart.utilities
  41. * @subpackage Crypt
  42. */
  43. class Crypt
  44. {
  45. /**
  46. * encrypt
  47. * @param string $key
  48. * @param string $plain_text
  49. * @return string
  50. * @author Thomas Schedler <tsh@massiveart.com>
  51. * @version 1.0
  52. */
  53. public static function encrypt($core, $key, $plain_text)
  54. {
  55. $core->logger->debug('massiveart->utilities->Crypt->encrypt: ' . $key . ', ' . $plain_text);
  56. try {
  57. $plain_text = trim($plain_text);
  58. $iv = substr(md5($key), 0, mcrypt_get_iv_size(MCRYPT_CAST_256, MCRYPT_MODE_CFB));
  59. $c_t = mcrypt_cfb(MCRYPT_CAST_256, $key, $plain_text, MCRYPT_ENCRYPT, $iv);
  60. return base64_encode($c_t);
  61. } catch (Exception $exc) {
  62. $core->logger->err($exc);
  63. return false;
  64. }
  65. }
  66. /**
  67. * decrypt
  68. * @param string $key
  69. * @param string $c_t
  70. * @return string
  71. * @author Thomas Schedler <tsh@massiveart.com>
  72. * @version 1.0
  73. */
  74. public static function decrypt($core, $key, $c_t)
  75. {
  76. $core->logger->debug('massiveart->utilities->Crypt->decrypt: ' . $key . ', ' . $c_t);
  77. try {
  78. $c_t = base64_decode($c_t);
  79. $iv = substr(md5($key), 0, mcrypt_get_iv_size(MCRYPT_CAST_256, MCRYPT_MODE_CFB));
  80. $p_t = mcrypt_cfb(MCRYPT_CAST_256, $key, $c_t, MCRYPT_DECRYPT, $iv);
  81. return trim($p_t);
  82. } catch (Exception $exc) {
  83. $core->logger->err($exc);
  84. return false;
  85. }
  86. }
  87. }
  88. ?>