I'm making a Cipher in Java but I can't do the follwing thing:
I want to edit the value of a char array to encrypt it
How should I do this?
Changing the ASCII value of each char might be the answer, I don't know; that's why I'm asking you Guys!
解决方案
You can add an int to a char, but the result is an int - you'd have to cast back to char to put it back in the array, unless you use the compound assignment operator:
array[x] += someInt;
or
array[x] = (char) (array[x] + someInt);
However, usually this isn't an appropriate way of performing encryption. You'll often end up with unprintable characters, or characters which don't even have a specific meaning in Unicode. Instead, most encryption algorithms are designed to work on arbitrary binary data - i.e. byte arrays.
Usually you would convert the string into a byte array first (e.g. with String.getBytes(charset) - definitely specify an encoding). Then perform encryption on the byte array, giving you a new byte array. If you really need to convert it back to text, use base64 to do so - do not use new String(encryptedBytes), as you no longer have text data encoded in a normal text encoding.