-2

I am very new to programming and can't figure out the issue with my Cesar cipher my method for encryption works fine but my method for decryption is not working.

Main class

package com.company;

import java.util.Scanner;

public class Main {
    public ceasarCipher ceasarCipher;

    public static void main(String[] args) {

        com.company.ceasarCipher utilityClass = new ceasarCipher();
        int inShift;
        String inString;
        String encryptedString = "";

        Scanner sca = new Scanner(System.in);

        System.out.println("Please enter a string to encrypt");
        inString = sca.nextLine();

        System.out.println("=======================================================================================");
        System.out.println("In this program");
        System.out.println("Press 1 to encrypt your string by shifting it forward a certain amount");
        System.out.println("Press 2 to decipher your string by shifting it backwards a certain amount");
        System.out.println("Press 3 to give you all possible combinations of the shift in order to decipher");
        System.out.println("Press 4 to exit the program");
        System.out.println("=======================================================================================");

        while(!sca.hasNext("x") && !sca.hasNext("X")){

            int userInput;
            userInput = sca.nextInt();

            switch (userInput){

                case 1:
                    System.out.println("Please enter the amount greater than 0 that you would like your String to be shifted forwards in order to encrypt it");
                    inShift = sca.nextInt();
                    System.out.println(utilityClass.ccEncrypt(inShift,inString));
                case 2:
                    System.out.println("Please enter the amount greater than 0 that you would like your String to be shifted backwards in order to decipher it");
                    inShift = sca.nextInt();
                    System.out.println(utilityClass.ccDecipher(inShift,encryptedString));
            }
        }
    }
}

Subclass with methods

package com.company;

/**
 * Created by Jake on 14/04/2017.
 */
public class ceasarCipher {

    private int maxVal = 26;

    //private int inShift;
    //private String inString;
    private String letters = "abcdefghijklmnopqrstuvwxyz";

    //-----------------------------------------------------------
    public String ccEncrypt (int inShift, String inString) {

        String encryptedString = "";
        char modulo;
        int pos;

        inString = inString.toLowerCase();

        for (int i = 0; i < inString.length(); i++) {

            pos = letters.indexOf(inString.charAt(i));

            modulo = this.letters.charAt((pos + inShift) % maxVal);

            encryptedString += modulo;
        }
        return encryptedString;
    }
    //----------------------------------------------------------------------

    public String ccDecipher (int inShift, String encryptedString) {

        String decipheredString = "";
        char modulo;
        int pos;

        for (int i = encryptedString.length() - 1; i >= 0; i--) {

            pos = letters.indexOf(encryptedString.charAt(i));

            modulo = this.letters.charAt((pos - inShift) % maxVal);

            decipheredString += modulo;
        }
        return decipheredString;
    }
    // -----------------------------------------------------------------------

    public void ccDecipherAll (String inString){

    }
}
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
Jake77
  • 1
  • What do you mean by *"printing nothing"*? Does it at least print "Please enter a string to encrypt"? If so, please [edit] your question to show the actual output along with your inputs. – Artjom B. Apr 15 '17 at 19:40
  • Yes it does to be more specific it isnt printing my decryption method in case 2 – Jake77 Apr 15 '17 at 19:41
  • So, you enter the ciphertext and then the program ends? This looks like an ArrayIndexOutOfBoundsException situation where weston's answer is the appropriate way to solve that. Do you see this exception anywhere? Look closely. – Artjom B. Apr 15 '17 at 19:45
  • i haven't seen it yet and it doesn't end, theres just nothing there. if I change the decipheredString = null it sometimes prints out just null. What exactly is an ArrayIndexOutofBoundsException as I don't think I have an array. – Jake77 Apr 15 '17 at 19:50
  • Sorry, I meant just an IndexOutOfBoundsException as shown in the documentation: https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#charAt(int) – Artjom B. Apr 15 '17 at 19:52
  • Hint: simply put the exception name into Google. For each of those basic things there is a lot of documentation available. – GhostCat Apr 15 '17 at 20:22
  • @GhostCat Reopened, the dupe didn't make sense. OP hasn't mentioned an NPE. – weston Apr 15 '17 at 20:47
  • Still can't figure it out I think it might be an NPE but not sure – Jake77 Apr 15 '17 at 22:47
  • @weston He had npe put in now deleted comments twice... – GhostCat Apr 16 '17 at 03:03
  • 1
    Hint: where exactly are you assigning the user input to `encryptedString`? – Artjom B. Apr 16 '17 at 07:09

1 Answers1

1

You never assign anything to encryptedString. You should use inString perhaps.

System.out.println(utilityClass.ccDecipher(inShift, inString));

And if you want to do this in a loop, you might want to update that inString each time:

inString = utilityClass.ccDecipher(inShift, inString); //same for encrypt
System.out.println(inString);

Then, modulo of a negative doesn't do what you want it to:

Make:

(pos - inShift) % maxVal

Into:

(pos - (inShift % maxVal) + maxVal) % maxVal

To ensure you're not taking modulo of a negative.

You're also decoding in a backwards order.

weston
  • 54,145
  • 21
  • 145
  • 203