1

I have a problem when I'm trying to decode an encrypted message. It decrypts almost everything as it should be, but when I try to decrypt 'w x y z' (all lower case), it doesn't work properly. It only decrypts those letters when they are UPPERCASE. What did I do wrong?

@SuppressWarnings("unused")
public static void main(String[] args) {

    String message = "ABCDEFGHILMNOPQRSTU hello giordano WORLD HOW wxyZ XyZ XYZ YOU  xyz";

    System.out.println(message);
    String encr = encrypt(message);
    String decr = decrypt(encr);

    System.out.println(encr + "\n" + decr);

}



@SuppressWarnings("unused")
private static String encrypt(String message) {
    StringBuilder temp = new StringBuilder();

    for(int i=0; i<message.length(); i++) {
        char c = (char)(message.charAt(i) + 3);

        if(c >= 'x') {
            c = (char)(message.charAt(i) - 23);
        } else {
            c = (char)(message.charAt(i) + 3);
        }

        temp.append(c);
    }

    return temp.toString();
}



private static String decrypt(String message) {
    StringBuilder temp = new StringBuilder();

    for(int i=0; i<message.length(); i++) {
            char c = (char)(message.charAt(i) - 3);

        // HELLO WORLD
        // KHOOR ZRUOG

        if(c > 'x') {
            c = (char)(message.charAt(i) + 26);
        } else {
            c = (char)(message.charAt(i) - 3);
        }

        temp.append(c);
    }

    return temp.toString();
}

OUTPUT:

Text: ABCDEFGHILMNOPQRSTU hello giordano WORLD HOW wxyZ XyZ XYZ YOU  xyz
Encrypted: DEFGHIJKLOPQRSTUVWX#khoor#jlrugdqr#ZRUOG#KRZ#`ab]#[b]#[\]#\RX##abc
Decrypted: ABCDEFGHILMNOPQRSTU hello giordano WORLD HOW ]^_Z X_Z XYZ YOU  ^_`

EXPECTED OUTPUT:

Text: ABCDEFGHILMNOPQRSTU hello giordano WORLD HOW wxyZ XyZ XYZ YOU  xyz
Encrypted: DEFGHIJKLOPQRSTUVWX#khoor#jlrugdqr#ZRUOG#KRZ#`ab]#[b]#[\]#\RX##abc
Decrypted: ABCDEFGHILMNOPQRSTU hello giordano WORLD HOW wxyZ XyZ XYZ YOU  xyz
  • Please describe the exact intention of your code how exactly should your plain text be shifted what is the intention of each line of your code - maybe this way you solve your own question. – baxbear Nov 26 '19 at 18:01
  • Shift = 3. Basically I'm developing a software server/client where the client sends a message to the server. The client transfers the encrypted message and the server decrypts it. – Lorenzo Orlando Nov 26 '19 at 18:06

2 Answers2

1

The issue you are facing only for small case is because you have written this code in encrypt

        if(c >= 'x') {
            c = (char)(message.charAt(i) - 23);
        } else {
            c = (char)(message.charAt(i) + 3);
        }

and this one for decrypt

        if(c > 'x') {
            c = (char)(message.charAt(i) + 26);
        } else {
            c = (char)(message.charAt(i) - 3);
        }

they only handle small 'x' while for capital 'X' you have not written any such condition. So if you remove above mentioned code from your function it will start working. But this will not caesar-cipher, for that matter you can start looking by "how to traverse in cyclic order".

Himanshu Singh
  • 2,117
  • 1
  • 5
  • 15
0

After Searching and Testing found that is the correct way to decoding.

private static String decrypt(String message) {
        StringBuilder temp = new StringBuilder();
       //key = 4
        for(int i=0; i<message.length(); i++) {
                char  c = (char)((message.charAt(i) - 4 - 97) %26 +97);
                if(c < 'a') {
                    c = (char) (message.charAt(i) - 4 - 'a' + 'z' + 1);
                }
            temp.append(c);
        }

        return temp.toString();
    }