用于java中大写和小写输入的caesar密码

时间:2016-05-27 05:43:22

标签: java

我在Caeser密码中遇到问题,我的程序适用于小写输入。如何处理大小写输入。提前致谢。 这是我的代码:

public class CeaserCipher {

public static String encrypt(String input,int key){
    String alphabet="abcdefghijklmnopqrstuvwxyz";
    StringBuilder encrypted= new StringBuilder(input);
    String shiftedalphabet = alphabet.substring(key)+alphabet.substring(0,key);
    for(int i=0;i<encrypted.length();i++){
        char currentchar=encrypted.charAt(i);
        int indx=alphabet.indexOf(currentchar);
    if(indx!=-1){
            char newchar=shiftedalphabet.charAt(indx);
            encrypted.setCharAt(i, newchar);
        }
    }
    return encrypted.toString();
}

public static void main(String args[]){

        Scanner sc = new Scanner(System.in);
        System.out.println("enter the message to encrypt and key");
        String message= new String();
        int key=17;

        message=sc.nextLine();
        System.out.println(encrypt(message,key));
        }
} 

0 个答案:

没有答案