无法找到密钥的符号错误

时间:2016-01-04 04:23:00

标签: java encryption

我正在尝试编译我的代码但是我收到一条错误,指出cannot find symbol - variable key。我试图解密来自我的文件的消息,并且似乎无法获得正确的void方法来运行所有这些代码。我希望找出这个小问题将导致我能够正确运行我的代码,并使解密/加密正确。注意:非常感谢任何关于如何开发空洞测试方法的建议。只是我专注于为什么我首先得到这个错误信息。它就在this.encrypt_key = key;这一行。

public class CaesarCipherTwo {
    private int encrypt_key = 0;
    private int key1 = -1;
    private int key2 = -1;
    private CaesarCipher cc1;
    private CaesarCipher cc2;


    /**
     * Constructor - Takes in the two keys used for encryption. Use this constructor when you want to use this object for encryption (& then subsequent 
     * decryption of same string)
     * 
     * @param key1
     *              Key1 used in encryption process
     * @param key2
     *              Key2 used in encryption process
     */


    public CaesarCipherTwo(int key1, int key2){

        this.key1 = key1;
        this.key2 = key2;
        cc1 = new CaesarCipher();

        cc2 = new CaesarCipher();


    }
     private void generateEncryptionLookup(){
        String alphabet_uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        String alphabet_lowercase = "abcdefghijklmnopqrstuvwxyz";   

        String e_alphabet_uppercase = alphabet_uppercase.substring(encrypt_key) + alphabet_uppercase.substring(0,encrypt_key);
        String e_alphabet_lowercase = alphabet_lowercase.substring(encrypt_key) + alphabet_lowercase.substring(0,encrypt_key);


        String alphabet = alphabet_uppercase + alphabet_lowercase;
        String e_alphabet = e_alphabet_uppercase + e_alphabet_lowercase;
    }
    private String set_key;
    public void set_key(String set_key) {

        this.encrypt_key = key;
        this.generateEncryptionLookup();
        cc1.set_key(key1);
        cc2.set_key(key2);
    }
    public String decryptString(int mode){

        int key = -1;
        int current_key = this.get_key();

        if (mode == 1){
            key = 26 - encrypt_key;

        } else if (mode == 2){
            this.get_char_freq();
            key = this.compute_shift(this.get_max_char_freq());

        }

        this.set_key(key); //this statement internally mutates the key which is not a good idea

        String decryptedString = this.encryptString();
        this.set_key(current_key); //this statement reverts the state of the Caesar Cipher
        return decryptedString;

    }
    /**
     * Constructor - To be used when you want to use this class for decryption & the method needs to figure out the decryption keys
     */
    public CaesarCipherTwo(){
        cc1 = new CaesarCipher();
        cc2 = new CaesarCipher();
    }

    /**
     * Takes a String as input and encrypts it using 2 keys
     * @param s 
     *          Input String
     * @return
     *          Encrypted String
     */

    public String encrypt(String s){
        cc1.set_string_under_action(s);
        cc2.set_string_under_action(s);
        String encrypted1 = cc1.encryptString();
        String encrypted2 = cc2.encryptString();
        String encrypted = CaesarCipher.interleave(encrypted1, encrypted2);
        return encrypted;

    }

    /**
     * Takes a String and decrypts it using 2 keys
     * @param s
     *          String input for decryption
     * @param mode
     *          Accepts either 1 or 2 as input
     *          Mode 1 - Use the complement of key used for encryption while doing decryption. Its critical that this mode be used with the same
     *                   key settings as used while encrypting the String
     *          Mode 2 - The method figures out the right decryption key to be used 
     * @return
     *          Decrypted String
     */

    public String decrypt(String s, int mode){

        String [] decrypt = CaesarCipherTwo.halfOfString(s);
        cc1.set_string_under_action(decrypt[0]);
        cc2.set_string_under_action(decrypt[1]);
        String d1 = cc1.decryptString(mode);
        String d2 = cc2.decryptString(mode);
        String d = CaesarCipherTwo.weaveTwoStrings(d1, d2);
        return d;   
    }

    /**
     * Break a string into 2 (alternate chars in each of the 2 resultant strings)
     * @param s 
     *          Input String
     * @return
     *          Returns an array of 2 strings. These are the resultant strings from breaking the input string
     */

    public static String[] halfOfString(String s){
        StringBuilder sb1 = new StringBuilder(10000);
        StringBuilder sb2 = new StringBuilder(10000);
        String [] decrypt = new String[2];

        for (int i = 0; i<s.length();i++){
            if (i%2 == 0){
                sb1.append(s.charAt(i));
            }else {
                sb2.append(s.charAt(i));
            }
        }

        decrypt[0] = sb1.toString();
        decrypt[1] = sb2.toString();
        return decrypt;
    }

    /**
     * This method weaves 2 strings together into a single string. Letters from each of the input strings form alternate letters in final string
     * @param s1
     *          First input String
     * @param s2
     *          Second input String
     * @return
     *          Final resultant String
     */

    public static String weaveTwoStrings(String s1,String s2){
        int len = Math.max(s1.length(), s2.length());
        StringBuilder sb = new StringBuilder(10000);

            for (int i=0; i< len; i++){
                if (i < s1.length() && i < s2.length()){
                    sb.append(s1.charAt(i));
                    sb.append(s2.charAt(i));
                }else if (i < s1.length() && i >= s2.length()){
                    sb.append(s1.charAt(i));
                }else if (i >= s1.length() && i < s2.length()){
                    sb.append(s2.charAt(i)); 
                }
            }        
        return sb.toString();

    }
}

1 个答案:

答案 0 :(得分:0)

此时

private String set_key;
public void set_key(String set_key) {

    this.encrypt_key = key;     <<<*** "key" not defined here
    this.generateEncryptionLookup();
    cc1.set_key(key1);
    cc2.set_key(key2);
}

您尚未定义名为key的变量。