base64的编码和解码

作者:亚搏app官网    发布时间:2020-02-09 19:41    浏览:137 次

[返回]

时间: 2018-01-25阅读: 1036标签: base64Base64是什么?

Base64是大器晚成种基于陆十七个可打字与印刷字符来代表二进制数据的象征方法。由于2的6次方等于64,所以每6个比特为叁个单元,对应有些可打字与印刷字符。多个字节有二十三个比特,对应于4个Base64单元,即3个字节供给用4个可打印字符来表示。它可用来作为电子邮件的传导编码。在Base64中的可打字与印刷字符包罗字母A-Z、a-z、数字0-9 ,那样共有65个字符,其余多个可打字与印刷符号在分歧的种类中而各异。一些如uuencode的任何编码方法,和之后binhex的版本采纳不一样的64字符集来代表6个二进制数字,然则它们不叫Base64。Base64常用于在平时管理公事数据的场馆,表示、传输、存款和储蓄一些二进制数据。包罗MIME的email,email via MIME, 在XML中蕴藏复杂数据。Base64其实是意气风发种简单的调换加密方法,可是BASE64的用处往往而不是为了防守新闻败露,並且为了便于传输,进过BASE64编码后的音信会比原本音讯长,大约是4/3倍。

Base64其实是意气风发种简易的置换加密方法,不过BASE64的用场往往并非为着制止音讯外泄,而且为了有助于传输,进过BASE64编码后的消息会比原本消息长,大约是4/3倍。base64是生机勃勃种基于六11个可打字与印刷字符来表示二进制数据的象征方法。由于2的6次方等于64,所以每6个比特为贰个单元,对应有个别可打字与印刷字符。

实今世码风姿浪漫、测量检验能够用

Base64的作用:

varbase64EncodeChars="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";varbase64DecodeChars=newArray(-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63,52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1,-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1,-1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1);/***base64编码*@param{Object}str*/functionbase64encode{varout,i,len;varc1,c2,c3;len=str.length;i=0;out="";while{c1=str.charCodeAt{out+=base64EncodeChars.charAt;out+=base64EncodeChars.charAt;out+="==";break;}c2=str.charCodeAt{out+=base64EncodeChars.charAt;out+=base64EncodeChars.charAt|;out+=base64EncodeChars.charAt;out+="=";break;}c3=str.charCodeAt;out+=base64EncodeChars.charAt;out+=base64EncodeChars.charAt|;out+=base64EncodeChars.charAt|;out+=base64EncodeChars.charAt;}returnout;}/***base64解码*@param{Object}str*/functionbase64decode{varc1,c2,c3,c4;vari,len,out;len=str.length;i=0;out="";while{/*c1*/do{c1=base64DecodeChars[str.charCodeAt&0xff];}while;ifbreak;/*c2*/do{c2=base64DecodeChars[str.charCodeAt&0xff];}while;ifbreak;out+=String.fromCharCode>>4));/*c3*/do{c3=str.charCodeAtreturnout;c3=base64DecodeChars[c3];}while;ifbreak;out+=String.fromCharCode|;/*c4*/do{c4=str.charCodeAtreturnout;c4=base64DecodeChars[c4];}while;ifbreak;out+=String.fromCharCode;}returnout;}/***utf16转utf8*@param{Object}str*/functionutf16to8{varout,i,len,c;out="";len=str.length;for{c=str.charCodeAt&&{out+=str.charAt{out+=String.fromCharCode;out+=String.fromCharCode;out+=String.fromCharCode;}else{out+=String.fromCharCode;out+=String.fromCharCode;}}returnout;}/***utf8转utf16*@param{Object}str*/functionutf8to16{varout,i,len,c;varchar2,char3;out="";len=str.length;i=0;while{c=str.charCodeAt{case0:case1:case2:case3:case4:case5:case6:case7://0xxxxxxxout+=str.charAt;break;case12:case13://110xxxxx10xxxxxxchar2=str.charCodeAt;out+=String.fromCharCode|;break;case14://1110xxxx10xxxxxx10xxxxxxchar2=str.charCodeAt;char3=str.charCodeAt;out+=String.fromCharCode||;break;}}returnout;}//demo//functiondoit(){//varf=document.f;//f.output.value=base64encode(utf16to8;//f.decode.value=utf8to16(base64decode;//}

Base64常用于在常常处理公事数据的场面,表示、传输、存款和储蓄一些二进制数据。富含MIME的email,email via MIME, 在XML中寄放复杂数据。

成效分割版,正是将三个js函数分别写出来

JS加密解密方法运用

Javascript的base64加密函数和base64解密函数,仿PHP base64_encode.附实例。能够将js代码保存到base64.js中,在须要的时候调用。注意:base64加密和平解决密有固有的算,请不要转移以下代码,不然可能招致程序出错,恐怕无法取获得科学的结果。为了确定保证代码的精确性,请下载附属类小零部件base64.js ,而毫不一向复制本页代码。

//1.加密 var str = '124中文内容'; var base = new Base64(); var result = base.encode(str); //document.write(result); //2.解密 var result2 = base.decode(result); document.write(result2);

base64加密代码

加密、解密算法封装

 function base64_encode{ var c1, c2, c3; var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; var i = 0, len= str.length, string = ''; while { c1 = str.charCodeAt & 0xff; if { string += base64EncodeChars.charAt; string += base64EncodeChars.charAt; string += "=="; break; } c2 = str.charCodeAt{ string += base64EncodeChars.charAt; string += base64EncodeChars.charAt | ; string += base64EncodeChars.charAt; string += "="; break; } c3 = str.charCodeAt; string += base64EncodeChars.charAt; string += base64EncodeChars.charAt | ; string += base64EncodeChars.charAt | ; string += base64EncodeChars.charAt } return string }
function Base64() { // private property _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; // public method for encoding this.encode = function (input) { var output = ""; var chr1, chr2, chr3, enc1, enc2, enc3, enc4; var i = 0; input = _utf8_encode(input); while (i  input.length) { chr1 = input.charCodeAt(i++); chr2 = input.charCodeAt(i++); chr3 = input.charCodeAt(i++); enc1 = chr1  2; enc2 = ((chr1  3)  4) | (chr2  4); enc3 = ((chr2  15)  2) | (chr3  6); enc4 = chr3  63; if (isNaN(chr2)) { enc3 = enc4 = 64; } else if (isNaN(chr3)) { enc4 = 64; } output = output + _keyStr.charAt(enc1) + _keyStr.charAt(enc2) + _keyStr.charAt(enc3) + _keyStr.charAt(enc4); } return output; } // public method for decoding this.decode = function (input) { var output = ""; var chr1, chr2, chr3; var enc1, enc2, enc3, enc4; var i = 0; input = input.replace(/[^A-Za-z0-9+/=]/g, ""); while (i  input.length) { enc1 = _keyStr.indexOf(input.charAt(i++)); enc2 = _keyStr.indexOf(input.charAt(i++)); enc3 = _keyStr.indexOf(input.charAt(i++)); enc4 = _keyStr.indexOf(input.charAt(i++)); chr1 = (enc1  2) | (enc2  4); chr2 = ((enc2  15)  4) | (enc3  2); chr3 = ((enc3  3)  6) | enc4; output = output + String.fromCharCode(chr1); if (enc3 != 64) { output = output + String.fromCharCode(chr2); } if (enc4 != 64) { output = output + String.fromCharCode(chr3); } } output = _utf8_decode(output); return output; } // private method for UTF-8 encoding _utf8_encode = function (string) { string = string.replace(/rn/g,"n"); var utftext = ""; for (var n = 0; n  string.length; n++) { var c = string.charCodeAt(n); if (c  128) { utftext += String.fromCharCode(c); } else if((c  127)  (c  2048)) { utftext += String.fromCharCode((c  6) | 192); utftext += String.fromCharCode((c  63) | 128); } else { utftext += String.fromCharCode((c  12) | 224); utftext += String.fromCharCode(((c  6)  63) | 128); utftext += String.fromCharCode((c  63) | 128); } } return utftext; } // private method for UTF-8 decoding _utf8_decode = function (utftext) { var string = ""; var i = 0; var c = c1 = c2 = 0; while ( i  utftext.length ) { c = utftext.charCodeAt(i); if (c  128) { string += String.fromCharCode(c); i++; } else if((c  191)  (c  224)) { c2 = utftext.charCodeAt(i+1); string += String.fromCharCode(((c  31)  6) | (c2  63)); i += 2; } else { c2 = utftext.charCodeAt(i+1); c3 = utftext.charCodeAt(i+2); string += String.fromCharCode(((c  15)  12) | ((c2  63)  6) | (c3  63)); i += 3; } } return string; } }

base64解密啊代码

function base64_decode{ var c1, c2, c3, c4; var base64DecodeChars = new Array( -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1 ); var i=0, len = str.length, string = ''; while { do{ c1 = base64DecodeChars[str.charCodeAt & 0xff] } while ( i < len && c1 == -1 ); if  break; do{ c2 = base64DecodeChars[str.charCodeAt & 0xff] } while ( i < len && c2 == -1 ); if  break; string += String.fromCharCode | ; do{ c3 = str.charCodeAt & 0xff; if  return string; c3 = base64DecodeChars[c3] } while ( i < len && c3 == -1 ); if  break; string += String.fromCharCode | ; do{ c4 = str.charCodeAt & 0xff; if  return string; c4 = base64DecodeChars[c4] } while ( i < len && c4 == -1 ); if  break; string += String.fromCharCode | c4) } return string; }

 document.write(base64_encode; document.write; document.write(base64_decode('aHR0cDovL3d3dy5qYjUxLm5ldA==')); 

贯彻代码二:针对utf8

/* utf.js - UTF-8 &lt;=&gt; UTF-16 convertion** Copyright  1999 Masanao Izumo &lt;iz@onicos.co.jp&gt;* Version: 1.0* LastModified: Dec 25 1999* This library is free. You can redistribute it and/or modify it.*//** Interfaces:* utf8 = utf16to8;* utf16 = utf16to8;*/function utf16to8 { var out, i, len, c; out = ""; len = str.length; for { c = str.charCodeAt &amp;&amp;  { out += str.charAt; } else if  { out += String.fromCharCode &amp; 0x0F)); out += String.fromCharCode &amp; 0x3F)); out += String.fromCharCode &amp; 0x3F)); } else { out += String.fromCharCode &amp; 0x1F)); out += String.fromCharCode &amp; 0x3F)); } } return out;}function utf8to16 { var out, i, len, c; var char2, char3; out = ""; len = str.length; i = 0; while { c = str.charCodeAt { case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: // 0xxxxxxx out += str.charAt; break; case 12: case 13: // 110x xxxx 10xx xxxx char2 = str.charCodeAt; out += String.fromCharCode | ; break; case 14: // 1110 xxxx 10xx xxxx 10xx xxxx char2 = str.charCodeAt; char3 = str.charCodeAt; out += String.fromCharCode |  | ; break; } } return out;}/* Copyright  1999 Masanao Izumo &lt;iz@onicos.co.jp&gt;* Version: 1.0* LastModified: Dec 25 1999* This library is free. You can redistribute it and/or modify it.*//** Interfaces:* b64 = base64encode;* data = base64decode;*/var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";var base64DecodeChars = new Array( -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1);function base64encode { var out, i, len; var c1, c2, c3; len = str.length; i = 0; out = ""; while { c1 = str.charCodeAt &amp; 0xff; if { out += base64EncodeChars.charAt; out += base64EncodeChars.charAt; out += "=="; break; } c2 = str.charCodeAt { out += base64EncodeChars.charAt; out += base64EncodeChars.charAt | ; out += base64EncodeChars.charAt; out += "="; break; } c3 = str.charCodeAt; out += base64EncodeChars.charAt; out += base64EncodeChars.charAt | ; out += base64EncodeChars.charAt | ; out += base64EncodeChars.charAt; } return out;}function base64decode { var c1, c2, c3, c4; var i, len, out; len = str.length; i = 0; out = ""; while { /* c1 */ do { c1 = base64DecodeChars[str.charCodeAt &amp; 0xff]; } while; if break; /* c2 */ do { c2 = base64DecodeChars[str.charCodeAt &amp; 0xff]; } while; if break; out += String.fromCharCode | ; /* c3 */ do { c3 = str.charCodeAt &amp; 0xff; if return out; c3 = base64DecodeChars[c3]; } while; if break; out += String.fromCharCode | ; /* c4 */ do { c4 = str.charCodeAt &amp; 0xff; if return out; c4 = base64DecodeChars[c4]; } while; if break; out += String.fromCharCode | c4); } return out;}//input base64 encodefunction strdecode{ return utf8to16;}&lt;/pre&gt;

搜索