我得到一個錯誤,每次我運行此錯誤:由於最後一個塊未正確填充
「錯誤:給定最後一個塊未正確填充」
基本上我試圖蠻力最後3密鑰的字節,前13個字節是正確的。 任何想法我做錯了什麼?我嘗試刪除填充,它的工作原理,但它無法找到我確信它存在的明文,幷包含單詞「瑪麗」。 注意:我使用sun.misc.BASE64Decoder
這裏是我的代碼的一部分。
String myiv = new String(new byte[] {
0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x31,0x30,0x31,0x31,0x31,0x32,0x33
});
char [] mykeyarray = new char[] {0x86,0xe5,0x30,0x90,0xff,0x62,0xa0,0x9a,0x81,0x00,0xad,0x9e,0x8f,0x00,0x00,0x00};
String encoded = "dm8cfvs+c7pKM+WR+fde8b06SB+lqWLS4sZW+PfQSKtTfgPknzYzpTVOtJP3JBoU2Uo/7XWopjoPDOlPr24duuck0z+vAx91bYTwQo4INnIIBkj/lhJMWmvAKaUIO3qzBoGg8ynQOhuG6LY7Wo0uww==";
IvParameterSpec ivspec = new IvParameterSpec(myiv.getBytes());
byte [] decoded;
FileWriter fstream = new FileWriter("out.txt");
BufferedWriter out = new BufferedWriter(fstream);
String mykey;
int repeat = 256;
outerloop:
for(int i=0;i<repeat;i++){
for(int j=0;j<repeat;j++){
for(int k=0;k<repeat;k++){
mykey = new String(mykeyarray);
SecretKeySpec keyspec = new SecretKeySpec(mykey.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
System.out.println("I: "+i+" J: "+j+" K: "+k);
decoded = new BASE64Decoder().decodeBuffer(encoded);
cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
byte [] decrypted = cipher.doFinal(decoded);
String dec = new String(decrypted);
if(dec.contains("Mary")){
out.write(dec);
out.write("\n");
System.out.println(dec);
break outerloop;
}
mykeyarray[15]++;
}
mykeyarray[14]++;
mykeyarray[15]=0x00;
}
mykeyarray[13]++;
mykeyarray[14]=0x00;
mykeyarray[15]=0x00;
}
out.close();
}
catch(Exception e){
System.out.println("Error: " + e.getMessage());
}
}
}
爲了避免這個錯誤,只需要設置沒有填充的解密:'AES/CBC/NoPadding',所以它不會失敗。您仍然會使用錯誤的鑰匙進行垃圾回收,但您將可以看到垃圾。 – rossum
你的回答不正確Pavel,'BadPaddingException'會比256次中的次數稍多一次拋出。每次解密在十六進制結束時都是'01',這將是一個正確的PKCS5Padding('0202'等也是一個,但是這當然不太可能發生)。這並不意味着密鑰是正確的,或者說得到的純文本是正確的。 –
好的,我沒有計算出隨機塊中正確填充的概率。我只是解釋了例外的原因。 –