duangang3832 2019-01-19 12:33
浏览 178
已采纳

无法解密Xor-Base64文本

I am using the below code to encrypt and decrypt the data. Now I want to encrypt the data from Node JS and want to decrypt the data from Go lang. But I am not able to achieve it using GO lang.

var B64XorCipher = {
  encode: function(key, data) {
    return new Buffer(xorStrings(key, data),'utf8').toString('base64');
  },
  decode: function(key, data) {
    data = new Buffer(data,'base64').toString('utf8');
    return xorStrings(key, data);
  }
};

function xorStrings(key,input){
  var output='';
  for(var i=0;i<input.length;i++){
    var c = input.charCodeAt(i);
    var k = key.charCodeAt(i%key.length);
    output += String.fromCharCode(c ^ k);
  }
  return output;
}

From go I am trying to decode like below I am not able to achieve it.

bytes, err := base64.StdEncoding.DecodeString(actualInput)
encryptedText := string(bytes)
fmt.Println(EncryptDecrypt(encryptedText, "XXXXXX"))

func EncryptDecrypt(input, key string) (output string) {
    for i := range input {
        output += string(input[i] ^ key[i%len(key)])
    }

    return output
}

Can someone help me to resolve it.

  • 写回答

2条回答 默认 最新

  • douwengzao5790 2019-01-20 09:05
    关注

    You should use DecodeRuneInString instead of just slice string to byte.

    Solution in playground: https://play.golang.org/p/qi_6S1J_dZU

    package main
    
    import (
        "fmt"
        "unicode/utf8"
    )
    
    func main() {
        fmt.Println("Hello, playground")
        k:="1234fd23434"
        input:="The 我characterode我 113 is equal to q"
        fmt.Println(EncryptDecrypt(input,k))
    
        // expect: "eZV扷ZRFRWEWA[戣[@GRX@^B"
    
    }
    
    func EncryptDecrypt(input, key string) (output string) {
        keylen := len(key)
        count := len(input)
        i := 0
        j := 0
        for i < count {
            c, n := utf8.DecodeRuneInString(input[i:])
            i += n
            k, m := utf8.DecodeRuneInString(key[j:])
            j += m
            if j >= keylen {
                j = 0
            }
    
            output += string(c ^ k)
        }
    
        return output
    }
    

    compared to your js result

    function xorStrings(key,input){
      var output='';
      for(var i=0;i<input.length;i++){
        var c = input.charCodeAt(i);
        var k = key.charCodeAt(i%key.length);
        output += String.fromCharCode(c ^ k);
      }
      return output;
    }
    
    console.log(xorStrings('1234fd23434',"The 我characterode我 113 is equal to q"))
    // expect: "eZV扷ZRFRWEWA[戣[@GRX@^B"
    

    The test result is the same.

    Here is why.

    In go, when you range a string, you iterate bytes, but javascript charCodeAt is for character,not byte. In utf-8, the character is maybe 2 or 3 bytes long. So that is why you got different output.

    Test in playground https://play.golang.org/p/XawI9aR_HDh

    package main
    
    import (
        "fmt"
        "unicode/utf8"
    )
    
    var sentence = "The 我quick brown fox jumps over the lazy dog."
    
    var index = 4
    
    func main() {
        fmt.Println("slice of string...")
        fmt.Printf("The byte at %d is |%s|, |%s| is 3 bytes long.
    ",index,sentence[index:index+1],sentence[index:index+3])
    
        fmt.Println("runes of string...")
        ru, _ := utf8.DecodeRuneInString(sentence[index:])
        i := int(ru)
        fmt.Printf("The character code at %d is|%s|%d|    
    ",index, string(ru), i)
    }
    

    The output is

    slice of string...
    The byte at 4 is |�|, |我| is 3 bytes long.
    runes of string...
    The character code at 4 is|我|25105| 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题