1

I am encoding an executable in go and trying to decode it in javascript.

Decoding the encoded string in javascript does not result in a matching file. I am able to encode a string like "this is a test string" and decode it in javascript and it works fine. But when i take an executable application and do the same thing, the decoded file is larger than the file before encoding.

What am i doing wrong? Thanks!

Here is the test executable i am using. It is in c++, compile it with g++ and use the output.

#include <iostream>
int main(void) {

    char test1[] = "hello";

    std::cout << "test1: " << test1 << std::endl;

    char test2[] = "world";

    std::cout << "test2: " << test2 << std::endl;

    char test3[] = "foobar";

    std::cout << "test3: " << test3 << std::endl;

    return 0;
}

Here is the go app i am using to convert the file to bytes.

package main

import (
    "fmt"
    "github.com/atotto/clipboard"
    "io/ioutil"
)

func main() {
    bytes, err := ioutil.ReadFile("/path/to/file/a.out")
    if err != nil {
        fmt.Println(err)
    }

    enc := make([]byte, base64.RawStdEncoding.EncodedLen(len(bytes)))
    base64.RawStdEncoding.Encode(enc, bytes)

    fmt.Println("byte size: ", len(bytes))
    fmt.Println("encoded byte size: ", len(enc))

    clipboard.WriteAll(string(enc))
}

Here is how i am attempting to decode and save the file in javascript.

let decodedBytes = atob("put the bytes here from your clipboard from running the go app");

fs.writeFileSync(
    "/destination/to/save/file",
    decodedBytes
);
prolink007
  • 33,872
  • 24
  • 117
  • 185

1 Answers1

0

I figured it out. After some research and reading i found this question and this article. Initially the question did not help me, but after reading that article for some time, I tried a few of the examples and was able to get one of them to work. I was able to get solution 1 to work. Here is the javascript i have now to get this working.

Saved file is exactly the same as the source.

function b64ToUint6(nChr) {
  return nChr > 64 && nChr < 91
    ? nChr - 65
    : nChr > 96 && nChr < 123
    ? nChr - 71
    : nChr > 47 && nChr < 58
    ? nChr + 4
    : nChr === 43
    ? 62
    : nChr === 47
    ? 63
    : 0;
}

function base64DecToArr(sBase64, nBlockSize) {
  var sB64Enc = sBase64.replace(/[^A-Za-z0-9\+\/]/g, ""),
    nInLen = sB64Enc.length,
    nOutLen = nBlockSize
      ? Math.ceil(((nInLen * 3 + 1) >>> 2) / nBlockSize) * nBlockSize
      : (nInLen * 3 + 1) >>> 2,
    aBytes = new Uint8Array(nOutLen);

  for (
    var nMod3, nMod4, nUint24 = 0, nOutIdx = 0, nInIdx = 0;
    nInIdx < nInLen;
    nInIdx++
  ) {
    nMod4 = nInIdx & 3;
    nUint24 |= b64ToUint6(sB64Enc.charCodeAt(nInIdx)) << (18 - 6 * nMod4);
    if (nMod4 === 3 || nInLen - nInIdx === 1) {
      for (nMod3 = 0; nMod3 < 3 && nOutIdx < nOutLen; nMod3++, nOutIdx++) {
        aBytes[nOutIdx] = (nUint24 >>> ((16 >>> nMod3) & 24)) & 255;
      }
      nUint24 = 0;
    }
  }

  return aBytes;
}

let decodedBytes = base64DecToArr("bytes to decode");

fs.writeFileSync(
    "/destination/to/save/file",
    decodedBytes
);

prolink007
  • 33,872
  • 24
  • 117
  • 185