jedi nerd jedi nerd - 1 month ago 12
Java Question

Create a simple Caeser Cipher in Java

I need to create a cipher that will take a string from the user and a shift number that will change the character in the string and shift it the number the user entered. This is a project for one of my classes and I am stuck on what I am doing wrong. We have to do it with out using an array and have to use the constants declared at the beginning. Basically we can use what I already have within my code. I haven't finished the

decrypt
section. But that method should just revert the
encrypted
code back to the original.

import java.util.Scanner;


public class CaesarCipher {

public static final String UPPER_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
public static final String LOWER_ALPHABET = "abcdefghijklmnopqrstuvwxyz";
public static final String DIGITS = "0123456789";

public static String encrypt ( String plainText , int shift ) {

String cipherText = "";

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

char c = plainText.charAt(i);

int newShift = UPPER_ALPHABET.indexOf(c) + shift;

if (newShift < 0) {
newShift += 26;
}
else if (newShift > 25) {
int remainder = newShift % 25;
}
else if (newShift >= 0 && newShift < 26) {
newShift = newShift;
}
newShift = UPPER_ALPHABET.indexOf(c) + shift;
cipherText += UPPER_ALPHABET.charAt(newShift);
}
for (int i = 0; i < plainText.length(); i++) {

char c = plainText.charAt(i);

int newShift = LOWER_ALPHABET.indexOf(c) + shift;

if (newShift < 0) {
newShift += 26;
}
else if (newShift > 25) {
int remainder = newShift % 25;
}
else if (newShift >= 0 && newShift < 26) {
newShift = newShift;
}
newShift = LOWER_ALPHABET.indexOf(c) + shift;
cipherText += LOWER_ALPHABET.charAt(newShift);
}
for (int i = 0; i < plainText.length(); i++) {

char c = plainText.charAt(i);

int newShift = DIGITS.indexOf(c) + shift;

if (newShift < 0) {
newShift += 26;
}
else if (newShift > 25) {
int remainder = newShift % 25;
}
else if (newShift >= 0 && newShift < 26) {
newShift = newShift;
}
newShift = DIGITS.indexOf(c) + shift;
cipherText += DIGITS.charAt(newShift);
}
//System.out.println(cipherText);
return cipherText;
}

//public static String decrypt ( String cipherText , int shift ) {
// System.out.println(cipherText);
// return cipherText;
//}

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.println("Enter the String for Encryption: ");
String plainText = input.next();


System.out.println("Enter a positive interger between 0 and 25 to create our CeasarCipher");
int shift = input.nextInt();

if (shift < 0 || shift > 25) {
System.out.println("Don't be a jerk ... follow directions!");
System.exit(0);
}
else {
encrypt(plainText, shift);
}
}
}

Answer

Your code goes about this problem the wrong way. You treat every character as if it's uppercase, then lowercase, then a digit, augmenting the cipher text with three conversions for every character. You don't bother to test the result of .indexOf() to see if it wasn't in the character set before you shift it. You don't handle punctuation correctly (pass it through). Your modular arithmetic is off in several places and your main() routine doesn't read in lines of text correctly.

The problem is simpler than you're making it. Working within the constraints presented, here's a rewrite of your code that encrypts the text:

import java.util.Scanner;

public class CaesarCipher {

    public static final String UPPER_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    public static final String LOWER_ALPHABET = "abcdefghijklmnopqrstuvwxyz";
    public static final String DIGITS = "0123456789";

    public static String encrypt (String plainText, int shift) {

        String cipherText = "";

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

            char c = plainText.charAt(i);

            String alphabet = null;

            if (UPPER_ALPHABET.indexOf(c) > -1) {
                alphabet = UPPER_ALPHABET;
            } else if (LOWER_ALPHABET.indexOf(c) > -1) {
                alphabet = LOWER_ALPHABET;
            } else if (DIGITS.indexOf(c) > -1) {
                alphabet = DIGITS;
            }

            if (alphabet != null) {
                int length = alphabet.length();

                c += shift;

                while (c > alphabet.charAt(length - 1)) {
                    c -= length;
                }
            }

            cipherText += c;
        }

        return cipherText;
    }

    public static void main(String[] args) { 

        Scanner input = new Scanner(System.in);

        System.out.println("Enter the String for Encryption: ");
        String plainText = input.nextLine();

        System.out.print("Enter a positive integer between 0 and 25 to create our CeasarCipher: ");
        int shift = input.nextInt();

        if (shift < 0 || shift > 25) {
            System.out.println("Don't be a jerk ... follow directions!");
            System.exit(0);
        }

        System.out.println(encrypt(plainText, shift));
    } 
}

The decrypt method can be derived via trivial modifications to the encrypt method.