无法对消息进行编码和解码。

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

impossible to encode and decode a message

问题

我正在尝试通过异或位运算符进行消息的编码和解码:

class Search {
    public static void main(String[] args) {
        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 < encmsg.length(); i++)
            decmsg = decmsg + (char) (encmsg.charAt(i) ^ key);

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

输出应该是这样的:
原始消息:This is a test
编码后消息:01+x1+x9x,=+,
解码后消息:This is a test

这是我收到的内容:

原始消息:This is a test
异常信息:"main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.base/java.lang.StringLatin1.charAt(StringLatin1.java:48)
at java.base/java.lang.String.charAt(String.java:711)
at Package/Package.Search.main(Search.java:15)


<details>
<summary>英文:</summary>

I am trying to encode and decode a message through a XOR bitwise operator:

class Search {
public static void main(String[] args) {
String msg = "This is a test";
String encmsg = "";
String decmsg = "";
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) (encmsg.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) (decmsg.charAt(i) ^ key);

System.out.print(&quot;Decoded message: &quot;);
System.out.println(decmsg);

}
}

The output should be this: 
Original message: This is a test
Encoded message: 01+x1+x9x,=+,
Decoded message: This is a test
 
This is what a receive:

Original message: This is a test
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.base/java.lang.StringLatin1.charAt(StringLatin1.java:48)
at java.base/java.lang.String.charAt(String.java:711)
at Package/Package.Search.main(Search.java:15)


</details>


# 答案1
**得分**: 1

你将原始消息和加密后的消息弄混了。解密也是同样的道理。例如 `encmsg = encmsg + (char) (encmsg.charAt(i) ^ key);` 应该改为 `msg.charAt(i)` 而不是 `encmsg.charAt(i)`,因为你应该从原始字符串中获取字符,对它们进行编码,然后添加到已经编码的部分中。

以下是编写逻辑的正确方式:

```java
// 对消息进行编码
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);
英文:

You mixed up your original message and your encrypted one. The same thing with decryption. For example encmsg = encmsg + (char) (encmsg.charAt(i) ^ key); should have msg.charAt(i) instead of encmsg.charAt(i) because you should take chars from your original string, encode them and add to your already encoded part.

Here is the the proper way to code the logic:

// 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);

huangapple
  • 本文由 发表于 2020年9月29日 22:03:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/64121269.html
匿名

发表评论

匿名网友

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

确定