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!
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can add an
intto achar, but the result is anint– you’d have to cast back tocharto put it back in the array, unless you use the compound assignment operator:or
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 usenew String(encryptedBytes), as you no longer have text data encoded in a normal text encoding.