I'm trying to, with WSH JavaScript, decode a base64 string containing an executable file, and write the EXE to a file. This is the function I have for decoding:
decodeBase64 = function(s) {
var e={},i,b=0,c,x,l=0,a,r='',w=String.fromCharCode,L=s.length;
var A="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
for(i=0;i<64;i++){e[A.charAt(i)]=i;}
for(x=0;x<L;x++){
c=e[s.charAt(x)];b=(b<<6)+c;l+=6;
while(l>=8){((a=(b>>>(l-=8))&0xff)||(x<(L-2)))&&(r+=w(a));}
}
return r;
};
It works for the decoding but when I try to write the decoded string to a file, I get an error saying "Invalid procedure call or argument". I suspect that's due to the encoding being wrong. Here's that code:
var FSO = new ActiveXObject("Scripting.FileSystemObject");
var f = FSO.CreateTextFile(Path, true, false);
f.Write(decodeBase64(data));
f.Close();
Now, I know you can make a ADODB.Stream with ISO-8859-1 encoding and read it out as Windows-1252 encoding, but I don't have that in the environment I'm in. Is it possible that there is another way to decode a Base64 string, containing an EXE in JavaScript, in the Windows ANSI encoding then save that to a file? Possibly a way to convert the encoding of the result of the decodeBase64 function to an encoding that I could write as a valid EXE?