贴心
全国7×24小时客服热线
安全
病毒杀除率高于99%
稳定
网站可用性高于99.9%
实力
服务68家上市企业及集团公司
点击数:12672015-07-20 11:34:25 来源: 外贸网站建设,深圳外贸网站建设,深圳网站建设,外贸商城网站制作-亿恩科技
一般情况我们都是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);
- }
- //开源代码vcphp.com
- 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,代码如下:
- /*
- * Configurable variables. You may need to tweak these to be compatible with
- * the server-side, but the defaults work in most cases.
- */
- var hexcase = 0; /* hex output format. 0 - lowercase; 1 - uppercase */
- var b64pad = ""; /* base-64 pad character. "=" for strict RFC compliance */
- /*
- * These are the functions you'll usually want to call
- * They take string arguments and return either hex or base-64 encoded strings
- */
- 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);
- }
- /*
- * Perform a simple self-test to see if the VM is working
- */
- function md5_vm_test()
- {
- return hex_md5("abc").toLowerCase() == "900150983cd24fb0d6963f7d28e17f72";
- }
- /*
- * Calculate the MD5 of a raw string
- */
- function rstr_md5(s)
- {
- return binl2rstr(binl_md5(rstr2binl(s), s.length * 8));
- }
- /*
- * Calculate the HMAC-MD5, of a key and some data (raw strings)
- */
- 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));
- }
- /*
- * Convert a raw string to a hex string
- */
- function rstr2hex(input)
- {
- try {
- hexcase
- } catch(e) {
- hexcase=0;
- }