使用异或操作进行消息的编码和解码。

huangapple go评论72阅读模式
英文:

Use XOR to encode and decode a message

问题

我正在使用异或(XOR)来编码和解码一条消息:

class Search {
    public static void main(String arg[]) {
        String msg = "This is a test";
        String encmsg = "";
        String decmsg = "";
        int key = 88;

        System.out.print("Original message: ");
        System.out.println(msg);

        // 编码消息。
        for(int i=0; i < msg.length(); i++)
            encmsg = encmsg + (char) (msg.charAt(i) ^ key);

        System.out.print("Encoded message: ");
        System.out.println(encmsg);

        // 解码消息。
        for(int i=0; i < msg.length(); i++)
            decmsg = decmsg + (char) (encmsg.charAt(i) + key);

        System.out.print("Decoded message: ");
        System.out.println(decmsg);
    }
}

正如您所看到的,使用相同密钥的两个异或运算的结果应该产生解码后的消息。一元的反码(NOT)运算符会反转操作数的所有位的状态。我会为您留下输出:

Original message: This is a test
Encoded message: 01+x1+x9x,=+,
Decoded message: dˆ‰ƒ&#208;‰ƒ&#208;‘&#208;„•ƒ„

期望输出:

Original message: This is a test
Encoded message: 01+x1+x9x,=+,
Decoded message: This is a test

出了什么问题?问题可能与ASCII编码相关。

英文:

I'm using a XOR to encode and decode a message:

class Search {
public static void main(String arg[]) {
	String msg = &quot;This is a test&quot;;
	String encmsg = &quot;&quot;;
	String decmsg = &quot;&quot;;
	int key = 88;
	
	System.out.print(&quot;Original message: &quot;);
	System.out.println(msg);
	
	// encode the message.
	for(int i=0; i &lt; msg.length(); i++)
		encmsg = encmsg + (char) (msg.charAt(i) ^ key);
	
	System.out.print(&quot;Encoded message: &quot;);
	System.out.println(encmsg);
	
	// Decode the message.
	for(int i=0; i &lt; msg.length(); i++)
		decmsg = decmsg + (char) (encmsg.charAt(i) + key);
	
	System.out.print(&quot;Decoded message: &quot;);
	System.out.println(decmsg);
	
    }
}  

As you can see, the result of the two XORs using the same key should produce the decoded message.
The unary one's complement (NOT) operator reverses the state of all the bits of the operand. I'll leave you the output:

Original message: This is a test
Encoded message: 01+x1+x9x,=+,
Decoded message: dˆ‰ƒ&#208;‰ƒ&#208;‘&#208;„•ƒ„

Expected output:

Original message: This is a test
Encoded message: 01+x1+x9x,=+,
Decoded message: This is a test

What's wrong? The problem may be connected to the ASCII

答案1

得分: 1

可能是你的解码代码块中有一个拼写错误,你只需要将

decmsg = decmsg + (char) (encmsg.charAt(i) + key);

改为

decmsg = decmsg + (char) (encmsg.charAt(i) ^ key);

输出如下所示:

原始消息:This is a test
编码后消息:01+x1+x9x,=+,
解码后消息:This is a test

顺便提一下,最好在 if 代码块内部使用 {} 来使我们的代码更易读。

英文:

There might be a typo in your decode code block,you just need to change

decmsg = decmsg + (char) (encmsg.charAt(i) + key);

to

decmsg = decmsg + (char) (encmsg.charAt(i) ^ key);	

The output is listed as below:

Original message: This is a test
Encoded message: 01+x1+x9x,=+,
Decoded message: This is a test

BTW, we had better to use {} within the if code block to make our code more easy to read.

答案2

得分: 1

The + operator should be replaced by ^ in your decoding.

Because you use exclusive or operator to encode. Therefore you should stick on this operator to decode.

英文:

The + operator should be replaced by ^ in your decoding.

Because you use exclusive or operator to encode. Therefore you should stick on this operator to decode.

huangapple
  • 本文由 发表于 2020年9月27日 23:15:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/64090024.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定