0

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++;
2
  • possible duplicate of Vigenere Cipher program in Java Commented Mar 25, 2012 at 16:49
  • That was me too. I was working on the encryption part of my program. This one is is about the decryption. For the most parts, they are the same, but I am having a little difficult with just a tiny part of the code. Commented Mar 25, 2012 at 16:54

1 Answer 1

1

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);
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

Yes, yes. Thank you so much. That solved this problem. However, another problem came up while I was doing some testing. I noticed that for every letter that needs to go around, the end result always end up with an extra capital letter in front of that letter. Let's say the text is Oexl and the key is cats, the result should me Meet, but the actual result is MeeZs.So it is off by one and there is always an extra capital letter in front of a character that needs to go around.And same thing happened to my encryption part of the code too. Can you please look at the code and see what's wrong?Thanks
I did some testing, I think I know why there was an extra Capital letter in front of the character that needs to go around. Again the text is Oexl, and the key is cats. So for the first if statement, it keeps on going until it reaches l. Now the program shift the character and it is Z. Then the program goes to the second if statement and do the testing and see that it is Z. Then it adds text1 by 26 to go around. What the program needs to do is discard the capital Z before attaching t to the end of Mee. I am trying to see if I remember the code for discard a character.
@ScoutBlade The code has "text4 += text3" twice. You can do it once after the if, or as part of the else for the if instead. The off by one is because you're adding 25 instead of 26.
Thanks for pointing that out. I get what you are saying. But I am having trouble moving the text4=text3 around so that the program only implement it once. I tried various ways of moving that code, and errors kept on popping up. Last request, can you show me how I should move the code. Thank you so so much for your input.
I tried different ways of moving my code. I noticed that if I move if ( !(Character.isLetter(line.charAt(i)))) { this if statement to the top, instead of bottom, the result is now MeeZ, there is no t at the end like before.(See the updated code). I am trying to retrace my code.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.