ScoutBlade
ScoutBlade

Reputation: 125

Errors with decryption code

I am trying to do a Vigenere cipher program, but I am having some trouble with the decrypting part of the code. The funny thing is that I thought if I got the encrypting part of the code to work, the decrypting, would just be reverse. For the most part, it is, but there is just one tiny problem.

Let me start off by explaining what the program is trying to do: My whole program is to get a text file from the user, and a key word( has to be letters and lower cases) and shift the individual letters of that text file to certain positions according to the key word. For this decryption part of the code, I only need to decrypt a single line, the other part of the program will take care of keep decrypting until it reaches the end of the text file. For simplicity, let's say the text is "oexl" and the key is "cats", if the decryption works, the result should be "meet", but instead my code ends up with "meeZ"

I know what went wrong, but I am not really sure how to fix it. I tried various ways, and the end results kept getting worse.

for the problem part of the code, I tried replacing it with this for the if statement

Character.isLetter(text2) && line.charAt(i) >= 97 && line.charAt(i) <= 122 && text1 >=65 && text1 <=90

That didn't work.

Another idea that I came up with is an if statement that is first testing to see if the the original character from the text is a letter, if it is, subtract that character by the shift position and if that number is less than 97 (this is the ascii value for a), then add 25 to that number to bring it to z. For example. let's say the original letter is l ( ascii value 108), key is s (ascii value 115, sutract that number by ascii value of a ( 97), the result 18, that means the letter has to shift by 18 position). What I do next is subtract 18 from 108(l), I get 90. Afterward, I compare this number to 97 (ascii value for lower case a) Since 90 is less than 97, what the program should do is add 25 to 108, which I get 133. Subtract 113 by 18(the number of places the text needs to shift), I get 116, which is ascii value for t. The problem is, I am not really sure how to implement this code.

Thank you very much in advance for your help.

        if (Character.isLetter(line.charAt(i))){


        int text1 =  line.charAt(i) - (key.charAt(j%(key.length()))-'a');  

        char text2 =(char)text1;
        String text3 = Character.toString(text2);
        text4 += text3;
        System.out.println(text4);


        if (text1<= 65 || (text1 >90 && text1 <97)) {
        text1 = text1 + 26;
         text2 =(char)text1;
         text3 = Character.toString(text2);
        text4 += text3;

        }
        j++;

Upvotes: 0

Views: 132

Answers (1)

fgb
fgb

Reputation: 18569

Your condition just needs to check if the character was lowercase and is now < 'a', or was uppercase and is now < 'A':

if (text1 < 'A' || (text1 < 'a' && line.charAt(i) > 'a')) {
    text1 = text1 + 26;
    text2 = (char)text1;
    text3 = Character.toString(text2);
    text4 += text3;
}

Edit: I'd fit it in with the rest of the loop like:

for(int i = 0; i<= line.length()-1; i++) {
    if (Character.isLetter(line.charAt(i))) {
        int text1 =  line.charAt(i) - (key.charAt(j%(key.length()))-'a');  

        if (text1 < 'A' || (text1 < 'a' && line.charAt(i) > 'a')) {
            text1 = text1 + 26;
        }
        text4 += (char)text1;
        j++;
    } else {
        text4 += line.charAt(i);
    }
}

Upvotes: 1

Related Questions