Aplikacja wysyła SMSa zakodowanego z powiedzmy rotacją 10, zaszyfrowanie chodzi, ale jeżeli chce to odszyfrować to moja funkcja deszyfrująca odszyfrowuje do pewnego momentu.
np po wpisaniu całego alfabetu i daniu rotacji 10
deszyfracja wygląda tak:
abcdefghijklmnoprstqabcdef
czyli działa tylko do q
11 uciana po "u"
12 uciana po "x"
Ale jeżeli zakoduje z rotacją 13 to deszyfracja działa na wszystkim
Możecie spojrzeć na kod ? bo ja nie mogę doszukać błędu.
Klasa Cipher
public class Cipher {
private int _rotationAmount = 13;
private char[] _lowercase = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
private char[] _uppercase = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
public Cipher(int rotationAmount) {
_rotationAmount = rotationAmount;
}
public String Encrypt(String text) {
StringBuilder sb = new StringBuilder();
for(int c = 0; c < text.length(); c++) {
char letter = text.charAt(c);
if( letter >= 'a' && letter <= _lowercase[_rotationAmount - 1] ) {
letter += _rotationAmount;
}
else if( letter >= _lowercase[_rotationAmount] && letter <= 'z' ) {
letter -= _rotationAmount;
}
else if( letter >= 'A' && letter <= _uppercase[_rotationAmount - 1] ) {
letter += _rotationAmount;
}
else if( letter >= _uppercase[_rotationAmount] && letter <= 'Z' ) {
letter -= _rotationAmount;
}
sb.append(letter);
}
return sb.toString();
}
public String Decrypt(String text) {
StringBuilder sb = new StringBuilder();
for(int c = 0; c < text.length(); c++) {
char letter = text.charAt(c);
if( letter >= 'a' && letter <= _lowercase[_rotationAmount - 1] ) {
letter += _rotationAmount;
}
else if( letter >= _lowercase[_rotationAmount] && letter <= 'z' ) {
letter -= _rotationAmount;
}
else if( letter >= 'A' && letter <= _uppercase[_rotationAmount - 1] ) {
letter += _rotationAmount;
}
else if( letter >= _uppercase[_rotationAmount] && letter <= 'Z' ) {
letter -= _rotationAmount;
}
sb.append(letter);
}
return sb.toString();
}
}
w następnej klasie pobieram sobie rotacje, która chce ze spinnera
private Cipher _cipher = null;
private void SetRotationAmount(String selectedText) {
if (selectedText == null || selectedText == "")
return;
Log.i("print", selectedText);
if( selectedText.equalsIgnoreCase("Rotation 10")) {
_cipher = new Cipher(10);
}
else if(selectedText.equalsIgnoreCase("Rotation 11")) {
_cipher = new Cipher(11);
}
else if( selectedText.equalsIgnoreCase("Rotation 12")) {
_cipher = new Cipher(12);
}
else if( selectedText.equalsIgnoreCase("Rotation 13")) {
_cipher = new Cipher(13);
}
}
Spinner rotationSpinner = (Spinner)findViewById(R.id.spinnerRotationAmount);
ArrayAdapter adapter = ArrayAdapter.createFromResource(
this, R.array.rotation_list, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
rotationSpinner.setAdapter(adapter);
rotationSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
SetRotationAmount(arg0.getSelectedItem().toString());
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
// moje dwa buttony do encode i decode
Button encryptButton = (Button)findViewById(R.id.koduj);
encryptButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText textBox = getEditTextObject();
textBox.setText(_cipher.Encrypt(textBox.getText().toString()));
}
});
Button decryptButton = (Button)findViewById(R.id.odkoduj);
decryptButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText textBox = getEditTextObject();
textBox.setText(_cipher.Decrypt(textBox.getText().toString()));
}
});
Dlaczego tak się dzieje ze rotacja 13 chodzi a gdy wybiorę rotacje 10 11 lub 12 to ucina mi dekodowanie po jakiejś literze?