PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/sys/utility/encryption.php

http://github.com/jawngee/HeavyMetal
PHP | 214 lines | 74 code | 25 blank | 115 comment | 9 complexity | 0361a24c49776ec6ade2f914ae76e210 MD5 | raw file
  1. <?
  2. /**
  3. * Provides two-way keyed encoding using XOR Hashing and Mcrypt
  4. *
  5. * @copyright Copyright 2009-2012 Jon Gilkison and Trunk Archive Inc
  6. * @package application
  7. *
  8. * Original code: Rick Ellis
  9. * http://codeigniter.com/user_guide/license.html
  10. *
  11. * Modified to be more heavymetal-ish by Jon Gilkison. Made mcrypt a requirement.
  12. *
  13. * Copyright (c) 2009, Jon Gilkison and Trunk Archive Inc.
  14. * All rights reserved.
  15. *
  16. * Redistribution and use in source and binary forms, with or without
  17. * modification, are permitted provided that the following conditions are met:
  18. *
  19. * - Redistributions of source code must retain the above copyright notice,
  20. * this list of conditions and the following disclaimer.
  21. * - Redistributions in binary form must reproduce the above copyright
  22. * notice, this list of conditions and the following disclaimer in the
  23. * documentation and/or other materials provided with the distribution.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  26. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  29. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  30. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  31. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  32. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  33. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  34. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  35. * POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. * This is a modified BSD license (the third clause has been removed).
  38. * The BSD license may be found here:
  39. *
  40. * http://www.opensource.org/licenses/bsd-license.php
  41. */
  42. uses('sys.app.config');
  43. /**
  44. * Provides two-way keyed encoding using XOR Hashing and Mcrypt
  45. *
  46. * @package application
  47. * @subpackage utility
  48. * @link http://wiki.getheavy.info/index.php/Encryption
  49. */
  50. class Encryption
  51. {
  52. private $key = '';
  53. private $hash = 'sha1';
  54. private $mode = MCRYPT_MODE_ECB;
  55. private $cipher = MCRYPT_RIJNDAEL_256;
  56. /**
  57. * Constructor
  58. */
  59. function __construct($key=null,$hash='sha1',$cipher=MCRYPT_RIJNDAEL_256)
  60. {
  61. if ($key==null)
  62. {
  63. $config=Config::Get('encryption');
  64. $this->key=md5($config->key);
  65. $this->hash=$config->hash ? $config->hash : $hash;
  66. $this->cipher=$config->cipher ? constant($config->cipher) : MCRYPT_RIJNDAEL_256;
  67. }
  68. $this->key=md5($config->key);
  69. $this->hash=$config->hash ? $config->hash : $hash;
  70. }
  71. /**
  72. * Encode
  73. *
  74. * Encodes the message string using bitwise XOR encoding.
  75. * The key is combined with a random hash, and then it
  76. * too gets converted using XOR. The whole thing is then run
  77. * through mcrypt (if supported) using the randomized key.
  78. * The end result is a double-encrypted message string
  79. * that is randomized with each call to this function,
  80. * even if the supplied message and key are the same.
  81. *
  82. * @access public
  83. * @param string the string to encode
  84. * @param string the key
  85. * @return string
  86. */
  87. function encode($string, $key = null)
  88. {
  89. if (!$key)
  90. $key = $this->key;
  91. $enc = $this->_xor_encode($string, $key);
  92. $init_size = mcrypt_get_iv_size($this->cipher, $this->mode);
  93. $init_vect = mcrypt_create_iv($init_size, MCRYPT_RAND);
  94. $enc=mcrypt_encrypt($this->cipher, $key, $enc, $this->mode, $init_vect);
  95. return base64_encode($enc);
  96. }
  97. /**
  98. * Decode
  99. *
  100. * Reverses the above process
  101. *
  102. * @access public
  103. * @param string
  104. * @param string
  105. * @return string
  106. */
  107. function decode($string, $key = '')
  108. {
  109. if (!$key)
  110. $key = $this->key;
  111. $dec = base64_decode($string);
  112. if ($dec === FALSE)
  113. return FALSE;
  114. $init_size = mcrypt_get_iv_size($this->cipher, $this->mode);
  115. $init_vect = mcrypt_create_iv($init_size, MCRYPT_RAND);
  116. $dec=rtrim(mcrypt_decrypt($this->cipher, $key, $dec, $this->mode, $init_vect), "\0");
  117. return $this->_xor_decode($dec, $key);
  118. }
  119. /**
  120. * XOR Encode
  121. *
  122. * Takes a plain-text string and key as input and generates an
  123. * encoded bit-string using XOR
  124. *
  125. * @access private
  126. * @param string
  127. * @param string
  128. * @return string
  129. */
  130. function _xor_encode($string, $key)
  131. {
  132. $rand = '';
  133. while (strlen($rand) < 32)
  134. $rand .= mt_rand(0, mt_getrandmax());
  135. $rand = $this->hash($rand);
  136. $enc = '';
  137. for ($i = 0; $i < strlen($string); $i++)
  138. $enc .= substr($rand, ($i % strlen($rand)), 1).(substr($rand, ($i % strlen($rand)), 1) ^ substr($string, $i, 1));
  139. return $this->_xor_merge($enc, $key);
  140. }
  141. /**
  142. * XOR Decode
  143. *
  144. * Takes an encoded string and key as input and generates the
  145. * plain-text original message
  146. *
  147. * @access private
  148. * @param string
  149. * @param string
  150. * @return string
  151. */
  152. function _xor_decode($string, $key)
  153. {
  154. $string = $this->_xor_merge($string, $key);
  155. $dec = '';
  156. for ($i = 0; $i < strlen($string); $i++)
  157. $dec .= (substr($string, $i++, 1) ^ substr($string, $i, 1));
  158. return $dec;
  159. }
  160. /**
  161. * XOR key + string Combiner
  162. *
  163. * Takes a string and key as input and computes the difference using XOR
  164. *
  165. * @access private
  166. * @param string
  167. * @param string
  168. * @return string
  169. */
  170. function _xor_merge($string, $key)
  171. {
  172. $hash = $this->hash($key);
  173. $str = '';
  174. for ($i = 0; $i < strlen($string); $i++)
  175. $str .= substr($string, $i, 1) ^ substr($hash, ($i % strlen($hash)), 1);
  176. return $str;
  177. }
  178. /**
  179. * Hash encode a string
  180. *
  181. * @access public
  182. * @param string
  183. * @return string
  184. */
  185. function hash($str)
  186. {
  187. return ($this->hash == 'sha1') ? sha1($str) : md5($str);
  188. }
  189. }