有没有用javascript进行base64编码的代码?

wabc 2002-02-02 02:31:40
...全文
60 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
karma 2002-02-02
  • 打赏
  • 举报
回复
the simplest way is to use ADO.Stream, but to do everything in javascript, try

<script language="JavaScript1.2">
var base64 = [
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', // 0 to 7
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', // 8 to 15
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', // 16 to 23
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', // 24 to 31
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', // 32 to 39
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', // 40 to 47
'w', 'x', 'y', 'z', '0', '1', '2', '3', // 48 to 55
'4', '5', '6', '7', '8', '9', '+', '/' ]; // 56 to 63

function charFromCharCode (charCode) {
return unescape('%' + charCode.toString(16));
}

function reverseBase64 () {
var r = new Object();
for (var i = 0; i < 64; i++) {
r[base64[i]] = i;
} return r;
}

var reversedBase64 = reverseBase64();

function decode (encStr) {
var charCodes = new Array();
var decStr = "";

/* charCodes contains the index values into the base64 array
for each character in the encoded string */
for (var i = 0; i < encStr.length; i++)
charCodes[i] = reversedBase64[encStr.charAt(i)];

for (var i = 0; i < encStr.length; i += 4) {
/* bits24 is 4 groups of 6-bit character indexes */
var bits24 = ( charCodes [i] & 0xFF ) << 18;
bits24 |= ( charCodes [i + 1] & 0xFF ) << 12;
bits24 |= ( charCodes [i + 2] & 0xFF ) << 6;
bits24 |= ( charCodes [i + 3] & 0xFF ) << 0;

/* grab the character for the first 8 bits by masking off the
last 16 bits and then shifting right */
decStr += charFromCharCode((bits24 & 0xFF0000) >> 16);

/* if the next characer is a pad character, there won't be a
charCode
for it; so charCodes[] will return false and the character
won't
be decoded. */

/* grab the character for the second 8 bits by masking off the
last 8 bits and then shifting right */
if (charCodes[i + 2]) // check for padding character =
decStr += charFromCharCode((bits24 & 0xFF00) >> 8);

/* grab the character for the last 8 bits */
if (charCodes[i + 3]) // check for padding character =
decStr += charFromCharCode((bits24 & 0xFF) >> 0);
} return decStr;
}

function encode (Str) {
var charCodes = new Array();
var encStr = "";

for (var i = 0; i < Str.length; i += 3) {
/* grab groups of three characters, 24 bits, then split into
4 groups of 6 bits, and use each group as an index into
base64[] to get the encoded character */

/* bits24 is 3 groups of 8-bit characters */
var bits24 = ( Str.charCodeAt(i) ) << 16;

if ( Str.substr(i + 1, i + 2) ) {
bits24 |= ( Str.charCodeAt(i + 1) ) << 8;
}
else {
bits24 |= ( 0 ) << 8;
}

if ( Str.substr(i + 2, i + 3) ) {
bits24 |= ( Str.charCodeAt(i + 2) ) << 0;
}
else {
bits24 |= ( 0 ) << 0;
}

encStr += base64[ (bits24 >>> 18) & 0x3F ];

if ( (bits24 >>> 12) & 0x3F ) {
encStr += base64[ (bits24 >>> 12) & 0x3F ];
}
else {
encStr += '=';
}

if ( (bits24 >>> 6) & 0x3F ) {
encStr += base64[ (bits24 >>> 6) & 0x3F ];
}
else {
encStr += '=';
}

if ( (bits24 >>> 0) & 0x3F ) {
encStr += base64[ (bits24 >>> 0) & 0x3F ];
}
else {
encStr += '=';
}
} return encStr;
}

var ex = "Kibology";
var exEnc = "S2lib2xvZ3k=";
alert(decode(encode(ex)) + ' == ' + ex);
</script>

孟子E章 2002-02-02
  • 打赏
  • 举报
回复
var base64s =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

function encode(decStr){
var bits, dual, i = 0, encOut = '';
while(decStr.length >= i + 3){
bits =
(decStr.charCodeAt(i++) & 0xff) <<16 |
(decStr.charCodeAt(i++) & 0xff) <<8 |
decStr.charCodeAt(i++) & 0xff;
encOut +=
base64s.charAt((bits & 0x00fc0000) >>18) +
base64s.charAt((bits & 0x0003f000) >>12) +
base64s.charAt((bits & 0x00000fc0) >> 6) +
base64s.charAt((bits & 0x0000003f));
}
if(decStr.length -i > 0 && decStr.length -i < 3){
dual = Boolean(decStr.length -i -1);
bits =
((decStr.charCodeAt(i++) & 0xff) <<16) |
(dual ? (decStr.charCodeAt(i) & 0xff) <<8 : 0);
encOut +=
base64s.charAt((bits & 0x00fc0000) >>18) +
base64s.charAt((bits & 0x0003f000) >>12) +
(dual ? base64s.charAt((bits & 0x00000fc0) >>6) : '=') +
'=';
}
return encOut
}

var str = "spaghetti for the people!";
alert(encode(str))

...

/* Just couldn't let this slip by, one of these rare moments Mr. Honnen
proves human to the rest of us ;))
If for some reason one would decide to use the above "decode"
implementation instead of the NN4+ built-in "atob" method, he would
find it a no-go in NN4.05-. So, just for sake of it, provided below
goes a JS1.2 crossbrowser version using the same "base64s" string
scheme used in my "encode" implementation above. (should run slightly
faster than the previous "decode"-routine aswell).
*/


function decode(encStr) {
var bits, decOut = '', i = 0;
for(; i<encStr.length; i += 4){
bits =
(base64s.indexOf(encStr.charAt(i)) & 0xff) <<18 |
(base64s.indexOf(encStr.charAt(i +1)) & 0xff) <<12 |
(base64s.indexOf(encStr.charAt(i +2)) & 0xff) << 6 |
base64s.indexOf(encStr.charAt(i +3)) & 0xff;
decOut += String.fromCharCode(
(bits & 0xff0000) >>16, (bits & 0xff00) >>8, bits & 0xff);
}
if(encStr.charCodeAt(i -2) == 61)
return decOut.substring(0, decOut.length -2);
else if(encStr.charCodeAt(i -1) == 61)
return decOut.substring(0, decOut.length -1);
else return decOut;
}

var str = "spaghetti for the people!";
var enc_str = encode(str);
alert(decode(enc_str))

87,915

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧