title: Detecting incorrect key using AES/GCM in JAVA
body: <p>I'm using <code>AES</code> to encrypt/decrypt some files in <code>GCM</code> mode using BouncyCastle.<br>
  While I'm proving wrong key for decryption there is no exception.<br>
  How should I check that the key is incorrect?<br>
  my code is this:  </p>
  
  <pre><code>    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();
  </code></pre>
  
  <p>thanks</p>