简单地在 Javascript 中进行 XOR 加密并在 Java 中解密

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33529103/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2021-01-28 16:35:58  来源:igfitidea点击:

Simply XOR encrypt in Javascript and Decrypt in Java

javascriptjavaencryptionxor

提问by DragonGamer

The purpose for this is not highly security-relevant and the key will be long, so I'm just wanting to use simple XOR encryption to the strings.

这样做的目的与安全性无关,并且密钥很长,所以我只想对字符串使用简单的 XOR 加密。

Well, the Javascript on the client is as follows:

那么,客户端上的Javascript如下:

function dc_encrypt(str, key)
{
   var ord = []; var res = "";

   var i;
   for (i = 1; i <= 255; i++) {ord[String.fromCharCode(i)] = i}

   for (i = 0; i < str.length; i++)
       res += String.fromCharCode(ord[str.substr(i, 1)] ^ ord[key.substr(i %    key.length, 1)]);

   return(res);
}

And the Java is is:

而Java是:

public String dc_decrypt(String str, String key)
{
   StringBuilder sb = new StringBuilder();
   for(int i = 0; i < str.length(); i++)
   sb.append((char)(str.charAt(i) ^ key.charAt(i % (key.length()))));
   return(sb.toString());
}

Unfortunately this produces some very weird results. Some letters differ after encrypting in JS, sending the result through a POST and decrypt in Java. In every case it doesn't seem to be reliable.

不幸的是,这会产生一些非常奇怪的结果。有些字母在JS中加密后不同,通过POST发送结果并在Java中解密。在每种情况下,它似乎都不可靠。

I assume the issue must have something to do with encoding... does someone know a more reliable solution for this?

我认为这个问题一定与编码有关......有人知道更可靠的解决方案吗?

Huge thanks in advance! :)

非常感谢提前!:)

采纳答案by kairda

When XOR-encoding two strings, the resulting XOR-values of the individual characters sometimes do not result in characters that can be displayed. Therefore one solution is to encode the result as a sequence of hex-values and then to decode these hex-values on the server side.

对两个字符串进行 XOR 编码时,生成的单个字符的 XOR 值有时不会产生可以显示的字符。因此,一种解决方案是将结果编码为一系列十六进制值,然后在服务器端解码这些十六进制值。

Javascript:

Javascript:

function encryptStringWithXORtoHex(input,key) {
    var c = '';
    while (key.length < input.length) {
         key += key;
    }
    for(var i=0; i<input.length; i++) {
        var value1 = input[i].charCodeAt(0);
        var value2 = key[i].charCodeAt(0);

        var xorValue = value1 ^ value2;

        var xorValueAsHexString = xorValue.toString("16");

        if (xorValueAsHexString.length < 2) {
            xorValueAsHexString = "0" + xorValueAsHexString;
        }

        c += xorValueAsHexString;
    }
    return c;
}

Java-Code:

Java代码:

private static String decryptStringWithXORFromHex(String input,String key) {
    StringBuilder c = new StringBuilder();
    while (key.length() < input.length()/2) {
        key += key;
    }

    for (int i=0;i<input.length();i+=2) {
        String hexValueString = input.substring(i, i+2);
        int value1 = Integer.parseInt(hexValueString, 16);
        int value2 = key.charAt(i/2);

        int xorValue = value1 ^ value2;

        c.append(Character.toString((char) xorValue));

    }
    return c.toString();
};

Example: Encode in Javascript:

示例:在 Javascript 中编码:

encryptStringWithXORtoHex('Encrypt This','SecretKey');

encryptStringWithXORtoHex('Encrypt This','SecretKey');

returns the string 160b00001c043f452d3b0c10

返回字符串 160b00001c043f452d3b0c10

Decrypting in Java:

用Java解密:

decryptStringWithXORFromHex("160b00001c043f452d3b0c10","SecretKey")

decryptStringWithXORFromHex("160b00001c043f452d3b0c10","SecretKey")

returns Encrypt This

回报 Encrypt This

Please note: the shown solution only works for characters that have a charChode value of less or equal than 255. If you want to use the solution for unicode characters (e.g. ) you will have to change the code to take care of this.

请注意:所示解决方案仅适用于 charChode 值小于或等于 255 的字符。如果您想对 unicode 字符(例如 )使用该解决方案,则必须更改代码以解决此问题。