Hãy thử điều này:
var B64 = {
alphabet: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/=',
lookup: null,
ie: /MSIE /.test(navigator.userAgent),
ieo: /MSIE [67]/.test(navigator.userAgent),
encode: function (s) {
var buffer = B64.toUtf8(s),
position = -1,
len = buffer.length,
nan1, nan2, enc = [, , , ];
if (B64.ie) {
var result = [];
while (++position < len) {
nan1 = buffer[position + 1], nan2 = buffer[position + 2];
enc[0] = buffer[position] >> 2;
enc[1] = ((buffer[position] & 3) << 4) | (buffer[++position] >> 4);
if (isNaN(nan1)) enc[2] = enc[3] = 64;
else {
enc[2] = ((buffer[position] & 15) << 2) | (buffer[++position] >> 6);
enc[3] = (isNaN(nan2)) ? 64 : buffer[position] & 63;
}
result.push(B64.alphabet[enc[0]], B64.alphabet[enc[1]], B64.alphabet[enc[2]], B64.alphabet[enc[3]]);
}
return result.join('');
} else {
result = '';
while (++position < len) {
nan1 = buffer[position + 1], nan2 = buffer[position + 2];
enc[0] = buffer[position] >> 2;
enc[1] = ((buffer[position] & 3) << 4) | (buffer[++position] >> 4);
if (isNaN(nan1)) enc[2] = enc[3] = 64;
else {
enc[2] = ((buffer[position] & 15) << 2) | (buffer[++position] >> 6);
enc[3] = (isNaN(nan2)) ? 64 : buffer[position] & 63;
}
result += B64.alphabet[enc[0]] + B64.alphabet[enc[1]] + B64.alphabet[enc[2]] + B64.alphabet[enc[3]];
}
return result;
}
},
decode: function (s) {
var buffer = B64.fromUtf8(s),
position = 0,
len = buffer.length;
if (B64.ieo) {
result = [];
while (position < len) {
if (buffer[position] < 128) result.push(String.fromCharCode(buffer[position++]));
else if (buffer[position] > 191 && buffer[position] < 224) result.push(String.fromCharCode(((buffer[position++] & 31) << 6) | (buffer[position++] & 63)));
else result.push(String.fromCharCode(((buffer[position++] & 15) << 12) | ((buffer[position++] & 63) << 6) | (buffer[position++] & 63)));
}
return result.join('');
} else {
result = '';
while (position < len) {
if (buffer[position] < 128) result += String.fromCharCode(buffer[position++]);
else if (buffer[position] > 191 && buffer[position] < 224) result += String.fromCharCode(((buffer[position++] & 31) << 6) | (buffer[position++] & 63));
else result += String.fromCharCode(((buffer[position++] & 15) << 12) | ((buffer[position++] & 63) << 6) | (buffer[position++] & 63));
}
return result;
}
},
toUtf8: function (s) {
var position = -1,
len = s.length,
chr, buffer = [];
if (/^[\x00-\x7f]*$/.test(s)) while (++position < len)
buffer.push(s.charCodeAt(position));
else while (++position < len) {
chr = s.charCodeAt(position);
if (chr < 128) buffer.push(chr);
else if (chr < 2048) buffer.push((chr >> 6) | 192, (chr & 63) | 128);
else buffer.push((chr >> 12) | 224, ((chr >> 6) & 63) | 128, (chr & 63) | 128);
}
return buffer;
},
fromUtf8: function (s) {
var position = -1,
len, buffer = [],
enc = [, , , ];
if (!B64.lookup) {
len = B64.alphabet.length;
B64.lookup = {};
while (++position < len)
B64.lookup[B64.alphabet[position]] = position;
position = -1;
}
len = s.length;
while (position < len) {
enc[0] = B64.lookup[s.charAt(++position)];
enc[1] = B64.lookup[s.charAt(++position)];
buffer.push((enc[0] << 2) | (enc[1] >> 4));
enc[2] = B64.lookup[s.charAt(++position)];
if (enc[2] == 64) break;
buffer.push(((enc[1] & 15) << 4) | (enc[2] >> 2));
enc[3] = B64.lookup[s.charAt(++position)];
if (enc[3] == 64) break;
buffer.push(((enc[2] & 3) << 6) | enc[3]);
}
return buffer;
}
};
Xem Here
Ý của bạn là Chuỗi là "UTF-8"? Một đối tượng String không biết về mã hóa và bảng mã. –
@Michael Konietzka: Tôi nghĩ đó là việc đánh lừa không cần thiết. Base64 mã hóa một chuỗi các byte. Tôi nghĩ rằng OP rõ ràng nói rằng chuỗi byte được giả định là mã hóa UTF-8 của một chuỗi unicode ** không ** rằng một 'java.lang.String' được mã hóa trực tiếp dưới dạng Base64 (như bạn nói sẽ không làm cho tinh thần.) – finnw
@finnw xin lỗi tôi không biết làm thế nào để giải thích rõ ràng. Tôi nhận được chuỗi được mã hóa bằng base64 và tôi muốn kiểm tra xem nó có đúng không. Tôi muốn nắm bắt tình hình khi tôi nhận được chuỗi mã hóa base64 mà sau khi giải mã trông giống như thùng rác, bởi vì tất cả mọi thứ tôi nhận được nên có một số ví dụ tên. – terry207