简体   繁体   中英

Need help troubleshooting my VigenereCipher java code

Okay so I created this code last year for a class project and I remember it working correctly. I now need it to implement a text cipher but for some reason it does not work correctly. It will encrypt, but when I try to decrypt only the first two letters are correct. The rest of it is all wrong. It is very simple, it is a command-line program where the first argument is whether it is encrypting(-e) or decrypting(-d), second argument is the key and third argument is the text you will encrypt. It is similar to a caesar cipher except it takes each character as a reference when adding to each individual char in the string. Can anyone tell me what is wrong, I do not understand why it does not work anymore and I need it for a project.

import java.util.*;

public class VigenereCipher 
{
    public static void main(String[] args) 
    {
        Scanner scan = new Scanner (System.in);
        String key = "";
        String ori = "";

        String res = "";

        if(!(args.length == 0)) 
        {
            if (args[0].equals("-e"))
            {
                key = args[1];
                ori = args[2];
                encrypt(ori, key);
                System.out.println(encrypt(ori, key));

            }
            else if (args[0].equals("-d"))
            {
                key = args[1];
                ori = args[2];
                decrypt(ori, key);
                System.out.println(decrypt(ori, key));

            }
           else
           {
            System.out.print("Usage: java VigenereCipher [-e,-d] key text");
           }


        }


    }



    static String encrypt(String text, final String key) 
    {
        String res = "";
        text = text.toUpperCase();
        for (int i = 0, j = 0; i < text.length(); i++) 
        {
            char c = text.charAt(i);
            if (c < 'A' ||  c > 'Z') continue;
            res += (char)((c + key.charAt(j) - 2 * 'A') % 26 + 'A');
            j = ++j % key.length();
        }
        return res;
    }

    static String decrypt(String text, final String key)
      {
        String res = "";
        text = text.toUpperCase();
        for (int i = 0, j = 0; i < text.length(); i++)
        {
            char c = text.charAt(i);
            if (c < 'A' || c > 'Z') continue;
            res += (char) ((c - key.charAt(j) + 26) % 26 + 'A');
            j = ++j % key.length();
        }
        return res;
    }
}

You are supposed to be able to encrypt a string of text with a key then decrypt the output from the encryption using the same key for example: java VigenereCipher -e hello hello will give me "UOCCI" as output but when I take that output and do java VigenereCipher -d hello UOCCI it gives me "HE225" as my output and not "HELLO".

You forgot that your key also needs to be in the same alphabet. So if you supply a lowercase key your algorithm will fail.

This will become abundantly clear when you split your algorithm in parts, eg I just went through:

int im = c + key.charAt(j) - 2 * 'A';
res += (char)(im % 26 + 'A');

with my debugger and presto, the problem showed up.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM