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仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘