0

I have got a code which should cipher text from my textArea with some sort of String key, after the button is pressed down. The problem is, that this method can cipher text using only one character, not the whole string. I need to have a little longer key, so string is needed here. How can I change that?

btnCipher.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent klik) {
        String textToCipher = textArea.getText();
        String cipherKey = textField.getText();

        String cipheredText = "";
        int xor;
        char temp;

        for (int i=0; i<textToCipher.length(); i++){
            xor = textToCipher.charAt(i)^cipherKey; //error
            temp = (char)xor;
            cipheredText += temp;
        }
        textArea.setText(cipheredText);
    }
});
nhahtdh
  • 55,989
  • 15
  • 126
  • 162
user3235376
  • 87
  • 1
  • 2
  • 10
  • If this is just to obscure some text then that's fine, but if this is for security you should not write your own encryption. Discussed in [this SO question](http://stackoverflow.com/questions/3651090/) – Stephen P Feb 12 '14 at 00:00
  • possible duplicate of [XOR operation with two strings in java](http://stackoverflow.com/questions/5126616/xor-operation-with-two-strings-in-java) – nhahtdh Feb 12 '14 at 00:32

2 Answers2

-1

Change that line to:

xor = textToCipher.charAt(i) ^ cipherKey.charAt(i % cipherKey.length);

As mentioned in this post, while String class doesn't really mind storing a string with unpaired surrogates (which is actually invalid for a UTF-16 format), you will run into trouble encoding/decoding the string in other charset and when using code point related methods.

Community
  • 1
  • 1
nhahtdh
  • 55,989
  • 15
  • 126
  • 162
  • This action does "exclusive or" the character values, but may produce an invalid UTF-8 character as a result. – ErstwhileIII Feb 11 '14 at 23:56
  • @ErstwhileIII: String in Java is *somewhat* UTF16, since it allows unpaired surrogates. Real UTF-16 doesn't allow surrogate. – nhahtdh Feb 12 '14 at 00:16
-1

If your strings are in UTF-8 encoding, then using exclusive or between two strings (assume equal length for a moment) may NOT produce a valid UTF-8 result (in particular, you may create a "continuation" character if you have "a" and "æ" in the same position). Look here for a deeper discusion.

What is the use-case that you are trying to handle?

Community
  • 1
  • 1
ErstwhileIII
  • 4,829
  • 2
  • 23
  • 37
  • I just need to encode some standard text just to show that kind of possibility using java :) – user3235376 Feb 12 '14 at 06:35
  • @user3235376: The thing is that, while you get back a String object in Java and you can output it to the screen, you will run into trouble when you write it out to a stream to save it on some device. – nhahtdh Feb 12 '14 at 08:27
  • Trying to understand the "downgrade" for stating a fact about using "exclusive or" on a UTF string that produces an invalid UTF string in case one ever wants to display the encoded string. – ErstwhileIII Feb 12 '14 at 13:39
  • While it is correct to say that any UTF encoding has invalid sequences (that are used for surrogate in UTF-16 and UTF-32, or to indicate that there are more bytes to come for UTF-8), using a counter-example in UTF-8 to dissuade doesn't really work here, since String class uses UTF-16 to represent string, and 'a' ^ 'æ' happens to produce a valid UTF-16 character. – nhahtdh Feb 12 '14 at 15:02