| 1 |
<?php |
| 2 |
/** |
| 3 |
* Encryption utilities. |
| 4 |
* |
| 5 |
* Copyright: © 2009-2011 |
| 6 |
* {@link http://www.websharks-inc.com/ WebSharks, Inc.} |
| 7 |
* ( coded in the USA ) |
| 8 |
* |
| 9 |
* Released under the terms of the GNU General Public License. |
| 10 |
* You should have received a copy of the GNU General Public License, |
| 11 |
* along with this software. In the main directory, see: /licensing/ |
| 12 |
* If not, see: {@link http://www.gnu.org/licenses/}. |
| 13 |
* |
| 14 |
* @package s2Member\Utilities |
| 15 |
* @since 3.5 |
| 16 |
*/ |
| 17 |
if (realpath (__FILE__) === realpath ($_SERVER["SCRIPT_FILENAME"])) |
| 18 |
exit ("Do not access this file directly."); |
| 19 |
/**/ |
| 20 |
if (!class_exists ("c_ws_plugin__s2member_utils_encryption")) |
| 21 |
{ |
| 22 |
/** |
| 23 |
* Encryption utilities. |
| 24 |
* |
| 25 |
* @package s2Member\Utilities |
| 26 |
* @since 3.5 |
| 27 |
*/ |
| 28 |
class c_ws_plugin__s2member_utils_encryption |
| 29 |
{ |
| 30 |
/** |
| 31 |
* Determines the proper encryption/decryption Key to use. |
| 32 |
* |
| 33 |
* @package s2Member\Utilities |
| 34 |
* @since 111106 |
| 35 |
* |
| 36 |
* @param str $key Optional. Attempt to force a specific Key. Defaults to the one configured for s2Member. Short of that, defaults to: ``wp_salt()``. |
| 37 |
* @return str Proper encryption/decryption Key. If ``$key`` is passed in, and it validates, we'll return that. Otherwise use a default Key. |
| 38 |
*/ |
| 39 |
public static function key ($key = FALSE) |
| 40 |
{ |
| 41 |
$key = (!is_string ($key) || !strlen ($key)) ? $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["sec_encryption_key"] : $key; |
| 42 |
$key = (!is_string ($key) || !strlen ($key)) ? /* Use the installed WordPress® salt. */ wp_salt () : $key; |
| 43 |
$key = (!is_string ($key) || !strlen ($key)) ? /* Default/backup. */ md5 ($_SERVER["HTTP_HOST"]) : $key; |
| 44 |
return /* Proper encryption/decryption key. */ $key; |
| 45 |
} |
| 46 |
/** |
| 47 |
* RIJNDAEL 256: two-way encryption/decryption, with a URL-safe base64 wrapper. |
| 48 |
* |
| 49 |
* Falls back on XOR encryption/decryption when/if mcrypt is not possible. |
| 50 |
* |
| 51 |
* @package s2Member\Utilities |
| 52 |
* @since 3.5 |
| 53 |
* |
| 54 |
* @param str $string A string of data to encrypt. |
| 55 |
* @param str $key Optional. Key used for encryption. Defaults to the one configured for s2Member. Short of that, defaults to: ``wp_salt()``. |
| 56 |
* @param bool $w_md5_cs Optional. Defaults to true. When true, an MD5 checksum is used in the encrypted string *( recommended )*. |
| 57 |
* @return str Encrypted string. |
| 58 |
*/ |
| 59 |
public static function encrypt ($string = FALSE, $key = FALSE, $w_md5_cs = TRUE) |
| 60 |
{ |
| 61 |
if (function_exists ("mcrypt_encrypt") && in_array ("rijndael-256", mcrypt_list_algorithms ()) && in_array ("cbc", mcrypt_list_modes ())) |
| 62 |
{ |
| 63 |
$string = /* Force a valid string value here. */ (is_string ($string)) ? $string : ""; |
| 64 |
$string = /* Indicating this is an RIJNDAEL 256 encrypted string. */ (strlen ($string)) ? "~r2|" . $string : ""; |
| 65 |
/**/ |
| 66 |
$key = /* Obtain encryption/decryption key. */ c_ws_plugin__s2member_utils_encryption::key ($key); |
| 67 |
$key = /* Proper key length. */ substr ($key, 0, mcrypt_get_key_size (MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC)); |
| 68 |
/**/ |
| 69 |
$iv = c_ws_plugin__s2member_utils_strings::random_str_gen (mcrypt_get_iv_size (MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC), false); |
| 70 |
/**/ |
| 71 |
if (strlen ($string) && is_string ($e = mcrypt_encrypt /* Encrypt the string. */ (MCRYPT_RIJNDAEL_256, $key, $string, MCRYPT_MODE_CBC, $iv)) && strlen ($e)) |
| 72 |
$e = /* RIJNDAEL 256 encrypted string with IV and checksum built into itself. */ "~r2:" . $iv . (($w_md5_cs) ? ":" . md5 ($e) : "") . "|" . $e; |
| 73 |
/**/ |
| 74 |
return (isset ($e) && is_string ($e) && strlen ($e)) ? ($base64 = c_ws_plugin__s2member_utils_strings::base64_url_safe_encode ($e)) : ""; |
| 75 |
} |
| 76 |
else /* Fallback on XOR encryption. */ |
| 77 |
return c_ws_plugin__s2member_utils_encryption::xencrypt ($string, $key, $w_md5_cs); |
| 78 |
} |
| 79 |
/** |
| 80 |
* RIJNDAEL 256: two-way encryption/decryption, with a URL-safe base64 wrapper. |
| 81 |
* |
| 82 |
* Falls back on XOR encryption/decryption when mcrypt is not available. |
| 83 |
* |
| 84 |
* @package s2Member\Utilities |
| 85 |
* @since 3.5 |
| 86 |
* |
| 87 |
* @param str $base64 A string of data to decrypt. Should still be base64 encoded. |
| 88 |
* @param str $key Optional. Key used originally for encryption. Defaults to the one configured for s2Member. Short of that, defaults to: ``wp_salt()``. |
| 89 |
* @return str Decrypted string. |
| 90 |
*/ |
| 91 |
public static function decrypt ($base64 = FALSE, $key = FALSE) |
| 92 |
{ |
| 93 |
$base64 = /* Force a valid string value here. */ (is_string ($base64)) ? $base64 : ""; |
| 94 |
$e = (strlen ($base64)) ? c_ws_plugin__s2member_utils_strings::base64_url_safe_decode ($base64) : ""; |
| 95 |
/**/ |
| 96 |
if (function_exists ("mcrypt_decrypt") && in_array ("rijndael-256", mcrypt_list_algorithms ()) && in_array ("cbc", mcrypt_list_modes ()) # |
| 97 |
&& strlen ($e) /* And, is this an RIJNDAEL 256 encrypted string? */ && preg_match ("/^~r2\:([a-zA-Z0-9]+)(?:\:([a-zA-Z0-9]+))?\|(.*?)$/s", $e, $iv_md5_e)) |
| 98 |
{ |
| 99 |
$key = /* Obtain encryption/decryption key. */ c_ws_plugin__s2member_utils_encryption::key ($key); |
| 100 |
$key = /* Proper key length. */ substr ($key, 0, mcrypt_get_key_size (MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC)); |
| 101 |
/**/ |
| 102 |
if (strlen ($iv_md5_e[3]) && ( /* No checksum? */!$iv_md5_e[2] || /* Or, a matching checksum? */ $iv_md5_e[2] === md5 ($iv_md5_e[3]))) |
| 103 |
$d = /* Decrypt the string. */ mcrypt_decrypt (MCRYPT_RIJNDAEL_256, $key, $iv_md5_e[3], MCRYPT_MODE_CBC, $iv_md5_e[1]); |
| 104 |
/**/ |
| 105 |
if (isset ($d) && /* Was ``$iv_md5_e[3]`` decrypted successfully? */ is_string ($d) && strlen ($d)) |
| 106 |
/**/ |
| 107 |
if (strlen ($d = preg_replace ("/^~r2\|/", "", $d, 1, $r2)) && $r2) |
| 108 |
$d = rtrim /* Right-trim NULLS and EOTs. */ ($d, "\0\4"); |
| 109 |
else /* Else we need to empty this out. */ |
| 110 |
$d = /* Empty string. Invalid. */ ""; |
| 111 |
/**/ |
| 112 |
return (isset ($d) && is_string ($d) && strlen ($d)) ? ($string = $d) : ""; |
| 113 |
} |
| 114 |
else /* Fallback on XOR decryption. */ |
| 115 |
return c_ws_plugin__s2member_utils_encryption::xdecrypt ($base64, $key); |
| 116 |
} |
| 117 |
/** |
| 118 |
* XOR two-way encryption/decryption, with a base64 wrapper. |
| 119 |
* |
| 120 |
* @package s2Member\Utilities |
| 121 |
* @since 3.5 |
| 122 |
* |
| 123 |
* @param str $string A string of data to encrypt. |
| 124 |
* @param str $key Optional. Key used for encryption. Defaults to the one configured for s2Member. Short of that, defaults to: ``wp_salt()``. |
| 125 |
* @param bool $w_md5_cs Optional. Defaults to true. When true, an MD5 checksum is used in the encrypted string *( recommended )*. |
| 126 |
* @return str Encrypted string. |
| 127 |
*/ |
| 128 |
public static function xencrypt ($string = FALSE, $key = FALSE, $w_md5_cs = TRUE) |
| 129 |
{ |
| 130 |
$string = /* Force a valid string value here. */ (is_string ($string)) ? $string : ""; |
| 131 |
$string = /* Indicating this is an XOR encrypted string. */ (strlen ($string)) ? "~xe|" . $string : ""; |
| 132 |
/**/ |
| 133 |
$key = /* Obtain encryption/decryption key. */ c_ws_plugin__s2member_utils_encryption::key ($key); |
| 134 |
/**/ |
| 135 |
for ($i = 1, $e = ""; $i <= /* Will NOT run if ``$string`` has no length. */ strlen ($string); $i++) |
| 136 |
{ |
| 137 |
$char = substr ($string, $i - 1, 1); |
| 138 |
$keychar = substr ($key, ($i % strlen ($key)) - 1, 1); |
| 139 |
$e .= chr (ord ($char) + ord ($keychar)); |
| 140 |
} |
| 141 |
$e = /* XOR encrypted? */ (strlen ($e)) ? "~xe" . (($w_md5_cs) ? ":" . md5 ($e) : "") . "|" . $e : ""; |
| 142 |
/**/ |
| 143 |
return (strlen ($e)) ? ($base64 = c_ws_plugin__s2member_utils_strings::base64_url_safe_encode ($e)) : ""; |
| 144 |
} |
| 145 |
/** |
| 146 |
* XOR two-way encryption/decryption, with a base64 wrapper. |
| 147 |
* |
| 148 |
* @package s2Member\Utilities |
| 149 |
* @since 3.5 |
| 150 |
* |
| 151 |
* @param str $base64 A string of data to decrypt. Should still be base64 encoded. |
| 152 |
* @param str $key Optional. Key used originally for encryption. Defaults to the one configured for s2Member. Short of that, defaults to: ``wp_salt()``. |
| 153 |
* @return str Decrypted string. |
| 154 |
*/ |
| 155 |
public static function xdecrypt ($base64 = FALSE, $key = FALSE) |
| 156 |
{ |
| 157 |
$base64 = /* Force a valid string value here. */ (is_string ($base64)) ? $base64 : ""; |
| 158 |
$e = (strlen ($base64)) ? c_ws_plugin__s2member_utils_strings::base64_url_safe_decode ($base64) : ""; |
| 159 |
/**/ |
| 160 |
if (strlen ($e) /* And, is this an XOR encrypted string? */ && preg_match ("/^~xe(?:\:([a-zA-Z0-9]+))?\|(.*?)$/s", $e, $md5_e)) |
| 161 |
{ |
| 162 |
$key = /* Obtain encryption/decryption key. */ c_ws_plugin__s2member_utils_encryption::key ($key); |
| 163 |
/**/ |
| 164 |
if (strlen ($md5_e[2]) && ( /* No checksum? */!$md5_e[1] || /* Or a matching checksum? */ $md5_e[1] === md5 ($md5_e[2]))) |
| 165 |
/**/ |
| 166 |
for ($i = 1, $d = ""; $i <= /* Will NOT run if ``$md5_e[2]`` has no length. */ strlen ($md5_e[2]); $i++) |
| 167 |
{ |
| 168 |
$char = substr ($md5_e[2], $i - 1, 1); |
| 169 |
$keychar = substr ($key, ($i % strlen ($key)) - 1, 1); |
| 170 |
$d .= chr (ord ($char) - ord ($keychar)); |
| 171 |
} |
| 172 |
if (isset ($d) && /* Was ``$md5_e[2]`` decrypted successfully? */ is_string ($d) && strlen ($d)) |
| 173 |
/**/ |
| 174 |
if (strlen ($d = preg_replace ("/^~xe\|/", "", $d, 1, $xe)) && $xe) |
| 175 |
$d = /* Just re-assign this here. Nothing more to do. */ $d; |
| 176 |
else /* Else we need to empty this out. */ |
| 177 |
$d = /* Empty string. Invalid. */ ""; |
| 178 |
/**/ |
| 179 |
return (isset ($d) && is_string ($d) && strlen ($d)) ? ($string = $d) : ""; |
| 180 |
} |
| 181 |
else /* Otherwise we must fail here with an empty string value. */ |
| 182 |
return /* Just return an empty string in this case. */ ""; |
| 183 |
} |
| 184 |
} |
| 185 |
} |
| 186 |
?> |