我正在尝试编译此代码,但我得到了cannot find symbol - variable alphabet
。我不明白为什么,因为alphabet
被宣布为其价值,所以很明显。我有什么东西在这里失踪吗?我的代码旨在通过使用两个密钥来加密和解密消息,这两个密钥通过将字母分开来使每个其他字母由从索引0开始的一个密钥加密,并且索引1中的每个其他字母由另一个密钥加密。计算这两组字母并将它们编织在一起以形成一个解密的字符串。无论如何,我只对找出cannot find symbol - variable alphabet
错误的原因感兴趣。对我的代码的任何其他建议表示赞赏,但不是必需的。谢谢!
public class CaesarCipherTwo {
private int encrypt_key = 0;
private int key1 = -1;
private int key2 = -1;
private CaesarCipher cc1;
private CaesarCipher cc2;
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);
alphabet = alphabet_uppercase + alphabet_lowercase;
e_alphabet = e_alphabet_uppercase + e_alphabet_lowercase;
}
/**
* Setter function for key used by cipher
* @param key
* input key
**/
public void set_key(int key){
this.encrypt_key = key;
this.generateEncryptionLookup();
}
/**
* 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();
cc1.set_key(key1);
cc2 = new CaesarCipher();
cc2.set_key(key2);
}
/**
* 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();
}
}
答案 0 :(得分:0)
您应该将这些变量alphabet
和e_alphabet
写成global
变量,例如:
String alphabet = "";
String e_alphabet = "";
private void generateEncryptionLookup(){...