如果我输入
kunci = a and pesan = b my output is 8
但如果我输入
kunci = asd and pesan = d or kunci = a and pesan = qw
我的应用程序强制关闭
my error i got error "String index out of bounds" at line 77.
拜托,有人可以帮我吗?我试图修复它,但我没有得到任何解决方案。请帮助我..谢谢..问候。
这是我的 XOREnkripsi.class
package com.enkripsisms;
public class XOREnkripsi {
public String charToBinary( char c ) {
int ascii = (int)c;
String binaryChar = Integer.toBinaryString(ascii);
String addBit = "";
if(binaryChar.length() < 8) {
for( int index = 0; index < 8 - binaryChar.length(); index++ ) {
addBit = "0" + addBit;
}
}
return addBit + binaryChar;
}
public String XORKriptografi( String plain, String key ) {
StringBuffer enkripsi = new StringBuffer();
for( int index = 0; index < plain.length(); index++ ) {
int panjangk = key.length();
Character teksX = new Character(plain.charAt(index));
Character keyX = new Character(key.charAt(index%panjangk));
int xor = Integer.parseInt(teksX.toString()) ^ Integer.parseInt(keyX.toString());
enkripsi.append(xor);
}
return enkripsi.toString();
}
public String binaryToAsciiToString( String onebit ) {
int ascii;
ascii = Integer.parseInt(onebit, 2);
return Character.toString((char) ascii);
}
}
tulisPesan.class
public void sandikan ( View v ) {
kunci = (EditText)findViewById(R.id.kunci);
pesan = (EditText)findViewById(R.id.pesan);
String k = kunci.getText().toString();
String p = pesan.getText().toString();
int pk = k.length();
int pp = p.length();
binaryText = "";
plainChiper = "";
String ciper;
if( k.isEmpty()) {
Toast.makeText(this, "Maaf, kunci harus diisi", Toast.LENGTH_LONG).show();
Intent tulisPesan = new Intent();
tulisPesan.setClass(this, tulisPesan.class);
startActivity(tulisPesan);
}else{
String acak = "";
double acakN;
for (int e = 0; e<pk; e++) {
acakN = Math.random() * 25 + 65;
acak = acak + (char) acakN;
k = acak;
}
}
if (k.length() != p.length()) {
for (pk = 0; pk < pp; pk++) {
String tambahk = "";
tambahk = tambahk + k.substring(0, 1);
k = tambahk;
}
}
XOREnkripsi xor = new XOREnkripsi();
for(int a = 0; a < pk; a++) {
binaryKey = binaryKey + xor.charToBinary(k.charAt(a)); //my line error
}
for(int b = 0; b < pp; b++) {
binaryText = binaryText + xor.charToBinary(p.charAt(b));
}
binaryChiper = xor.XORKriptografi(binaryText, binaryKey);
for(int d = 0; d < binaryChiper.length(); d=d+8) {
plainChiper = plainChiper + xor.binaryToAsciiToString(binaryChiper.substring(d, d+8));
}
chiperAllText = chiperAllText + plainChiper;
ciper = chiperAllText;
Intent hasilPesan = new Intent();
hasilPesan.setClass(this, hasilPesan.class);
hasilPesan.putExtra("ciper", ciper);
startActivity(hasilPesan);
}