用go编码可执行文件并用javascript解码不起作用

我正在go中编码可执行文件,并尝试在javascript中对其进行解码。

string中对已编码的javascript进行解码不会得到匹配的文件。我能够对"this is a test string"这样的字符串进行编码,并在javascript中对其进行解码,并且效果很好。但是,当我使用可执行应用程序并执行相同的操作时,解码后的文件会比编码前的文件大。

我在做什么错?谢谢!

这是我正在使用的测试可执行文件。它是用c ++编写的,用g++编译并使用输出。

#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;
}

这是我用来将文件转换为go的{​​{1}}应用程序。

bytes

这是我尝试将文件解码并保存为javascript的方式。

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))
}
aheiheiliu 回答:用go编码可执行文件并用javascript解码不起作用

我知道了。经过研究和阅读,我发现了this questionthis article。最初,该问题并没有帮助我,但是在阅读了一段时间后,我尝试了一些示例,并能够使其中的一个起作用。我能够使solution 1正常工作。这是javascript,我现在必须开始工作。

保存的文件与源文件完全相同。

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
);

本文链接:https://www.f2er.com/3098241.html

大家都在问