I have problem with this part of the code
private char encryptChar(char c, int shift) {
if (Character.isLetter(c))
return (char) ('A' + (c - 'A' + shift) % 26);
else
return c;
}
private String encryptMessage(String msg, int shift) {
String result = "";
for (int i = 0; i < msg.length(); i++)
result += encryptChar(msg.charAt(i), shift);
return result;
}
and for encoding and decoding
if (clientMsg.equals("ENCRYPT"))
{
System.out.println("Enkript");
opw.println("OK"); // send ok to client
opw.flush();
// take the message from the server and send it back
String text = br.readLine();
System.out.println(text);
int shiftmsg = 26 - moving;
opw.println(encryptMessage(text, shiftmsg)+"\n"); // return encrypted message to server
opw.flush();
}
else if (clientMsg.equals("DECRYPT"))
{
System.out.println("DEKRzpt");
opw.println("OK");
opw.flush();
// take the message fom the server and send it back
String text = br.readLine();
System.out.println(text);
opw.println(encryptMessage(text, 26-moving)+"\n"); // return decrypted message to server
// //opw.println("OK");
opw.flush();
It works in most occasions, but when I enter send text and I decrypt it I get MEND TEXT
Can you help me with this?