php前后台登录交互明文传输使用base64加密

3 base64加解密
base64是通用的加解密方法,JavaScript也有现成的库可以使用,PHP更是有现成函数可用。
解决方法如下。
首先,前端引用base64加密库,推荐用支持中文的库:https://github.com/emn178/hi-base64
数据提交之前,用base64的方法加密:
var html_source = base64.encode(html_source)这样不论什么字符,都可以提交了,都是。
然后,在PHP读取数据时,用base64_decode() 方法就可以解密了。
$html_source = str_replace(' ', '+', $html_source);
$html_source = base64_decode($html_source);

下面是base64.js文件的内容

  1 /*
  2  * [hi-base64]{@link https://github.com/emn178/hi-base64}
  3  *
  4  * @version 0.2.1
  5  * @author Chen, Yi-Cyuan [emn178@gmail.com]
  6  * @copyright Chen, Yi-Cyuan 2014-2017
  7  * @license MIT
  8  */
  9 /*jslint bitwise: true */
 10 (function () {
 11     'use strict';
 12 
 13     var root = typeof window === 'object' ? window : {};
 14     var NODE_JS = !root.HI_BASE64_NO_NODE_JS && typeof process === 'object' && process.versions && process.versions.node;
 15     if (NODE_JS) {
 16         root = global;
 17     }
 18     var COMMON_JS = !root.HI_BASE64_NO_COMMON_JS && typeof module === 'object' && module.exports;
 19     var AMD = typeof define === 'function' && define.amd;
 20     var BASE64_ENCODE_CHAR = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'.split('');
 21     var BASE64_DECODE_CHAR = {
 22         'A': 0, 'B': 1, 'C': 2, 'D': 3, 'E': 4, 'F': 5, 'G': 6, 'H': 7, 'I': 8,
 23         'J': 9, 'K': 10, 'L': 11, 'M': 12, 'N': 13, 'O': 14, 'P': 15, 'Q': 16,
 24         'R': 17, 'S': 18, 'T': 19, 'U': 20, 'V': 21, 'W': 22, 'X': 23, 'Y': 24,
 25         'Z': 25, 'a': 26, 'b': 27, 'c': 28, 'd': 29, 'e': 30, 'f': 31, 'g': 32,
 26         'h': 33, 'i': 34, 'j': 35, 'k': 36, 'l': 37, 'm': 38, 'n': 39, 'o': 40,
 27         'p': 41, 'q': 42, 'r': 43, 's': 44, 't': 45, 'u': 46, 'v': 47, 'w': 48,
 28         'x': 49, 'y': 50, 'z': 51, '0': 52, '1': 53, '2': 54, '3': 55, '4': 56,
 29         '5': 57, '6': 58, '7': 59, '8': 60, '9': 61, '+': 62, '/': 63, '-': 62,
 30         '_': 63
 31     };
 32 
 33     var utf8ToBytes = function (str) {
 34         var bytes = [];
 35         for (var i = 0; i < str.length; i++) {
 36             var c = str.charCodeAt(i);
 37             if (c < 0x80) {
 38                 bytes[bytes.length] = c;
 39             } else if (c < 0x800) {
 40                 bytes[bytes.length] = 0xc0 | (c >> 6);
 41                 bytes[bytes.length] = 0x80 | (c & 0x3f);
 42             } else if (c < 0xd800 || c >= 0xe000) {
 43                 bytes[bytes.length] = 0xe0 | (c >> 12);
 44                 bytes[bytes.length] = 0x80 | ((c >> 6) & 0x3f);
 45                 bytes[bytes.length] = 0x80 | (c & 0x3f);
 46             } else {
 47                 c = 0x10000 + (((c & 0x3ff) << 10) | (str.charCodeAt(++i) & 0x3ff));
 48                 bytes[bytes.length] = 0xf0 | (c >> 18);
 49                 bytes[bytes.length] = 0x80 | ((c >> 12) & 0x3f);
 50                 bytes[bytes.length] = 0x80 | ((c >> 6) & 0x3f);
 51                 bytes[bytes.length] = 0x80 | (c & 0x3f);
 52             }
 53         }
 54         return bytes;
 55     };
 56 
 57     var decodeAsBytes = function (base64Str) {
 58         var v1, v2, v3, v4, bytes = [], index = 0, length = base64Str.length;
 59         if (base64Str.charAt(length - 2) === '=') {
 60             length -= 2;
 61         } else if (base64Str.charAt(length - 1) === '=') {
 62             length -= 1;
 63         }
 64 
 65         // 4 char to 3 bytes
 66         for (var i = 0, count = length >> 2 << 2; i < count;) {
 67             v1 = BASE64_DECODE_CHAR[base64Str.charAt(i++)];
 68             v2 = BASE64_DECODE_CHAR[base64Str.charAt(i++)];
 69             v3 = BASE64_DECODE_CHAR[base64Str.charAt(i++)];
 70             v4 = BASE64_DECODE_CHAR[base64Str.charAt(i++)];
 71             bytes[index++] = (v1 << 2 | v2 >>> 4) & 255;
 72             bytes[index++] = (v2 << 4 | v3 >>> 2) & 255;
 73             bytes[index++] = (v3 << 6 | v4) & 255;
 74         }
 75 
 76         // remain bytes
 77         var remain = length - count;
 78         if (remain === 2) {
 79             v1 = BASE64_DECODE_CHAR[base64Str.charAt(i++)];
 80             v2 = BASE64_DECODE_CHAR[base64Str.charAt(i++)];
 81             bytes[index++] = (v1 << 2 | v2 >>> 4) & 255;
 82         } else if (remain === 3) {
 83             v1 = BASE64_DECODE_CHAR[base64Str.charAt(i++)];
 84             v2 = BASE64_DECODE_CHAR[base64Str.charAt(i++)];
 85             v3 = BASE64_DECODE_CHAR[base64Str.charAt(i++)];
 86             bytes[index++] = (v1 << 2 | v2 >>> 4) & 255;
 87             bytes[index++] = (v2 << 4 | v3 >>> 2) & 255;
 88         }
 89         return bytes;
 90     };
 91 
 92     var encodeFromBytes = function (bytes) {
 93         var v1, v2, v3, base64Str = '', length = bytes.length;
 94         for (var i = 0, count = parseInt(length / 3) * 3; i < count;) {
 95             v1 = bytes[i++];
 96             v2 = bytes[i++];
 97             v3 = bytes[i++];
 98             base64Str += BASE64_ENCODE_CHAR[v1 >>> 2] +
 99                 BASE64_ENCODE_CHAR[(v1 << 4 | v2 >>> 4) & 63] +
100                 BASE64_ENCODE_CHAR[(v2 << 2 | v3 >>> 6) & 63] +
101                 BASE64_ENCODE_CHAR[v3 & 63];
102         }
103 
104         // remain char
105         var remain = length - count;
106         if (remain === 1) {
107             v1 = bytes[i];
108             base64Str += BASE64_ENCODE_CHAR[v1 >>> 2] +
109                 BASE64_ENCODE_CHAR[(v1 << 4) & 63] +
110                 '==';
111         } else if (remain === 2) {
112             v1 = bytes[i++];
113             v2 = bytes[i];
114             base64Str += BASE64_ENCODE_CHAR[v1 >>> 2] +
115                 BASE64_ENCODE_CHAR[(v1 << 4 | v2 >>> 4) & 63] +
116                 BASE64_ENCODE_CHAR[(v2 << 2) & 63] +
117                 '=';
118         }
119         return base64Str;
120     };
121 
122     var btoa = root.btoa, atob = root.atob, utf8Base64Encode, utf8Base64Decode;
123     if (NODE_JS) {
124         var Buffer = require('buffer').Buffer;
125         btoa = function (str) {
126             return new Buffer(str, 'ascii').toString('base64');
127         };
128 
129         utf8Base64Encode = function (str) {
130             return new Buffer(str).toString('base64');
131         };
132 
133         encodeFromBytes = utf8Base64Encode;
134 
135         atob = function (base64Str) {
136             return new Buffer(base64Str, 'base64').toString('ascii');
137         };
138 
139         utf8Base64Decode = function (base64Str) {
140             return new Buffer(base64Str, 'base64').toString();
141         };
142     } else if (!btoa) {
143         btoa = function (str) {
144             var v1, v2, v3, base64Str = '', length = str.length;
145             for (var i = 0, count = parseInt(length / 3) * 3; i < count;) {
146                 v1 = str.charCodeAt(i++);
147                 v2 = str.charCodeAt(i++);
148                 v3 = str.charCodeAt(i++);
149                 base64Str += BASE64_ENCODE_CHAR[v1 >>> 2] +
150                     BASE64_ENCODE_CHAR[(v1 << 4 | v2 >>> 4) & 63] +
151                     BASE64_ENCODE_CHAR[(v2 << 2 | v3 >>> 6) & 63] +
152                     BASE64_ENCODE_CHAR[v3 & 63];
153             }
154 
155             // remain char
156             var remain = length - count;
157             if (remain === 1) {
158                 v1 = str.charCodeAt(i);
159                 base64Str += BASE64_ENCODE_CHAR[v1 >>> 2] +
160                     BASE64_ENCODE_CHAR[(v1 << 4) & 63] +
161                     '==';
162             } else if (remain === 2) {
163                 v1 = str.charCodeAt(i++);
164                 v2 = str.charCodeAt(i);
165                 base64Str += BASE64_ENCODE_CHAR[v1 >>> 2] +
166                     BASE64_ENCODE_CHAR[(v1 << 4 | v2 >>> 4) & 63] +
167                     BASE64_ENCODE_CHAR[(v2 << 2) & 63] +
168                     '=';
169             }
170             return base64Str;
171         };
172 
173         utf8Base64Encode = function (str) {
174             var v1, v2, v3, base64Str = '', bytes = utf8ToBytes(str), length = bytes.length;
175             for (var i = 0, count = parseInt(length / 3) * 3; i < count;) {
176                 v1 = bytes[i++];
177                 v2 = bytes[i++];
178                 v3 = bytes[i++];
179                 base64Str += BASE64_ENCODE_CHAR[v1 >>> 2] +
180                     BASE64_ENCODE_CHAR[(v1 << 4 | v2 >>> 4) & 63] +
181                     BASE64_ENCODE_CHAR[(v2 << 2 | v3 >>> 6) & 63] +
182                     BASE64_ENCODE_CHAR[v3 & 63];
183             }
184 
185             // remain char
186             var remain = length - count;
187             if (remain === 1) {
188                 v1 = bytes[i];
189                 base64Str += BASE64_ENCODE_CHAR[v1 >>> 2] +
190                     BASE64_ENCODE_CHAR[(v1 << 4) & 63] +
191                     '==';
192             } else if (remain === 2) {
193                 v1 = bytes[i++];
194                 v2 = bytes[i];
195                 base64Str += BASE64_ENCODE_CHAR[v1 >>> 2] +
196                     BASE64_ENCODE_CHAR[(v1 << 4 | v2 >>> 4) & 63] +
197                     BASE64_ENCODE_CHAR[(v2 << 2) & 63] +
198                     '=';
199             }
200             return base64Str;
201         };
202 
203         atob = function (base64Str) {
204             var v1, v2, v3, v4, str = '', length = base64Str.length;
205             if (base64Str.charAt(length - 2) === '=') {
206                 length -= 2;
207             } else if (base64Str.charAt(length - 1) === '=') {
208                 length -= 1;
209             }
210 
211             // 4 char to 3 bytes
212             for (var i = 0, count = length >> 2 << 2; i < count;) {
213                 v1 = BASE64_DECODE_CHAR[base64Str.charAt(i++)];
214                 v2 = BASE64_DECODE_CHAR[base64Str.charAt(i++)];
215                 v3 = BASE64_DECODE_CHAR[base64Str.charAt(i++)];
216                 v4 = BASE64_DECODE_CHAR[base64Str.charAt(i++)];
217                 str += String.fromCharCode((v1 << 2 | v2 >>> 4) & 255) +
218                     String.fromCharCode((v2 << 4 | v3 >>> 2) & 255) +
219                     String.fromCharCode((v3 << 6 | v4) & 255);
220             }
221 
222             // remain bytes
223             var remain = length - count;
224             if (remain === 2) {
225                 v1 = BASE64_DECODE_CHAR[base64Str.charAt(i++)];
226                 v2 = BASE64_DECODE_CHAR[base64Str.charAt(i++)];
227                 str += String.fromCharCode((v1 << 2 | v2 >>> 4) & 255);
228             } else if (remain === 3) {
229                 v1 = BASE64_DECODE_CHAR[base64Str.charAt(i++)];
230                 v2 = BASE64_DECODE_CHAR[base64Str.charAt(i++)];
231                 v3 = BASE64_DECODE_CHAR[base64Str.charAt(i++)];
232                 str += String.fromCharCode((v1 << 2 | v2 >>> 4) & 255) +
233                     String.fromCharCode((v2 << 4 | v3 >>> 2) & 255);
234             }
235             return str;
236         };
237 
238         utf8Base64Decode = function (base64Str) {
239             var str = '', bytes = decodeAsBytes(base64Str), length = bytes.length;
240             var i = 0, followingChars = 0, b, c;
241             while (i < length) {
242                 b = bytes[i++];
243                 if (b <= 0x7F) {
244                     str += String.fromCharCode(b);
245                     continue;
246                 } else if (b > 0xBF && b <= 0xDF) {
247                     c = b & 0x1F;
248                     followingChars = 1;
249                 } else if (b <= 0xEF) {
250                     c = b & 0x0F;
251                     followingChars = 2;
252                 } else if (b <= 0xF7) {
253                     c = b & 0x07;
254                     followingChars = 3;
255                 } else {
256                     throw 'not a UTF-8 string';
257                 }
258 
259                 for (var j = 0; j < followingChars; ++j) {
260                     b = bytes[i++];
261                     if (b < 0x80 || b > 0xBF) {
262                         throw 'not a UTF-8 string';
263                     }
264                     c <<= 6;
265                     c += b & 0x3F;
266                 }
267                 if (c >= 0xD800 && c <= 0xDFFF) {
268                     throw 'not a UTF-8 string';
269                 }
270                 if (c > 0x10FFFF) {
271                     throw 'not a UTF-8 string';
272                 }
273 
274                 if (c <= 0xFFFF) {
275                     str += String.fromCharCode(c);
276                 } else {
277                     c -= 0x10000;
278                     str += String.fromCharCode((c >> 10) + 0xD800);
279                     str += String.fromCharCode((c & 0x3FF) + 0xDC00);
280                 }
281             }
282             return str;
283         };
284     } else {
285         utf8Base64Encode = function (str) {
286             var result = '';
287             for (var i = 0; i < str.length; i++) {
288                 var charcode = str.charCodeAt(i);
289                 if (charcode < 0x80) {
290                     result += String.fromCharCode(charcode);
291                 } else if (charcode < 0x800) {
292                     result += String.fromCharCode(0xc0 | (charcode >> 6)) +
293                         String.fromCharCode(0x80 | (charcode & 0x3f));
294                 } else if (charcode < 0xd800 || charcode >= 0xe000) {
295                     result += String.fromCharCode(0xe0 | (charcode >> 12)) +
296                         String.fromCharCode(0x80 | ((charcode >> 6) & 0x3f)) +
297                         String.fromCharCode(0x80 | (charcode & 0x3f));
298                 } else {
299                     charcode = 0x10000 + (((charcode & 0x3ff) << 10) | (str.charCodeAt(++i) & 0x3ff));
300                     result += String.fromCharCode(0xf0 | (charcode >> 18)) +
301                         String.fromCharCode(0x80 | ((charcode >> 12) & 0x3f)) +
302                         String.fromCharCode(0x80 | ((charcode >> 6) & 0x3f)) +
303                         String.fromCharCode(0x80 | (charcode & 0x3f));
304                 }
305             }
306             return btoa(result);
307         };
308 
309         utf8Base64Decode = function (base64Str) {
310             var tmpStr = atob(base64Str.trim('=').replace(/-/g, '+').replace(/_/g, '/'));
311             if (!/[^\x00-\x7F]/.test(tmpStr)) {
312                 return tmpStr;
313             }
314             var str = '', i = 0, length = tmpStr.length, followingChars = 0, b, c;
315             while (i < length) {
316                 b = tmpStr.charCodeAt(i++);
317                 if (b <= 0x7F) {
318                     str += String.fromCharCode(b);
319                     continue;
320                 } else if (b > 0xBF && b <= 0xDF) {
321                     c = b & 0x1F;
322                     followingChars = 1;
323                 } else if (b <= 0xEF) {
324                     c = b & 0x0F;
325                     followingChars = 2;
326                 } else if (b <= 0xF7) {
327                     c = b & 0x07;
328                     followingChars = 3;
329                 } else {
330                     throw 'not a UTF-8 string';
331                 }
332 
333                 for (var j = 0; j < followingChars; ++j) {
334                     b = tmpStr.charCodeAt(i++);
335                     if (b < 0x80 || b > 0xBF) {
336                         throw 'not a UTF-8 string';
337                     }
338                     c <<= 6;
339                     c += b & 0x3F;
340                 }
341                 if (c >= 0xD800 && c <= 0xDFFF) {
342                     throw 'not a UTF-8 string';
343                 }
344                 if (c > 0x10FFFF) {
345                     throw 'not a UTF-8 string';
346                 }
347 
348                 if (c <= 0xFFFF) {
349                     str += String.fromCharCode(c);
350                 } else {
351                     c -= 0x10000;
352                     str += String.fromCharCode((c >> 10) + 0xD800);
353                     str += String.fromCharCode((c & 0x3FF) + 0xDC00);
354                 }
355             }
356             return str;
357         };
358     }
359 
360     var encode = function (str, asciiOnly) {
361         var notString = typeof(str) != 'string';
362         if (notString && str.constructor === root.ArrayBuffer) {
363             str = new Uint8Array(str);
364         }
365         if (notString) {
366             return encodeFromBytes(str);
367         } else {
368             if (!asciiOnly && /[^\x00-\x7F]/.test(str)) {
369                 return utf8Base64Encode(str);
370             } else {
371                 return btoa(str);
372             }
373         }
374     };
375 
376     var decode = function (base64Str, asciiOnly) {
377         return asciiOnly ? atob(base64Str) : utf8Base64Decode(base64Str);
378     };
379 
380     var exports = {
381         encode: encode,
382         decode: decode,
383         atob: atob,
384         btoa: btoa
385     };
386     decode.bytes = decodeAsBytes;
387     decode.string = decode;
388 
389     if (COMMON_JS) {
390         module.exports = exports;
391     } else {
392         root.base64 = exports;
393         if(AMD) {
394             define(function() {
395                 return exports;
396             });
397         }
398     }
399 })();

 

posted @ 2020-03-19 11:58  thomas张  阅读(1058)  评论(0编辑  收藏  举报