0

我正在制作一个加密纯文本然后通过电子邮件发送的应用程序。我cText在“加密”方法中创建的变量(从用户输入的 passT 和 keyT 创建)在方法结束时返回。然而,我很好奇我将如何将它合并到我的 onCreate 方法中,以便将它的新加密内容包含到电子邮件中?以下是我所拥有的,但我只收到错误:

public class ScreenNext extends Activity {

int key = 0;
static char ch;

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

    send.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            String keyText = keyT.getText().toString();
            String passText = passT.getText().toString();
            String EmailAdd = emailT.getText().toString();

            //This must be fixed
            //String cipherText = cText.getText().toString();


            Intent email = new Intent(Intent.ACTION_SEND);//The intent
            email.setType("plain/text");
            email.putExtra(Intent.EXTRA_EMAIL, new String[]{EmailAdd.toString()});
            email.putExtra(Intent.EXTRA_SUBJECT, "Your encrypted Password");//Subject is hard coded for convenience sake
            email.putExtra(Intent.EXTRA_TEXT, cText.toString());//Here we add encrypted Password that has just been generated
            startActivity(email);//Start the activity
        }

    });

}//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();


ScreenNext subcipher_1 = null;
String CipherTxt = subcipher_1.encrypt(message, key);
return CipherTxt;
}


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;
}

很感谢任何形式的帮助。

4

1 回答 1

0

如果您想在您cText的多个部分中使用您的变量,Activity那么您需要通过将其放在方法之外使其成为成员变量,最好像这样

public class ScreenNext extends Activity {

int key = 0;
static char ch;
String cText;

但是您可能希望null在使用它或对其应用任何方法之前对其进行初始化或检查。将变量放在这里为其提供了类范围,因此可以在 this 中的任何位置访问它Activity。您不能真正再次“使用”它,onCreate()因为一旦onCreate()方法完成,它就不会再次被调用。我假设您正在谈论您的onClick()which 设置onCreate()但将在Button单击时被调用。我对你的问题的措辞有点困惑,所以如果这不是你想要的,那就更清楚地解释一下

于 2013-04-10T23:04:31.550 回答