hey i’m creating new project about cryptography
here’s my code:
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();
}
}
});
when click encrypt, encrypt Caesar first then RF.
i have done opposite RF then Caesar just work fine
but when caesar then RF problem comes up
so the problem is when the ciphertext supposed to be “XYZ” but instead it appear “nullXYZ”
nullString gets ‘null’ when converted toString. Try initialize your String variable with