1
0

StonePhpSafeCrypt_packcrypt.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. function PackCrypt(&$Data, $WeakKey, $options=array() ) {
  3. $result = array(
  4. 'success' => false,
  5. 'reason' => 'Incomplete pack for unknown reason; indicates horrible failure.',
  6. 'output' => false
  7. );
  8. // load options
  9. if (isset($options['cipher'])) { // Check whether user specified an alternate cipher in the options
  10. $CipherType = $options['cipher']; // if so, use it
  11. } else {
  12. $CipherType = DEFAULT_ENCRYPTION_METHOD; // otherwise, use the default cipher
  13. }
  14. if (isset($options['mode'])) { // Check whether user specified an alternate block mode in the options
  15. $mode = $options['mode']; // if so, use it
  16. } else {
  17. $mode = DEFAULT_ENCRYPTION_MODE; // otherwise, use the default block mode
  18. }
  19. if (isset($options['salt'])) { // Check whether user specified an alternate md5 salt in the options
  20. $salt = $options['salt']; // if so, use it
  21. } else {
  22. $salt = DEFAULT_MD5_SALT; // otherwise, use the default salt
  23. }
  24. // do preparation
  25. $SecretData = serialize($Data); // Convert data into a serialized string for single packing
  26. $compressor = false;
  27. global $compressors;
  28. // handle potential compression
  29. if (isset($options['compressor'])) {
  30. if (isset($compressors[$options['compressor']])) {
  31. $compressor = $compressors[$options['compressor']];
  32. if (function_exists($compressor['encode']['fname'])) {
  33. if (function_exists($compressor['decode']['fname'])) {
  34. if (isset($compressor['encode']['args'])) {
  35. if (isset($compressor['encode']['data_arg'])) {
  36. $largs = $compressor['encode']['args'];
  37. $largs[$compressor['encode']['data_arg']] = $SecretData;
  38. $SecretData = call_user_func_array($compressor['encode']['fname'], $largs);
  39. } else {
  40. $result['reason'] = 'The requested compressor, "' . $options['compressor'] . '", requires an argument list; however the data argument has not been specified, making call impossible.';
  41. return $result;
  42. }
  43. } else {
  44. $SecretData = $compressor['encode']['fname']($SecretData);
  45. }
  46. } else {
  47. $result['reason'] = 'The requested compressor, "' . $options['compressor'] . '", requires the decode function "' . $compressor['decode']['fname'] . '", which is not present in this PHP installation.';
  48. return $result;
  49. }
  50. } else {
  51. $result['reason'] = 'The requested compressor, "' . $options['compressor'] . '", requires the encode function "' . $compressor['encode']['fname'] . '", which is not present in this PHP installation.';
  52. return $result;
  53. }
  54. } else {
  55. $result['reason'] = 'The requested compressor, "' . $options['compressor'] . '", is not configured in this script.';
  56. return $result;
  57. }
  58. }
  59. // do work
  60. $td = mcrypt_module_open($CipherType, '', $mode, ''); // Open the cipher module
  61. $ks = mcrypt_enc_get_key_size($td); // Get required key size
  62. $strongkey = substr(md5($salt . $WeakKey), 0, $ks); // Harden key data into safe key
  63. $ivsz = mcrypt_enc_get_iv_size($td); // Get the size of the appropriate local initialization vector
  64. // net2ftp - the 2nd argument of mcrypt_create_iv must be different on Unix and Windows
  65. // Try first Unix, and if $iv is empty try Windows
  66. // Use prefix @ to suppress PHP Warning messages
  67. $iv = @mcrypt_create_iv($ivsz, MCRYPT_DEV_RANDOM); // Generate an initialization vector ** Unix **
  68. if ($iv == "") {
  69. $iv = @mcrypt_create_iv($ivsz, MCRYPT_RAND); // Generate an initialization vector ** Windows **
  70. }
  71. mcrypt_generic_init($td, $strongkey, $iv); // Init encryption engine
  72. $encrypted = mcrypt_generic($td, $SecretData); // Perform encryption
  73. mcrypt_generic_deinit($td); // Shut down encryption engine
  74. mcrypt_module_close($td); // Close the cipher module
  75. $IvDataPack = $iv . $encrypted; // Prepend the IV onto the data stream for convenient transfer
  76. $result['output'] = BlockScramble($IvDataPack, $strongkey); // Return the IV prepended to the data stream, CBC tamper protected
  77. $result['success'] = true;
  78. $result['reason'] = 'Successful pack.';
  79. return $result;
  80. }
  81. function UnpackCrypt(&$SecretData, $WeakKey, $options=array() ) {// $CipherType = 'tripledes', $mode = 'ofb') {
  82. $result = array(
  83. 'success' => false,
  84. 'reason' => 'Incomplete unpack for unknown reason; indicates horrible failure.',
  85. 'output' => false
  86. );
  87. // load options
  88. if (isset($options['cipher'])) { // Check whether user specified an alternate cipher in the options
  89. $CipherType = $options['cipher']; // if so, use it
  90. } else {
  91. $CipherType = DEFAULT_ENCRYPTION_METHOD; // otherwise, use the default cipher
  92. }
  93. if (isset($options['mode'])) { // Check whether user specified an alternate block mode in the options
  94. $mode = $options['mode']; // if so, use it
  95. } else {
  96. $mode = DEFAULT_ENCRYPTION_MODE; // otherwise, use the default block mode
  97. }
  98. if (isset($options['salt'])) { // Check whether user specified an alternate md5 salt in the options
  99. $salt = $options['salt']; // if so, use it
  100. } else {
  101. $salt = DEFAULT_MD5_SALT; // otherwise, use the default salt
  102. }
  103. // do work
  104. $td = mcrypt_module_open($CipherType, '', $mode, ''); // Open the cipher module
  105. $ks = mcrypt_enc_get_key_size($td); // Get required key size
  106. $strongkey = substr(md5($salt . $WeakKey), 0, $ks); // Regenerate hardened key from weak key
  107. $DescrambleData = BlockDescramble($SecretData, $strongkey); // Remove leading-block CBC tampering attack protection
  108. $ivsz = mcrypt_enc_get_iv_size($td); // Get the size of the appropriate local initialization vector
  109. $iv = substr($DescrambleData, 0, $ivsz); // Recover the initialization vector
  110. $WorkData = substr($DescrambleData, $ivsz); // Recover the data block
  111. mcrypt_generic_init($td, $strongkey, $iv); // Init decryption engine
  112. $decrypted = mdecrypt_generic($td, $WorkData); // Perform decryption
  113. mcrypt_generic_deinit($td); // Shut down decryption engine
  114. mcrypt_module_close($td); // Close the cipher module
  115. // handle potential decompression
  116. global $compressors;
  117. $compressor = false;
  118. if (isset($options['compressor'])) {
  119. if (isset($compressors[$options['compressor']])) {
  120. $compressor = $compressors[$options['compressor']];
  121. if (function_exists($compressor['encode']['fname'])) {
  122. if (function_exists($compressor['decode']['fname'])) {
  123. if (isset($compressor['decode']['args'])) {
  124. if (isset($compressor['decode']['data_arg'])) {
  125. $largs = $compressor['decode']['args'];
  126. $largs[$compressor['decode']['data_arg']] = $decrypted;
  127. $decrypted = call_user_func_array($compressor['decode']['fname'], $largs);
  128. } else {
  129. $result['reason'] = 'The requested compressor, "' . $options['compressor'] . '", requires an argument list; however the data argument has not been specified, making call impossible.';
  130. return $result;
  131. }
  132. } else {
  133. $decrypted = $compressor['decode']['fname']($decrypted);
  134. }
  135. } else {
  136. $result['reason'] = 'The requested compressor, "' . $options['compressor'] . '", requires the decode function "' . $compressor['decode']['fname'] . '", which is not present in this PHP installation.';
  137. return $result;
  138. }
  139. } else {
  140. $result['reason'] = 'The requested compressor, "' . $options['compressor'] . '", requires the encode function "' . $compressor['encode']['fname'] . '", which is not present in this PHP installation.';
  141. return $result;
  142. }
  143. } else {
  144. $result['reason'] = 'The requested compressor, "' . $options['compressor'] . '", is not configured in this script.';
  145. return $result;
  146. }
  147. }
  148. $result['success'] = true;
  149. $result['reason'] = 'Successful unpack.';
  150. $result['output'] = unserialize($decrypted); // Convert data from a serialized string and return
  151. return $result;
  152. }
  153. ?>