简体   繁体   English

需要帮助解决我的VigenereCipher Java代码

[英]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. 这很简单,它是一个命令行程序,其中第一个参数是加密(-e)还是解密(-d),第二个参数是密钥,第三个参数是您要加密的文本。 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". 您应该能够使用密钥对文本字符串进行加密,然后使用相同的密钥对加密输出进行解密,例如: java VigenereCipher -e hello hello将给我“ UOCCI”作为输出,但是当我使用该输出时,做java VigenereCipher -d hello UOCCI它给了我“ HE225”作为我的输出,而不是“ 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. 用我的调试器和presto,问题出现了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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