传递字节数组越界出错,但它通过了junit测试
问题描述:
使用des进行一些加密和解密以及Avalanche效果。但在测试时,我遇到了一些问题,我的阵列超出了界限。这里是具有麻烦的相关代码IM:传递字节数组越界出错,但它通过了junit测试
public static void main(String[] args){
//take original plain text and get the ct to compare all other ct's to
String plaintxt = "Coolbro!";
byte [] ptAr = getBytes(asciiToHex(plaintxt));
byte [] ctFinal = encrypt(ptAr);
byte [] ptCopy;
byte [] newCt;
int differences =0;
for (int j = 63; j >= 0; j--){
ptCopy = flip(ptAr, 2);
newCt = encrypt(ptCopy);
differences = diff(ctFinal,newCt);
System.out.println(differences);
}
public static byte [] flip(byte [] a, int position){
byte[] copy = a;
String temp = "";
String tempf = "";
for(int i = 0; i <= a.length; i++){
temp = temp + String.format("%8s", Integer.toBinaryString(a[i])).replace(' ', '0');
}
if(temp.charAt(position) == '1'){
for(int i = 0; i < temp.length(); i++){
if (i == position){
tempf += "0";
}
else{
tempf += temp.charAt(i);
}
}
}
else{
for(int i = 0; i < temp.length(); i++){
if (i == position){
tempf += "1";
}
else{
tempf += temp.charAt(i);
}
}
}
temp = Integer.toHexString(Integer.parseInt(tempf, 2));
byte [] fin = temp.getBytes();
return fin;
}
倒装只是假定在给定的位置翻转给定的字节阵列的单个位(和它的工作,经过的单元测试)。
所有差异是否表示两个密文阵列中有多少位置不同。那也行得通。
但由于某些原因IM就吃麻烦这里
ptCopy = flip(ptAr, 2);
我知道PTAR是一个可以接受的字节数组,所以我不看处理它扔errors.These是错误的即时得到:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 8
at AvalancheUtilities.flip(AvalancheUtilities.java:47)
at AvalancheUtilities.main(AvalancheUtilities.java:17)
任何想法如何解决? IDK其中其从
哪一行是'AvalancheUtilities.java:47'? – 2013-03-01 00:14:13
再次运行它,它应该是行44而不是47,但这里是44:'temp = temp + String.format(“%8s”,Integer.toBinaryString(a [i]))。replace('','0 ');' – erp 2013-03-01 00:19:23