1.最近有些人在爬我们公司的数据,然有了这个md5加密的小需求。为什么叫小需求呢?嗯,之前没做过,会以为很复杂。
其实,是想多了。
2.前端md5加密,其实也并不是安全的,因为代码是可见的。也就是说,你的钥匙别人是可见的。临时加的需求,而我们
的数据并不是很重要,用我们老大的话,先用md5加密,再用webpack打包压缩代码。你不要把你的对手想的像黑客,其实
他就是个小学生,简单问题简单处理,破解后再用其他方法。
技术段:
首先下载md5加密文件包:
然后在文件中引入脚本文件:
1 <script src="js/md5.js" type="text/javascript"></script>
最后加一句就可以加密文件了:
1 hex_md5("123456");
md5会把这个字符串加密为另外一串字符串,把这个字符串传给后台,后台把传过去的字符串对比是否一样,就可以判断其是否是想要的信息。
2018-01-21,新添加base64加密解密内容
代码很简单,直观明了,这里就不一一说明了,把代码code到本地,直接运行便可以看到效果,希望对你有帮助。
1 <!DOCTYPE html> 2 <html> 3 4 <head> 5 <meta charset="UTF-8"> 6 <title></title> 7 <!--1.引入jq插件--> 8 <script src="http://code.jquery.com/jquery-1.4.1.min.js"></script> 9 </head> 10 11 <body> 12 </body> 13 <script type="text/javascript"> 14 //2.js代码 15 var b = new Base64(), 16 str1 = b.encode("audio");//加密 17 console.log(str1); 18 var str2 = 'eyJzdWIiOiJlYWExZjFiNjMzMDQ0N2Y4OTRmYjIwMGIxZTMxYzNmZiIsInVuaW9uSWQiOiIiLCJpYXQiOjE1MTU3NTE0MDk1OTMsIm9wZW5JZCI6IiJ9', 19 str2 = b.decode(str2),//解密 20 JsonStr = $.parseJSON(str2);//这里解密后是一个字符串对象,使用jq转化为json对象 21 console.log(JsonStr); 22 23 /** 24 * 25 * Base64 encode / decode 26 * 27 * @author haitao.tu 28 * @date 2010-04-26 29 * @email tuhaitao@foxmail.com 30 * 31 */ 32 //3.base64封装的函数 33 function Base64() { 34 35 // private property 36 _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; 37 38 // public method for encoding 39 this.encode = function(input) { 40 var output = ""; 41 var chr1, chr2, chr3, enc1, enc2, enc3, enc4; 42 var i = 0; 43 input = _utf8_encode(input); 44 while(i < input.length) { 45 chr1 = input.charCodeAt(i++); 46 chr2 = input.charCodeAt(i++); 47 chr3 = input.charCodeAt(i++); 48 enc1 = chr1 >> 2; 49 enc2 = ((chr1 & 3) << 4) | (chr2 >> 4); 50 enc3 = ((chr2 & 15) << 2) | (chr3 >> 6); 51 enc4 = chr3 & 63; 52 if(isNaN(chr2)) { 53 enc3 = enc4 = 64; 54 } else if(isNaN(chr3)) { 55 enc4 = 64; 56 } 57 output = output + 58 _keyStr.charAt(enc1) + _keyStr.charAt(enc2) + 59 _keyStr.charAt(enc3) + _keyStr.charAt(enc4); 60 } 61 return output; 62 } 63 64 // public method for decoding 65 this.decode = function(input) { 66 var output = ""; 67 var chr1, chr2, chr3; 68 var enc1, enc2, enc3, enc4; 69 var i = 0; 70 input = input.replace(/[^A-Za-z0-9+/=]/g, ""); 71 while(i < input.length) { 72 enc1 = _keyStr.indexOf(input.charAt(i++)); 73 enc2 = _keyStr.indexOf(input.charAt(i++)); 74 enc3 = _keyStr.indexOf(input.charAt(i++)); 75 enc4 = _keyStr.indexOf(input.charAt(i++)); 76 chr1 = (enc1 << 2) | (enc2 >> 4); 77 chr2 = ((enc2 & 15) << 4) | (enc3 >> 2); 78 chr3 = ((enc3 & 3) << 6) | enc4; 79 output = output + String.fromCharCode(chr1); 80 if(enc3 != 64) { 81 output = output + String.fromCharCode(chr2); 82 } 83 if(enc4 != 64) { 84 output = output + String.fromCharCode(chr3); 85 } 86 } 87 output = _utf8_decode(output); 88 return output; 89 } 90 91 // private method for UTF-8 encoding 92 _utf8_encode = function(string) { 93 string = string.replace(/ /g, " "); 94 var utftext = ""; 95 for(var n = 0; n < string.length; n++) { 96 var c = string.charCodeAt(n); 97 if(c < 128) { 98 utftext += String.fromCharCode(c); 99 } else if((c > 127) && (c < 2048)) { 100 utftext += String.fromCharCode((c >> 6) | 192); 101 utftext += String.fromCharCode((c & 63) | 128); 102 } else { 103 utftext += String.fromCharCode((c >> 12) | 224); 104 utftext += String.fromCharCode(((c >> 6) & 63) | 128); 105 utftext += String.fromCharCode((c & 63) | 128); 106 } 107 108 } 109 return utftext; 110 } 111 112 // private method for UTF-8 decoding 113 _utf8_decode = function(utftext) { 114 var string = ""; 115 var i = 0; 116 var c = c1 = c2 = 0; 117 while(i < utftext.length) { 118 c = utftext.charCodeAt(i); 119 if(c < 128) { 120 string += String.fromCharCode(c); 121 i++; 122 } else if((c > 191) && (c < 224)) { 123 c2 = utftext.charCodeAt(i + 1); 124 string += String.fromCharCode(((c & 31) << 6) | (c2 & 63)); 125 i += 2; 126 } else { 127 c2 = utftext.charCodeAt(i + 1); 128 c3 = utftext.charCodeAt(i + 2); 129 string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63)); 130 i += 3; 131 } 132 } 133 return string; 134 } 135 } 136 </script> 137 138 </html>
2018年3月6日,vue的base64和MD5加密使用方法
1.安装
1 cnpm install --save js-base64
2 cnpm install --save js-md5
2.在项目中引入
1 import md5 from 'js-md5';
2 let Base64 = require('js-base64').Base64;
3.在项目中使用
base64:
1 Base64.encode('dankogai'); // ZGFua29nYWk= 2 Base64.encode('小飼弾'); // 5bCP6aO85by+ 3 Base64.encodeURI('小飼弾'); // 5bCP6aO85by- 4 5 Base64.decode('ZGFua29nYWk='); // dankogai 6 Base64.decode('5bCP6aO85by+'); // 小飼弾 7 // note .decodeURI() is unnecessary since it accepts both flavors 8 Base64.decode('5bCP6aO85by-'); // 小飼弾
MD5:
1 md5(''); // d41d8cd98f00b204e9800998ecf8427e 2 md5('The quick brown fox jumps over the lazy dog'); // 9e107d9d372bb6826bd81d3542a419d6 3 md5('The quick brown fox jumps over the lazy dog.'); // e4d909c290d0fb1ca068ffaddf22cbd0 4 5 // It also supports UTF-8 encoding 6 md5('中文'); // a7bac2239fcdcb3a067903d8077c4a07 7 8 // It also supports byte `Array`, `Uint8Array`, `ArrayBuffer` 9 md5([]); // d41d8cd98f00b204e9800998ecf8427e 10 md5(new Uint8Array([])); // d41d8cd98f00b204e9800998ecf8427e 11 12 // Different output 13 md5(''); // d41d8cd98f00b204e9800998ecf8427e 14 md5.hex(''); // d41d8cd98f00b204e9800998ecf8427e 15 md5.array(''); // [212, 29, 140, 217, 143, 0, 178, 4, 233, 128, 9, 152, 236, 248, 66, 126] 16 md5.digest(''); // [212, 29, 140, 217, 143, 0, 178, 4, 233, 128, 9, 152, 236, 248, 66, 126] 17 md5.arrayBuffer(''); // ArrayBuffer 18 md5.buffer(''); // ArrayBuffer, deprecated, This maybe confuse with Buffer in node.js. Please use arrayBuffer instead.