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();
}
}
});
点击加密后,首先加密凯撒然后加密。 我做了相反的RF然后Caesar工作正常 但是当凯撒出现射频问题时 所以问题是密文应该是“XYZ”而是出现“nullXYZ”
答案 0 :(得分:4)
null
当转换为String时,字符串变为'null'。尝试使用
String ciphertext = "";