我想为android做一个Caesar密码应用程序

时间:2015-04-02 14:54:43

标签: java android

对于我在edit_text中插入后的正常文本:当我输入大写XYZ时,为什么我不能得到文本编码,我得到这些[\]符号,通常我应该得到ABC,因为非大写字母似乎工作时我按xyzi get abc ...当我按空格时我得到这个#符号......

和解码..反向过程:当我按下b c我得到这些  符号^ _`normmaly我应该得到x y z和大写字母A B C我得到这些> ? @通常我应该得到资本X Y Z ...但是当我按空格时,这里似乎工作并且不显示#

public class Main extends Activity {
    TextView myText, myText2;
    Button myCodeButton, myDecodeButton, deleteButton;
    public static EditText enteredEditText;
    public String getText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // text for showing coded and decoded words
        myText = (TextView) findViewById(R.id.textView1);

        myText2 = (TextView) findViewById(R.id.textView2);

        //edittext
        enteredEditText = (EditText) findViewById(R.id.editText1);
        //buttons
        myCodeButton = (Button) findViewById(R.id.button1);
        myDecodeButton = (Button) findViewById(R.id.button2);
        deleteButton = (Button) findViewById(R.id.button3);


        Code_My_TextButton();  
        Decode_my_textButton();

        // this is just for clearing edit_texts and text_views
        deleteClick();
    }


// i entered a text and this method should code the text and display it
    public void Code_My_TextButton()
    {
        myCodeButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View view) {

                Caesar_cipher_coding_method();
                myText2.setText("");
            }
        });
    }

    private void Caesar_cipher_coding_method() {
        int shift = 3; // the shift is for A is going to be A + 3 letters
       // and would be D , for B will be E , for C will be F and so on..
        Editable msg = enteredEditText.getText();
        String s = "";
        int len = msg.length();
        for (int x = 0; x < len; x++) {
            char c = (char) (msg.charAt(x) + shift);
            if (c > 'z')
                s += (char) (msg.charAt(x) - (26 - shift));
            else
                s += (char) (msg.charAt(x) + shift);


        }
        myText.setText(s);
    }


    public void Decode_my_textButton()
    {
        myDecodeButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View view) {
                Reverse_Caesar_cipher_coding_method();

            }
        });
    }
// here is the reverse process ...i enter the coded word and should show me 
//the normal word
    private void Reverse_Caesar_cipher_coding_method() {
        int shift = -3;
        Editable msg = enteredEditText.getText();
        String s = "";
        int len = msg.length();
        for (int x = 0; x < len; x++) {
            char c = (char) (msg.charAt(x) + shift);
            if (c > 'z')
                s += (char) (msg.charAt(x) - (26 - shift));
            else
                s += (char) (msg.charAt(x) + shift);


        }
        myText2.setText(s);
        myText.setText("");
    }

    // this is just for clearing edit_texts and text_views
    public void deleteClick()
    {
        deleteButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View view) {
           //clearing text
                enteredEditText.setText("");
                myText.setText("");
                myText2.setText("");
            }
        });
    }

}

1 个答案:

答案 0 :(得分:1)

在加密期间,还有另一个条件。 例如:

//in the Caesar cipher
for (int x = 0; x < len; x++) {
    char c = (char) (msg.charAt(x) + shift);
    if (c > 'z' || ( c > 'Z' && c < 'd')) { // if the shifted went past z or Z:
        c-=26;
    }
    s+=c;
}

//in the reverse
for (int x = 0; x < len; x++) {
    char c = (char) (msg.charAt(x) + shift);
    if (c < 'A' || (c < 'a' && c > 'W'))
        c += 26;
    s += c;
}
myText2.setText(s);
myText.setText("");