不能将Caesar Cipher移动超过1

时间:2017-01-11 00:24:26

标签: java caesar-cipher

我是Java的新手,我必须制作一个Caesar Cipher程序。这是我的代码。我遇到的问题是我不能将它移动超过1.例如,如果我写'帮助我'#39;并输入2的移位值它仍然只移动1 - > ' ifmq nf'

  public static void main(String[] args) 
    {
        Scanner sc=new Scanner(System.in);
        String str;
        String key;
        int keyLength;

        System.out.println("Enter message:");
        str=sc.nextLine();
        System.out.println("Enter encryption key:");
        key=sc.next();
        keyLength=key.length();
        //This for loop is repeated use of 'Enrypt' and 'Decrypt' options
        for(;;)
        {
            System.out.println("1.Encrypt\n2.Decrypt\n3.Exit...");
            int choice=sc.nextInt();
            switch(choice)
            {
                case 1:
                /*send input string keyLength to encrypt() method to encrypt it returns 'Encrypted' string*/
                    System.out.println("Encrypted message..."+encrypt(str,keyLength));
                    break;
                case 2:
                    //send retrived string from encrypt() method and keyLength to decrypt() method it returns 'Decrypted' string
                    System.out.println("Decryptedmessage..."+decrypt(encrypt(str,keyLength),keyLength));
                    break;
                case 3:
                    //exit from the program
                    System.exit(0);
                    break;
                default:
                System.out.println("Invalid option..");
            }
        }
    }
    public static String encrypt(String str,int keyLength)
    {
        String encrypted="";
        for(int i=0;i<str.length();i++)
        {
            //stores ascii value of character in the string at index 'i'
            int c=str.charAt(i);
            //encryption logic for uppercase letters
            if(Character.isUpperCase(c))
            {
                c=c+(keyLength%26);
                //if c value exceeds the ascii value of 'Z' reduce it by subtracting 26(no.of alphabets) to keep in boundaries of ascii values of 'A' and 'Z'
                if(c>'Z')
                    c=c-26;
            }
            //encryption logic for lowercase letters
            else if(Character.isLowerCase(c))
            {
                c=c+(keyLength%26);
                //if c value exceeds the ascii value of 'z' reduce it by subtracting 26(no.of alphabets) to keep in boundaries of ascii values of 'a' and 'z'
                if(c>'z')
                    c=c-26;
            }
            //concatinate the encrypted characters/strings
            encrypted=encrypted+(char) c;
        }
        return encrypted;
    }
    public static String decrypt(String str,int keyLength)
    {
        String decrypted="";
        for(int i=0;i<str.length();i++)
        {
            //stores ascii value of character in the string at index 'i'
            int c=str.charAt(i);
            //decryption logic for uppercase letters
            if(Character.isUpperCase(c))
            {
                c=c-(keyLength%26);
                //if c value deceed the ascii value of 'A' increase it by adding 26(no.of alphabets) to keep in boundaries of ascii values of 'A' and 'Z'
                if(c<'A')
                    c=c+26;
            }
            //decryption logic for uppercase letters
            else if(Character.isLowerCase(c))
            {
                c=c-(keyLength%26);
                //if c value deceed the ascii value of 'A' increase it by adding 26(no.of alphabets) to keep in boundaries of ascii values of 'A' and 'Z'
                if(c<'a')
                    c=c+26;
            }
            //concatinate the decrypted characters/strings
            decrypted=decrypted+(char) c;
        }
        return decrypted;
    }

1 个答案:

答案 0 :(得分:0)

正如Scary Wombat和MadProgrammer所说,在第12行你有keyLength=key.length();。这会返回您输入的String长度。因此,如果输入2,则keyLength为1.如果输入11,则keyLength为2

我会完全摆脱keyLength,将key改为int而不是String,并设置key=sc.nextInt();

这将允许1为1,2,为2,依此类推。

请务必将keyLength的所有实例更改为key