Ceasar Cipher解密方法不起作用,什么都不打印

时间:2017-04-15 19:34:13

标签: java encryption intellij-idea caesar-cipher

我对编程很陌生,无法用我的Cesar密码解决问题我的加密方法工作正常,但我的解密方法无效。

主要课程

package com.company;

import java.util.Scanner;

public class Main {
    public ceasarCipher ceasarCipher;

    public static void main(String[] args) {

        com.company.ceasarCipher utilityClass = new ceasarCipher();
        int inShift;
        String inString;
        String encryptedString = "";

        Scanner sca = new Scanner(System.in);

        System.out.println("Please enter a string to encrypt");
        inString = sca.nextLine();

        System.out.println("=======================================================================================");
        System.out.println("In this program");
        System.out.println("Press 1 to encrypt your string by shifting it forward a certain amount");
        System.out.println("Press 2 to decipher your string by shifting it backwards a certain amount");
        System.out.println("Press 3 to give you all possible combinations of the shift in order to decipher");
        System.out.println("Press 4 to exit the program");
        System.out.println("=======================================================================================");

        while(!sca.hasNext("x") && !sca.hasNext("X")){

            int userInput;
            userInput = sca.nextInt();

            switch (userInput){

                case 1:
                    System.out.println("Please enter the amount greater than 0 that you would like your String to be shifted forwards in order to encrypt it");
                    inShift = sca.nextInt();
                    System.out.println(utilityClass.ccEncrypt(inShift,inString));
                case 2:
                    System.out.println("Please enter the amount greater than 0 that you would like your String to be shifted backwards in order to decipher it");
                    inShift = sca.nextInt();
                    System.out.println(utilityClass.ccDecipher(inShift,encryptedString));
            }
        }
    }
}

带方法的子类

package com.company;

/**
 * Created by Jake on 14/04/2017.
 */
public class ceasarCipher {

    private int maxVal = 26;

    //private int inShift;
    //private String inString;
    private String letters = "abcdefghijklmnopqrstuvwxyz";

    //-----------------------------------------------------------
    public String ccEncrypt (int inShift, String inString) {

        String encryptedString = "";
        char modulo;
        int pos;

        inString = inString.toLowerCase();

        for (int i = 0; i < inString.length(); i++) {

            pos = letters.indexOf(inString.charAt(i));

            modulo = this.letters.charAt((pos + inShift) % maxVal);

            encryptedString += modulo;
        }
        return encryptedString;
    }
    //----------------------------------------------------------------------

    public String ccDecipher (int inShift, String encryptedString) {

        String decipheredString = "";
        char modulo;
        int pos;

        for (int i = encryptedString.length() - 1; i >= 0; i--) {

            pos = letters.indexOf(encryptedString.charAt(i));

            modulo = this.letters.charAt((pos - inShift) % maxVal);

            decipheredString += modulo;
        }
        return decipheredString;
    }
    // -----------------------------------------------------------------------

    public void ccDecipherAll (String inString){

    }
}

1 个答案:

答案 0 :(得分:1)

您永远不会向encryptedString分配任何内容。你应该使用inString

System.out.println(utilityClass.ccDecipher(inShift, inString));

如果您想循环执行此操作,您可能希望每次都更新inString

inString = utilityClass.ccDecipher(inShift, inString); //same for encrypt
System.out.println(inString);

然后,否定的模数不会做你想要的:

请:

(pos - inShift) % maxVal

分为:

(pos - (inShift % maxVal) + maxVal) % maxVal

确保您不会采用模数为负数。

您还要按向后顺序解码。