无法解密Xor-Base64文本 [英] Unable to decrypt the xor-base64 text

查看:206
本文介绍了无法解密Xor-Base64文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码对数据进行加密和解密.现在,我想从Node JS加密数据,并想从Go lang解密数据.但是我无法使用GO lang来实现.

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.

推荐答案

您应该使用DecodeRuneInString而不是仅将slice字符串转换为字节.

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

操场上的解决方案: 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
}

与您的js结果相比

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"

测试结果是相同的.

这是为什么.

在运行中,当您设置字符串范围时,将迭代字节,但是javascript charCodeAt用于字符而不是字节.在utf-8中,字符长度可能为2或3个字节.这就是为什么您得到不同的输出的原因.

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.

在操场上进行测试 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.\n",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|    \n",index, string(ru), i)
}

输出为

slice of string...
The byte at 4 is |�|, |我| is 3 bytes long.
runes of string...
The character code at 4 is|我|25105| 

这篇关于无法解密Xor-Base64文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆