PluginProbe
s2Member – Excellent for All Kinds of Memberships, Content Restriction Paywalls & Member Access Subscriptions / 110710
s2Member – Excellent for All Kinds of Memberships, Content Restriction Paywalls & Member Access Subscriptions v110710
260913 260909 260829 260814 260805 110710 110731 110812 110815 110912 110913 110915 110926 110927 111002 111003 111011 111017 111029 111105 111206 111216 111220 120213 120219 All 188 releases
s2member / includes / classes / utils-encryption.inc.php

utils-encryption.inc.php in s2Member – Excellent for All Kinds of Memberships, Content Restriction Paywalls & Member Access Subscriptions 110710, at includes/classes/utils-encryption.inc.php

171 lines 7.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 * RIJNDAEL 256: two-way encryption/decryption, with a URL-safe base64 wrapper.
32 *
33 * Includes a built-in fallback on XOR encryption when mcrypt is not available.
34 *
35 * @package s2Member\Utilities
36 * @since 3.5
37 *
38 * @param str $string A string of data to encrypt.
39 * @param str $key Optional. Key used for encryption.
40 * Defaults to the one configured for s2Member.
41 * Short of that, defaults to: ``wp_salt()``.
42 * @return str Encrypted string.
43 */
44 public static function encrypt ($string = FALSE, $key = FALSE)
45 {
46 $string = (is_string ($string)) ? $string : "";
47 /**/
48 $key = (!is_string ($key) || !strlen ($key)) ? $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["sec_encryption_key"] : $key;
49 $key = (!is_string ($key) || !strlen ($key)) ? wp_salt () : $key;
50 /**/
51 if (function_exists ("mcrypt_encrypt") && in_array ("rijndael-256", mcrypt_list_algorithms ()) && in_array ("cbc", mcrypt_list_modes ()))
52 {
53 $string = (strlen ($string)) ? "~r2|" . $string : "";
54 $key = substr ($key, 0, mcrypt_get_key_size (MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC));
55 $iv = c_ws_plugin__s2member_utils_strings::random_str_gen (mcrypt_get_iv_size (MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC), false);
56 $encrypted = mcrypt_encrypt (MCRYPT_RIJNDAEL_256, $key, $string, MCRYPT_MODE_CBC, $iv);
57 $encrypted = (strlen ($encrypted)) ? "~r2:" . $iv . "|" . $encrypted : "";
58 /**/
59 return ($base64 = str_replace (array ("+", "/", "="), array ("-", "_", "~"), base64_encode ($encrypted)));
60 }
61 else /* Fallback on XOR encryption. */
62 return c_ws_plugin__s2member_utils_encryption::xencrypt ($string, $key);
63 }
64 /**
65 * RIJNDAEL 256: two-way encryption/decryption, with a URL-safe base64 wrapper.
66 *
67 * Includes a built-in fallback on XOR encryption when mcrypt is not available.
68 *
69 * @package s2Member\Utilities
70 * @since 3.5
71 *
72 * @param str $base64 A string of data to decrypt. Should still be base64 encoded.
73 * @param str $key Optional. Key used originally for encryption.
74 * Defaults to the one configured for s2Member.
75 * Short of that, defaults to: ``wp_salt()``.
76 * @return str Decrypted string.
77 */
78 public static function decrypt ($base64 = FALSE, $key = FALSE)
79 {
80 $base64 = (is_string ($base64)) ? $base64 : "";
81 /**/
82 $key = (!is_string ($key) || !strlen ($key)) ? $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["sec_encryption_key"] : $key;
83 $key = (!is_string ($key) || !strlen ($key)) ? wp_salt () : $key;
84 /**/
85 $encrypted = base64_decode (str_replace (array ("-", "_", "~", "."), array ("+", "/", "=", "="), $base64));
86 /**/
87 if (function_exists ("mcrypt_decrypt") && in_array ("rijndael-256", mcrypt_list_algorithms ()) && in_array ("cbc", mcrypt_list_modes ()) && preg_match ("/^~r2\:(.+?)\|/", $encrypted, $v1))
88 {
89 $encrypted = preg_replace ("/^~r2\:(.+?)\|/", "", $encrypted);
90 $key = substr ($key, 0, mcrypt_get_key_size (MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC));
91 $decrypted = mcrypt_decrypt (MCRYPT_RIJNDAEL_256, $key, $encrypted, MCRYPT_MODE_CBC, $v1[1]);
92 $decrypted = preg_replace ("/^~r2\|/", "", $decrypted, 1, $v2);
93 $decrypted = ($v2) ? $decrypted : ""; /* Check validity. */
94 $decrypted = rtrim ($decrypted, "\0\4"); /* Nulls/EOTs. */
95 /**/
96 return ($string = $decrypted);
97 }
98 else /* Fallback on XOR decryption. */
99 return c_ws_plugin__s2member_utils_encryption::xdecrypt ($base64, $key);
100 }
101 /**
102 * XOR two-way encryption/decryption, with a base64 wrapper.
103 *
104 * @package s2Member\Utilities
105 * @since 3.5
106 *
107 * @param str $string A string of data to encrypt.
108 * @param str $key Optional. Key used for encryption.
109 * Defaults to the one configured for s2Member.
110 * Short of that, defaults to: ``wp_salt()``.
111 * @return str Encrypted string.
112 */
113 public static function xencrypt ($string = FALSE, $key = FALSE)
114 {
115 $string = (is_string ($string)) ? $string : "";
116 /**/
117 $key = (!is_string ($key) || !strlen ($key)) ? $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["sec_encryption_key"] : $key;
118 $key = (!is_string ($key) || !strlen ($key)) ? wp_salt () : $key;
119 /**/
120 $string = (strlen ($string)) ? "~xe|" . $string : "";
121 /**/
122 for ($i = 1, $encrypted = ""; $i <= strlen ($string); $i++)
123 {
124 $char = substr ($string, $i - 1, 1);
125 $keychar = substr ($key, ($i % strlen ($key)) - 1, 1);
126 $encrypted .= chr (ord ($char) + ord ($keychar));
127 }
128 /**/
129 $encrypted = (strlen ($encrypted)) ? "~xe|" . $encrypted : "";
130 /**/
131 return ($base64 = str_replace (array ("+", "/", "="), array ("-", "_", "~"), base64_encode ($encrypted)));
132 }
133 /**
134 * XOR two-way encryption/decryption, with a base64 wrapper.
135 *
136 * @package s2Member\Utilities
137 * @since 3.5
138 *
139 * @param str $base64 A string of data to decrypt. Should still be base64 encoded.
140 * @param str $key Optional. Key used originally for encryption.
141 * Defaults to the one configured for s2Member.
142 * Short of that, defaults to: ``wp_salt()``.
143 * @return str Decrypted string.
144 */
145 public static function xdecrypt ($base64 = FALSE, $key = FALSE)
146 {
147 $base64 = (is_string ($base64)) ? $base64 : "";
148 /**/
149 $key = (!is_string ($key) || !strlen ($key)) ? $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["sec_encryption_key"] : $key;
150 $key = (!is_string ($key) || !strlen ($key)) ? wp_salt () : $key;
151 /**/
152 $encrypted = base64_decode (str_replace (array ("-", "_", "~", "."), array ("+", "/", "=", "="), $base64));
153 /**/
154 $encrypted = preg_replace ("/^~xe\|/", "", $encrypted, 1, $v1);
155 $encrypted = ($v1) ? $encrypted : ""; /* Check validity. */
156 /**/
157 for ($i = 1, $decrypted = ""; $i <= strlen ($encrypted); $i++)
158 {
159 $char = substr ($encrypted, $i - 1, 1);
160 $keychar = substr ($key, ($i % strlen ($key)) - 1, 1);
161 $decrypted .= chr (ord ($char) - ord ($keychar));
162 }
163 /**/
164 $decrypted = preg_replace ("/^~xe\|/", "", $decrypted, 1, $v2);
165 $decrypted = ($v2) ? $decrypted : ""; /* Check validity. */
166 /**/
167 return ($string = $decrypted);
168 }
169 }
170 }
171 ?>