1. 程式人生 > >Javascript實現base64的加密解密【轉】

Javascript實現base64的加密解密【轉】

場景

這幾天使用PHP向前端傳值的時候,遇到一個問題,要將程式碼傳過去賦值。如果使用urlencode()和urldecode()函式,就會出現js無法解碼的情況,因為php和js的相關函式演算法不一致。於是準備使用通用的base64加密解密。

javascript base64 加密解密

  下方是base64用javascript寫出來的函式和方法。

  1 //1.加密解密方法使用:
  2 
  3 //1.加密  
  4 var str = '124中文內容';  
  5 var base = new Base64();  
  6 var result = base.encode(str);  
7 document.write(result); 8 9 //2.解密 10 var result2 = base.decode(result); 11 document.write(result2); 12 //2.加密、解密演算法封裝: 13 14 function Base64() { 15 16 // private property 17 _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; 18
19 // public method for encoding 20 this.encode = function (input) { 21 var output = ""; 22 var chr1, chr2, chr3, enc1, enc2, enc3, enc4; 23 var i = 0; 24 input = _utf8_encode(input); 25 while (i < input.length) { 26 chr1 = input.charCodeAt(i++);
27 chr2 = input.charCodeAt(i++); 28 chr3 = input.charCodeAt(i++); 29 enc1 = chr1 >> 2; 30 enc2 = ((chr1 & 3) << 4) | (chr2 >> 4); 31 enc3 = ((chr2 & 15) << 2) | (chr3 >> 6); 32 enc4 = chr3 & 63; 33 if (isNaN(chr2)) { 34 enc3 = enc4 = 64; 35 } else if (isNaN(chr3)) { 36 enc4 = 64; 37 } 38 output = output + 39 _keyStr.charAt(enc1) + _keyStr.charAt(enc2) + 40 _keyStr.charAt(enc3) + _keyStr.charAt(enc4); 41 } 42 return output; 43 } 44 45 // public method for decoding 46 this.decode = function (input) { 47 var output = ""; 48 var chr1, chr2, chr3; 49 var enc1, enc2, enc3, enc4; 50 var i = 0; 51 input = input.replace(/[^A-Za-z0-9\+\/\=]/g, ""); 52 while (i < input.length) { 53 enc1 = _keyStr.indexOf(input.charAt(i++)); 54 enc2 = _keyStr.indexOf(input.charAt(i++)); 55 enc3 = _keyStr.indexOf(input.charAt(i++)); 56 enc4 = _keyStr.indexOf(input.charAt(i++)); 57 chr1 = (enc1 << 2) | (enc2 >> 4); 58 chr2 = ((enc2 & 15) << 4) | (enc3 >> 2); 59 chr3 = ((enc3 & 3) << 6) | enc4; 60 output = output + String.fromCharCode(chr1); 61 if (enc3 != 64) { 62 output = output + String.fromCharCode(chr2); 63 } 64 if (enc4 != 64) { 65 output = output + String.fromCharCode(chr3); 66 } 67 } 68 output = _utf8_decode(output); 69 return output; 70 } 71 72 // private method for UTF-8 encoding 73 _utf8_encode = function (string) { 74 string = string.replace(/\r\n/g,"\n"); 75 var utftext = ""; 76 for (var n = 0; n < string.length; n++) { 77 var c = string.charCodeAt(n); 78 if (c < 128) { 79 utftext += String.fromCharCode(c); 80 } else if((c > 127) && (c < 2048)) { 81 utftext += String.fromCharCode((c >> 6) | 192); 82 utftext += String.fromCharCode((c & 63) | 128); 83 } else { 84 utftext += String.fromCharCode((c >> 12) | 224); 85 utftext += String.fromCharCode(((c >> 6) & 63) | 128); 86 utftext += String.fromCharCode((c & 63) | 128); 87 } 88 89 } 90 return utftext; 91 } 92 93 // private method for UTF-8 decoding 94 _utf8_decode = function (utftext) { 95 var string = ""; 96 var i = 0; 97 var c = c1 = c2 = 0; 98 while ( i < utftext.length ) { 99 c = utftext.charCodeAt(i); 100 if (c < 128) { 101 string += String.fromCharCode(c); 102 i++; 103 } else if((c > 191) && (c < 224)) { 104 c2 = utftext.charCodeAt(i+1); 105 string += String.fromCharCode(((c & 31) << 6) | (c2 & 63)); 106 i += 2; 107 } else { 108 c2 = utftext.charCodeAt(i+1); 109 c3 = utftext.charCodeAt(i+2); 110 string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63)); 111 i += 3; 112 } 113 } 114 return string; 115 } 116 }

 

 

完全引用自

---------------------
作者:夜之寐
來源:CSDN
原文:https://blog.csdn.net/u012369749/article/details/73784897
版權宣告:本文為博主原創文章,轉載請附上博文連結!