0

currently I'm programming a chat with XOR-Encryption. But today i encountered a problem. The encryption is under Windows different as in Linux. Under Linux the chat runs normally, but under Windows not.

Here the class:

class XOR_c {
    private boolean active = true;
    private int key;

    // Constructor
    public XOR_c(int k){
        if (System.getProperty("os.name").contains("Windows")) {
            JOptionPane.showMessageDialog(null,"No encryption!","Client", JOptionPane.CANCEL_OPTION);
            this.active = false;
        }

        key = k;
    }

    public String encode(String s) {
        if (active == false) return s;

        char[] c = s.toCharArray();
        for (int i=0; i<c.length; i++)
            c[i] = (char)(c[i]^key);

        return new String(c);
    }

    public String decode(String s){
        return encode(s);
    }


}

This is tested with openSuse, Debian and Windows 7.

Now how to fix it (At this moment I made a bypass for the encryption but this is not my target, I want a encryption with both systems)? Is my source wrong?

4

1 回答 1

2

我猜这是一个编码问题:windows 和 linux 上的字符集可能不同。我建议

byte[] bytes = s.getBytes(charset);
...
return new String(bytes, charset)

,哪里charset是一些明确的字符集。

于 2013-06-11T20:00:39.273 回答