基本上我正在尝试在我的JS文件中保存一个小的二进制文件(作为Base64字符串)。然后,在执行期间,对Base64进行解码并将其保存在硬盘驱动器上。
我一直在努力找出我的代码有什么问题,但总而言之,一旦我将解码后的base64写回HDD,我看到原始文件与编码解码文件之间的字节不同
我还使用以下site来加密,解密Base64ed字符串,以验证输入/输出是否正常。
var base64string = "base64 string";
function Base64Encode(str) {
if (/([^\u0000-\u00ff])/.test(str)) throw Error('String must be ASCII');
var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
var o1, o2, o3, bits, h1, h2, h3, h4, e=[], pad = '', c;
c = str.length % 3; // pad string to length of multiple of 3
if (c > 0) { while (c++ < 3) { pad += '='; str += '\0'; } }
// note: doing padding here saves us doing special-case packing for trailing 1 or 2 chars
for (c=0; c<str.length; c+=3) { // pack three octets into four hexets
o1 = str.charCodeAt(c);
o2 = str.charCodeAt(c+1);
o3 = str.charCodeAt(c+2);
bits = o1<<16 | o2<<8 | o3;
h1 = bits>>18 & 0x3f;
h2 = bits>>12 & 0x3f;
h3 = bits>>6 & 0x3f;
h4 = bits & 0x3f;
// use hextets to index into code string
e[c/3] = b64.charAt(h1) + b64.charAt(h2) + b64.charAt(h3) + b64.charAt(h4);
}
str = e.join(''); // use Array.join() for better performance than repeated string appends
// replace 'A's from padded nulls with '='s
str = str.slice(0, str.length-pad.length) + pad;
return str;
}
function Base64Decode(str) {
if (!(/^[a-z0-9+/]+={0,2}$/i.test(str)) || str.length%4 != 0) throw Error('Not base64 string');
var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
var o1, o2, o3, h1, h2, h3, h4, bits, d=[];
for (var c=0; c<str.length; c+=4) { // unpack four hexets into three octets
h1 = b64.indexOf(str.charAt(c));
h2 = b64.indexOf(str.charAt(c+1));
h3 = b64.indexOf(str.charAt(c+2));
h4 = b64.indexOf(str.charAt(c+3));
bits = h1<<18 | h2<<12 | h3<<6 | h4;
o1 = bits>>>16 & 0xff;
o2 = bits>>>8 & 0xff;
o3 = bits & 0xff;
d[c/4] = String.fromCharCode(o1, o2, o3);
// check for padding
if (h4 == 0x40) d[c/4] = String.fromCharCode(o1, o2);
if (h3 == 0x40) d[c/4] = String.fromCharCode(o1);
}
str = d.join(''); // use Array.join() for better performance than repeated string appends
return str;
}
var base64decoded= Base64Decode(base64string);
var TextStream = WScript.CreateObject('ADODB.Stream');
TextStream.Type = 2;
.,TextStream.charSet = 'windows-1250';
TextStream.Open();
TextStream.WriteText(base64decoded);
var BinaryStream = WScript.CreateObject('ADODB.Stream');
BinaryStream.Type = 1;
BinaryStream.Open();
TextStream.Position = 0;
TextStream.CopyTo(BinaryStream);
BinaryStream.SaveToFile("C:\\file.bin", 2);
BinaryStream.Close();
答案 0 :(得分:0)
原来我使用了错误的charSet。
TextStream.charSet =&#39; iso-8859-1&#39 ;;是正确的答案。