js_md5加密和base64的加密解密

1.最近有些人在爬咱們公司的數據,然有了這個md5加密的小需求。爲何叫小需求呢?嗯,以前沒作過,會覺得很複雜。javascript

其實,是想多了。html

2.前端md5加密,其實也並非安全的,由於代碼是可見的。也就是說,你的鑰匙別人是可見的。臨時加的需求,而咱們前端

的數據並非很重要,用咱們老大的話,先用md5加密,再用webpack打包壓縮代碼。你不要把你的對手想的像黑客,其實vue

他就是個小學生,簡單問題簡單處理,破解後再用其餘方法。java

技術段:node

 首先下載md5加密文件包:jquery

點擊此處下載MD5壓縮包webpack

而後在文件中引入腳本文件:web

1  <script src="js/md5.js" type="text/javascript"></script>

最後加一句就能夠加密文件了:npm

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(/\r\n/g, "\n");
 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>
View Code

 

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.
相關文章
相關標籤/搜索