我正在使用AES
使用BouncyCastle加密/解密GCM
模式中的某些文件。
雖然我證明錯誤的解密密鑰,但沒有例外。
我應該如何檢查鑰匙是否有誤?
我的代碼是這樣的:在JAVA中使用AES/GCM檢測不正確的密鑰
SecretKeySpec incorrectKey = new SecretKeySpec(keyBytes, "AES");
IvParameterSpec ivSpec = new IvParameterSpec(ivBytes);
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding", "BC");
byte[] block = new byte[1048576];
int i;
cipher.init(Cipher.DECRYPT_MODE, incorrectKey, ivSpec);
BufferedInputStream fis=new BufferedInputStream(new ProgressMonitorInputStream(null,"Decrypting ...",new FileInputStream("file.enc")));
BufferedOutputStream ro=new BufferedOutputStream(new FileOutputStream("file_org"));
CipherOutputStream dcOut = new CipherOutputStream(ro, cipher);
while ((i = fis.read(block)) != -1) {
dcOut.write(block, 0, i);
}
dcOut.close();
fis.close();
感謝
這將是非常好的,如果你會接受一些更多的答案和跟進你的問題4r1y4n(這將是Ariyan我想) –
是否有一個特定的原因,你爲什麼不接受加密問題的答案,4r1y4n,或他們是否逃避了你的注意?你沒有接受任何4連續。 –
請注意:Java 7中的AEAD模式與CipherInputStream結合不受完整性保護:https://github.com/binwiederhier/syncany/issues/28#issuecomment-36337529 – binwiederhier