-1

嘿,我正在创建有关密码学的新项目,这是我的代码:

private void encCaesar() {

    char[] chars = plaintext.toCharArray();
    for (int z = 0; z < plaintext.length(); z++) {
        char c = chars[z];
        if (c >= 32 && c <= 125) {
            int x = c - 32;
            x = (x + keyCaesar) % 96;
            if (x < 0)
                x += 96;
            chars[z] = (char) (x + 32);
        }
    }
    tempCipher = new String(chars);
    Log.d("Caesar", tempCipher);  

}

private void encRF() {

    int skip;
    int i, d, j;

    for (d = 0; d < keyRF - 1; d++) {
        skip = 2 * (keyRF - d - 1);
        j = 0;
        for (i = d; i < tempCipher.length();) {

            ciphertext += tempCipher.charAt(i);
            if ((d == 0) || (j % 2 == 0))
                i += skip;
            else
                i += 2 * (keyRF - 1) - skip;
            j++;
        }
    }

    for (i = d; i < tempCipher.length(); i += 2 * (keyRF - 1)) {
        ciphertext += tempCipher.charAt(i);
    }
    Log.d("RF", ciphertext);  
    etCipher.setText(ciphertext);
}

btnEnc.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            try {

                keyRF = 2;
                String tempkey = key1.getText().toString();
                plaintext = etPlain.getText().toString();
                if (plaintext.trim().equals("")) {
                    {
                        AlertDialog alertDialog = new AlertDialog.Builder(
                                SentSMSActivity.this).create();
                        alertDialog.setTitle("Alert Dialog");
                        alertDialog
                                .setMessage("Plain text masih kosong . . .");
                        alertDialog.setButton("OK",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(
                                            DialogInterface dialog,
                                            int which) {
                                    }
                                });

                        alertDialog.show();
                    }

                }
                if (tempkey.trim().equals("")) {
                    Toast.makeText(
                            getBaseContext(),
                            "Key 1 masih kosong, maka akan digunakan digunakan key default yaitu 2",
                            Toast.LENGTH_SHORT).show();
                    key1.setText("2");
                } else if (Integer.parseInt(tempkey) > plaintext.trim()
                        .length() - 1) {
                    Toast.makeText(
                            getBaseContext(),
                            "Key 1 nilainya melebihi plaintext, maka akan digunakan digunakan key default yaitu 2",
                            Toast.LENGTH_SHORT).show();
                    key1.setText("2");
                } else {
                    keyRF = Integer.parseInt(key1.getText().toString());
                }

                keyCaesar = 3;
                if (key2.getText().toString().trim().equals("")) {
                    Toast.makeText(
                            getBaseContext(),
                            "Key 2 masih kosong, maka akan digunakan digunakan key default yaitu 3",
                            Toast.LENGTH_SHORT).show();
                    key2.setText("3");
                } else {
                    keyCaesar = Integer.parseInt(key2.getText().toString());
                }

            } finally {

                tempCipher = "";
                encCaesar();
                encRF();

            }
        }
    });

单击加密时,先加密Caesar,然后再加密RF。我做了相反的射频然后凯撒工作正常但是当凯撒然后射频问题出现所以问题是当密文应该是“XYZ”但它显示为“nullXYZ”

4

1 回答 1

4

null字符串在转换为字符串时变为“空”。尝试使用初始化您的 String 变量

String ciphertext = "";
于 2013-01-11T11:24:46.483 回答