Doogledude123 45 Posting Whiz

Alright, so I have been coding a Ceasar Cipher in Java and I have the encrypting working, However, I cannot seem to find my problem decrypting. Here is the code, I thought just by revearsing all the operations it would decode it, but that doesnt seem to be the case.

private static String Encrypt(String text, String key)
    {
        char[] cryptedText = new char[text.length()];
        char[] textArray = new char[text.length()];
        int[] keyArray = new int[key.length()];

        for(int i = 0; i < text.length(); i++)
        {
            textArray[i] = text.charAt(i);
        }

        for(int i = 0; i < key.length(); i++)
        {
            keyArray[i] = key.charAt(i);

            char c = textArray[i];
            int s = keyArray[i];
            if (c >= 32 && c <= 127)
            {
                int x = c - 32;
                x = (x + s) % 96;
                if (x < 0) 
                    x += 96;
                cryptedText[i] = (char) (x + 32);
            }
        }

        return new String(cryptedText);
    }

    private static String Decrypt(String text, String key)
    {
        char[] decryptedText = new char[text.length()];
        char[] textArray = new char[text.length()];
        int[] keyArray = new int[key.length()];

        for(int i = 0; i < text.length(); i++)
        {
            textArray[i] = text.charAt(i);
        }

        for(int i = 0; i < key.length(); i++)
        {
            keyArray[i] = key.charAt(i);

            char c = textArray[i];
            int s = keyArray[i];
            if (c >= 32 && c <= 127)
            {
                int x = c + 32;
                x = (x - s) % 96;
                if (x < 0) 
                    x += 96;
                decryptedText[i] = (char) (x - 32);
            }
        }

        return new String(decryptedText);
    }

SOLVED:

    int x = c - 32;
    x = (x - s) % 96;
    if (x < 0) 
        x += 96;
    decryptedText[i] = (char) (x + 32);

In Decrypt.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.