我在 Java 方面有一点经验,而且我只是 Android 环境的新手。我之前在 Java 中创建了一个加密算法(使用简单的移位密码)。此后,我进行了更改以使其成功融入 Android 代码,但我还没有让它完全正常工作。计划是让用户在一个 EditText 字段中输入纯文本,在第二个字段中输入一个键,在第三个字段中输入他们的电子邮件。单击“发送”按钮后,密文将发送到他们的电子邮件。下面,我在注释中包含了 Java 版本的主要方法(现在在我的 onCreate 方法中)。
public class ScreenNext extends Activity {
int key = 0;
static char ch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screen_next);
EditText emailT;//Import EditTexts (Key and Email)
Button send = (Button) findViewById(R.id.bSend);//Import button1 (Send) final EditText passT = (EditText) findViewById(R.id.etTogg);//passT variable for Password Text for EditText field
final EditText keyT = (EditText) findViewById(R.id.etKey);
final EditText passT = (EditText) findViewById(R.id.etTogg);//passT variable for Password Text for EditText field
//convert the JOption Panes now into Android equivalents
//String choice = JOptionPane.showInputDialog("(1) Encrypt \n"+ "(2) Close");
//String subKey = JOptionPane.showInputDialog(null, "Enter your ideal key");
//String message = JOptionPane.showInputDialog(null, "Enter message");
//String cText = subcipher_1.message(choice, subKey, message);
//JOptionPane.showMessageDialog(null, cText);//send cText as email
}//End onCreate
public static String message(String choice, String subKey, String message) {
int Option = Integer.parseInt(choice);//Must pareseInt
int key = Integer.parseInt(subKey);
message = message.toLowerCase();
//If the key is 26, prompt the user to change the key
if (key % 26 == 0) {
//Toast.makeText(getApplicationContext(), "You can't use a modulus of 26 as a key", Toast.LENGTH_LONG).show();
}
ScreenNext subcipher_1 = null;
String CipherTxt = subcipher_1.encrypt(message, key);
return CipherTxt;
}
// Message prompt method finished, now we create the method that has the
// encrypting algorithms
public static String encrypt(String Txt, int key) {
//local var cipherText of type string is init empty
String CipherTxt = "";//May be able to remove this'un
String cText="";
//enhanced for loop
// start at 0, go until "as long as input text"
for (int i = 0; i < Txt.length(); i++) {
//get a char from the string at index i (start at 0 work through to end of string)
// and store in local var extractedChar for type char
char extractedChar = Txt.charAt(i);
/* enhanced for loop
* start at 0, go until end of user entered cipherKeyValue
* either set to lowercase a or add one to the char
* uses the checkifz method
*/
for (int j = 0; j < key; j++) {
ScreenNext subcipher_1 = null;
if (subcipher_1.checkIfZ(extractedChar) == true) {
extractedChar = 'a';
} else {
extractedChar++;
}
CipherTxt= new StringBuilder().append(extractedChar).toString();
}
//add extracted char to builder object
//change object builder to string and assing to cipherText of type String
//create new object builder from StringBuilder class
cText = cText.concat(CipherTxt);
}
//Pass the cipherText value out of the method to whom ever called it
return cText;
}
// public method properCase, makes all strings lowercase
public String properCase(String input) {
if (input.length() == 0) {
return "";
}
if (input.length() == 1) {
return input.toLowerCase();//currently set to default; will shtill compile
}
return input.substring(0).toLowerCase();//no locale set
}
//Check if the letter is Z so we can loop back to A
public static boolean checkIfZ(char cInput) {
boolean yesNo = false;
if (cInput++ == 0x7A) {
yesNo = true;
}
return yesNo;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.screen_next, menu);
return true;
}
}
我自然知道 Android 中不存在 JOptionPane,因此我将提示用户在 EditText 字段中输入他们的输入。我有其中三个:用于纯文本输入的 passT,用于密钥输入的 keyT 和用于用户电子邮件的 emailT。
如何将 EditText 字段合并到加密和消息方法中的相应变量?
建议将不胜感激。