Caesar cipher in java ciphertext

Im creating a caesar Java cryptographic cipher, here is my code

private void encCaesar() { tempCipher = "abcdef"; char[] chars = tempCipher.toCharArray(); for (int z = 0; z < tempCipher.length(); z++) { char c = chars[z]; if (c >= 32 && c <= 126) { int x = c - 32; x = (x + keyCaesar) % 96; if (x < 0) x += 96; chars[z] = (char) (x + 32); } } ciphertext = chars.toString(); etCipher.setText(ciphertext); } 

I cannot find something wrong, but the ciphertext is something like this 405888, which is nonsense, where the plaintext is "abcdef" and the default key is 3

What's wrong?

correct:

 private void encCaesar() { tempCipher = "abcdef"; char[] chars = tempCipher.toCharArray(); for (int z = 0; z < tempCipher.length(); z++) { char c = chars[z]; if (c >= 32 && c <= 126) { int x = c - 32; x = (x + keyCaesar) % 96; if (x < 0) x += 96; chars[z] = (char) (x + 32); } } ciphertext = new String(chars); etCipher.setText(ciphertext); } 
+4
source share
1 answer

You should create a ciphertext with new String(chars) instead of chars.toString() :

 ciphertext = new String(chars); 
+3
source

All Articles