Javascript实现base64的加密解密方法
1 function Base64() { 2 // private property 3 _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; 4 // 公共编码方法 5 this.encode = function (input) { 6 var output = ""; 7 var chr1, chr2, chr3, enc1, enc2, enc3, enc4; 8 var i = 0; 9 input = _utf8_encode(input); 10 while (i < input.length) { 11 chr1 = input.charCodeAt(i++); 12 chr2 = input.charCodeAt(i++); 13 chr3 = input.charCodeAt(i++); 14 enc1 = chr1 >> 2; 15 enc2 = ((chr1 & 3) << 4) | (chr2 >> 4); 16 enc3 = ((chr2 & 15) << 2) | (chr3 >> 6); 17 enc4 = chr3 & 63; 18 if (isNaN(chr2)) { 19 enc3 = enc4 = 64; 20 } else if (isNaN(chr3)) { 21 enc4 = 64; 22 } 23 output = output + 24 _keyStr.charAt(enc1) + _keyStr.charAt(enc2) + 25 _keyStr.charAt(enc3) + _keyStr.charAt(enc4); 26 } 27 return output; 28 } 29 30 // 公共解码方法 31 this.decode = function (input) { 32 var output = ""; 33 var chr1, chr2, chr3; 34 var enc1, enc2, enc3, enc4; 35 var i = 0; 36 input = input.replace(/[^A-Za-z0-9\+\/\=]/g, ""); 37 while (i < input.length) { 38 enc1 = _keyStr.indexOf(input.charAt(i++)); 39 enc2 = _keyStr.indexOf(input.charAt(i++)); 40 enc3 = _keyStr.indexOf(input.charAt(i++)); 41 enc4 = _keyStr.indexOf(input.charAt(i++)); 42 chr1 = (enc1 << 2) | (enc2 >> 4); 43 chr2 = ((enc2 & 15) << 4) | (enc3 >> 2); 44 chr3 = ((enc3 & 3) << 6) | enc4; 45 output = output + String.fromCharCode(chr1); 46 if (enc3 != 64) { 47 output = output + String.fromCharCode(chr2); 48 } 49 if (enc4 != 64) { 50 output = output + String.fromCharCode(chr3); 51 } 52 } 53 output = _utf8_decode(output); 54 return output; 55 } 56 57 // UTF-8编码的私有方法 58 _utf8_encode = function (string) { 59 string = string.replace(/\r\n/g,"\n"); 60 var utftext = ""; 61 for (var n = 0; n < string.length; n++) { 62 var c = string.charCodeAt(n); 63 if (c < 128) { 64 utftext += String.fromCharCode(c); 65 } else if((c > 127) && (c < 2048)) { 66 utftext += String.fromCharCode((c >> 6) | 192); 67 utftext += String.fromCharCode((c & 63) | 128); 68 } else { 69 utftext += String.fromCharCode((c >> 12) | 224); 70 utftext += String.fromCharCode(((c >> 6) & 63) | 128); 71 utftext += String.fromCharCode((c & 63) | 128); 72 } 73 } 74 return utftext; 75 } 76 77 // UTF-8解码的私有方法 78 _utf8_decode = function (utftext) { 79 var string = ""; 80 var i = 0; 81 var c = c1 = c2 = 0; 82 while ( i < utftext.length ) { 83 c = utftext.charCodeAt(i); 84 if (c < 128) { 85 string += String.fromCharCode(c); 86 i++; 87 } else if((c > 191) && (c < 224)) { 88 c2 = utftext.charCodeAt(i+1); 89 string += String.fromCharCode(((c & 31) << 6) | (c2 & 63)); 90 i += 2; 91 } else { 92 c2 = utftext.charCodeAt(i+1); 93 c3 = utftext.charCodeAt(i+2); 94 string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63)); 95 i += 3; 96 } 97 } 98 return string; 99 } 100 }
加密解密方法使用:
1.加密 var str = '码云笔记mybj123'; var base = new Base64(); var result = base.encode(str); //document.write(result); 2. 解密 var result2 = base.decode(result); document.write(result2);
路是自己走出来的,而不是选出来的。