PHP加密由javascript解密的例子
一般情况我们都是php加密再由php解密了,但是我的工作就碰到了必须由php加密然后由js来解密了,这种情况我找到一站长写的例子,非常的优秀下面我们一起来看看.
PHP加密函数,代码如下:
- <?php
-
- function strencode($string) {
- $string = base64_encode($string);
- $key = md5('just a test');
- $len = strlen($key);
- $code = '';
- for ($i = 0; $i < strlen($string); $i++) {
- $k = $i % $len;
- $code .= $string [$i] ^ $key [$k];
- }
- return base64_encode($code);
- }
-
- echo strencode('just a test');
- ?>
JS:解密,代码如下:
- <script src="md5.js"></script>
- <script src="base64.js"></script>
- <script>
-
- function strencode(string) {
- key =md5('just a test');
- string = Base64.decode(string);
- len = key.length;
- code = '';
- for (i = 0; i < string.length; i++) {
- k = i % len;
- code += String.fromCharCode(string.charCodeAt(i) ^ key.charCodeAt(k));
- }
- return Base64.decode(code);
- }
- alert(strencode('U1s1TFN3IQ0reTZbBgJlCA===='));
- </script>
js MD5,代码如下:
-
-
-
-
- var hexcase = 0;
- var b64pad = "";
-
-
-
-
-
- function md5(s) {
- return rstr2hex(rstr_md5(str2rstr_utf8(s)));
- }
- function b64_md5(s) {
- return rstr2b64(rstr_md5(str2rstr_utf8(s)));
- }
- function any_md5(s, e) {
- return rstr2any(rstr_md5(str2rstr_utf8(s)), e);
- }
- function hex_hmac_md5(k, d)
- {
- return rstr2hex(rstr_hmac_md5(str2rstr_utf8(k), str2rstr_utf8(d)));
- }
- function b64_hmac_md5(k, d)
- {
- return rstr2b64(rstr_hmac_md5(str2rstr_utf8(k), str2rstr_utf8(d)));
- }
- function any_hmac_md5(k, d, e)
- {
- return rstr2any(rstr_hmac_md5(str2rstr_utf8(k), str2rstr_utf8(d)), e);
- }
-
-
-
-
- function md5_vm_test()
- {
- return hex_md5("abc").toLowerCase() == "900150983cd24fb0d6963f7d28e17f72";
- }
-
-
-
-
- function rstr_md5(s)
- {
- return binl2rstr(binl_md5(rstr2binl(s), s.length * 8));
- }
-
-
-
-
- function rstr_hmac_md5(key, data)
- {
- var bkey = rstr2binl(key);
- if(bkey.length > 16) bkey = binl_md5(bkey, key.length * 8);
-
- var ipad = Array(16), opad = Array(16);
- for(var i = 0; i < 16; i++)
- {
- ipad[i] = bkey[i] ^ 0x36363636;
- opad[i] = bkey[i] ^ 0x5C5C5C5C;
- }
-
- var hash = binl_md5(ipad.concat(rstr2binl(data)), 512 + data.length * 8);
- return binl2rstr(binl_md5(opad.concat(hash), 512 + 128));
- }
-
-
-
-
- function rstr2hex(input)
- {
- try {
- hexcase
- } catch(e) {
- hexcase=0;
- }
- var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
- var output = "";
- var x;
- for(var i = 0; i < input.length; i++)
- {
- x = input.charCodeAt(i);
- output += hex_tab.charAt((x >>> 4) & 0x0F)
- + hex_tab.charAt( x & 0x0F);
- }
- return output;
- }
-
-
-
-
- function rstr2b64(input)
- {
- try {
- b64pad
- } catch(e) {
- b64pad='';
- }
- var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
- var output = "";
- var len = input.length;
- for(var i = 0; i < len; i += 3)
- {
- var triplet = (input.charCodeAt(i) << 16)
- | (i + 1 < len ? input.charCodeAt(i+1) << 8 : 0)
- | (i + 2 < len ? input.charCodeAt(i+2) : 0);
- for(var j = 0; j < 4; j++)
- {
- if(i * 8 + j * 6 > input.length * 8) output += b64pad;
- else output += tab.charAt((triplet >>> 6*(3-j)) & 0x3F);
- }
- }
- return output;
- }
-
-
-
-
- function rstr2any(input, encoding)
- {
- var divisor = encoding.length;
- var i, j, q, x, quotient;
-
-
- var dividend = Array(Math.ceil(input.length / 2));
- for(i = 0; i < dividend.length; i++)
- {
- dividend[i] = (input.charCodeAt(i * 2) << 8) | input.charCodeAt(i * 2 + 1);
- }
-
-
-
-
-
-
-
- var full_length = Math.ceil(input.length * 8 /
- (Math.log(encoding.length) / Math.log(2)));
- var remainders =&nb