-1

I'm trying to encrypt a txt file, but when i send my chars to array I lose my spaces. I want to keep my spaces along with punctuation and cases of letters. I am so close but cannot seem to do anything that doesn't make A.) everything a null character or B.) loop capital letters. Thanks in advance.

public class Encryption {
     CaesarCipher c= new CaesarCipher();
     Scanner kb = new Scanner(System.in);
     String end = "";

public void changeLetters(File file) {
    System.out.println("How far would you like to shift?");
    int shift = Integer.parseInt(kb.nextLine());
    Scanner fileScanner;
    try {
        fileScanner = new Scanner(file);
        while (fileScanner.hasNextLine()) {
            String line = fileScanner.nextLine();
            shift(line, shift);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}

private void shift(String line, int shift) {
    char[] og = line.toCharArray();
    for (int i = 0; i < og.length; i++) {
        char letter = og[i];
        letter = (char) (letter + shift);
        if (letter > 'z') {
            letter = (char) (letter - 26);
        } else if (letter < 'a') {
            letter = (char) (letter + 26);
        }
        end = end + Character.toString(letter);
    }
    System.out.println(end);

    File file = new File("Encrypted.txt");
    FileWriter writer = null;

    {
        try {
            writer = new FileWriter(file);
            writer.write(end);
            writer.close();
        } catch (

        IOException e)

        {
            e.printStackTrace();
        }
        System.out.println("Decryption Complete");
        System.out.println("Q to quit, C to Continue");
        String response = kb.next();
        if (response.equals("q") || response.equals("Q")) {
            System.out.println("Goodbye");
        } else if (response.equals("c") || response.equals("C")) {
            c.getInformation();
        }
    }
}

1 Answers1

0

I believe the problem comes from the fact you are adding (+/-) 26 to your letter, for example letter = (char) (letter - 26);. This would only work within the alphabet [a-z]. However as you want to be able to handle capital letters, special characters and such you can't do this.

It would also be cleaner to use the modulo operator % in order to do this. Hence you won't have to make an explicit test, like you did if (letter > 'z').

Here is the shift procedure, which is really simple

private String shift(String str, int shift) {
    String shifted = "";
    for(int i = 0; i < str.length(); i++) {
        char original = str.charAt(i);
        char shiftedChar = (char) ((original + shift) % Integer.MAX_VALUE);
        shifted += shiftedChar; // Append shifted character to the end of the string
    }

    return shifted;
}

However i'm not sure this is the modulus to use. But i did some tests and this seemed to work.

Here is how you can shift and unshift

String test = "This is a test!";
String encoded = shift(test, 3);
String decoded = shift(encoded, -3);

System.out.println("Encoded : " + encoded + "\n" + "Decoded : " + decoded);
Kevin
  • 2,813
  • 3
  • 20
  • 30