Java Caesar Cipher

时间:2017-09-27 00:59:03

标签: java caesar-cipher

在这个Caesar密码中,我获得了大写字母的错误输出。 代码如下:

public class CaesarCipherCaseSensitive
{
public static final String ALPHABET = "abcdefghijklmnopqrstuvwxyz";

public static String encrypt(String pt, int shiftKey)
{
    String ct = "";
    for (int i = 0; i < pt.length(); i++)
    {
        int charPosition = ALPHABET.indexOf(pt.charAt(i));
        int keyVal = (shiftKey + charPosition) % 26;
        char replaceVal = ALPHABET.charAt(keyVal);
        ct += replaceVal;
    }
    return ct;
}

public static String decrypt(String ct, int shiftKey)
{
    String pt = "";
    for (int i = 0; i < ct.length(); i++)
    {
        int charPosition = ALPHABET.indexOf(ct.charAt(i));
        int keyVal = (charPosition - shiftKey) % 26;
        if (keyVal < 0)
        {
            keyVal = ALPHABET.length() + keyVal;
        }
        char replaceVal = ALPHABET.charAt(keyVal);
        pt += replaceVal;
    }
    return pt;
}

public static void main(String[] args)
{
    String message1 = "ABCDEFGHIJKLMOPQRSTUVWXYZ";
    System.out.println(encrypt(message1, 3));
    System.out.println(decrypt(encrypt(message1, 3), 3));
    System.out.println(encrypt(message1.toLowerCase(),5));   
    System.out.println(decrypt(encrypt(message1.toLowerCase(),5),5));  

}
}

输出如下: ccccccccccccccccccccccccc zzzzzzzzzzzzzzzzzzzzzzzzz fghijklmnopqrtuvwxyzabcde abcdefghijklmopqrstuvwxyz

所需的输出是: DEFGHIJKLMOPQRSTUVWXYZABC ABCDEFGHIJKLMOPQRSTUVWXYZ fghijklmnopqrtuvwxyzabcde abcdefghijklmopqrstuvwxyz

1 个答案:

答案 0 :(得分:0)

问题是你是将大写字母传递给方法indexOf(),它检查ALPHABET但找不到它,所以它返回-1。您需要将大写字母转换为小写字母,或者添加大写字母。

我的解决方案,将大写字母转换为小写字母然后加密/解密是:

public class CaesarCipherCaseSensitive{
public static final String ALPHABET = "abcdefghijklmnopqrstuvwxyz"; 

public static String encrypt(String pt, int shiftKey){
    String ct = "";
    for (int i = 0; i < pt.length(); i++){
        char letter = pt.charAt(i);
        boolean upperCase = false;
        if((int)letter < 91){
            letter = (char)((int)letter + 32);
            upperCase = true;
        }
        int charPosition = ALPHABET.indexOf(letter);
        int keyVal = (shiftKey + charPosition) % 26;
        char replaceVal = ALPHABET.charAt(keyVal);
        if(upperCase){
            replaceVal = (char)((int)replaceVal - 32);
        }
        ct += replaceVal;
    }
    return ct;
}

public static String decrypt(String ct, int shiftKey){
    String pt = "";
    for (int i = 0; i < ct.length(); i++){
        char letter = ct.charAt(i);
        boolean upperCase = false;
        if((int)letter < 91){
            letter = (char)((int)letter + 32);
            upperCase = true;
        }
        int charPosition = ALPHABET.indexOf(letter);
        int keyVal = (charPosition - shiftKey) % 26;
        if (keyVal < 0){
            keyVal = ALPHABET.length() + keyVal;
        }
        char replaceVal = ALPHABET.charAt(keyVal);
        if(upperCase){
            replaceVal = (char)((int)replaceVal - 32);
        }
        pt += replaceVal;
    }
    return pt;
}

public static void main(String[] args){
    String message1 = "ABCDEFGHIJKLMOPQRSTUVWXYZ";
    System.out.println(encrypt(message1, 3));
    System.out.println(decrypt(encrypt(message1, 3), 3));
    System.out.println(encrypt(message1.toLowerCase(),5));   
    System.out.println(decrypt(encrypt(message1.toLowerCase(),5),5));  
}
}