英文:
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ˆ‰ƒÐ‰ƒÐ‘Є•ƒ„
期望输出:
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 = "This is a test";
String encmsg = "";
String decmsg = "";
int key = 88;
System.out.print("Original message: ");
System.out.println(msg);
// encode the message.
for(int i=0; i < msg.length(); i++)
encmsg = encmsg + (char) (msg.charAt(i) ^ key);
System.out.print("Encoded message: ");
System.out.println(encmsg);
// Decode the message.
for(int i=0; i < msg.length(); i++)
decmsg = decmsg + (char) (encmsg.charAt(i) + key);
System.out.print("Decoded message: ");
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ˆ‰ƒÐ‰ƒÐ‘Є•ƒ„
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论