-6

如果我输入

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);
}
4

1 回答 1

0

您正在更改pk循环中的值for (pk = 0; pk < pp; pk++)。这意味着它将在pp之后。事实上,这个循环在当前状态下是毫无用处的,因为它破坏了旧的k,并给它一个substring(0, 1) pp时间值。k尺寸 1也是如此。

因为pp大于 1,所以你得到一个无效的索引。

于 2013-04-28T07:58:44.890 回答