title: Unlock bitlocker drive from java via cmd
body: <p>I am trying to unlock a drive secured by bitlocker from Java. As far as I know there are no libs which can help me to handle that, so I was trying it through cmd. Here's the code:</p>
  
  <pre><code>    public static boolean unlockDisk(String pwd) throws IOException
  {
      String[] script =
      {
              "manage-bde.exe", "-unlock", "D:", "-password",
      };
  
      Process process = new ProcessBuilder(script).start();
      InputStream inputStream = process.getInputStream();
      InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
      BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
      final OutputStream outputStream = process.getOutputStream();
      BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream));
      writer.write(pwd);
      writer.newLine();
      writer.close();
      System.out.println("--------------------------------------");
      System.out.println("Bitlocker log:");
      String line;
      while ((line = bufferedReader.readLine()) != null)
      {
          System.out.println(line);
      }
      bufferedReader.close();
  
      BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));
      System.out.println("Here is the standard error of the command (if any):\n");
      String tmp;
      while ((tmp = stdError.readLine()) != null)
      {
          System.out.println(tmp);
      }
      System.out.println("--------------------------------------");
  
      return true;
  }
  </code></pre>
  
  <p><strong>My Problem</strong></p>
  
  <p>If I execute this java code I get <code>The handle is invalid</code> with <code>Code 0x80070006</code>.</p>
  
  <p><strong>What I already tried</strong></p>
  
  <ol>
  <li>Different JDK Version 32 and 64 Bit Java 8 and Java 7 (JDK 32 complains somehow that it can't find the command <code>manage-bde</code>)</li>
  <li>Different combinations of output streams, with and without newline...</li>
  <li>Another script command for the processbuilder like <code>"cmd.exe", "/k", "manage-bde.exe", "-unlock", "D:", "-password",</code> or with <code>/c</code> instead of <code>/k</code></li>
  <li>With and without admin rights</li>
  <li>Simple *.bat with the command <code>manage-bde.exe -unlock D: -password</code> (which works perfectly)</li>
  <li>Locking the drive through a java command (which works perfectly)</li>
  <li>The command without <code>-password</code> (which let's bitlocker claim that I have to define how I want to unlock the drive)</li>
  </ol>
  
  <p>I googled around for some time and found others having this problems but in a different way with other applications. So it seems like a very common error message.</p>
  
  <p><strong>My guess</strong></p>
  
  <p>I think it has something to do with how I handle my Java output as Bitlocker input. Maybe I am using the wrong streams to write to.</p>
  
  <p>I can't provide the value of the password within the script variable, because Bitlocker want doesn't accept that way of entering the password. Usually you enter <code>manage-bde -unlock D: -password</code> within the command line and after a few lines of output Bitlocker asks you for the password.</p>
  
  <p>Well I described it as good as I can and hope that someone knows what the problem is.</p>
  
  <p>Any suggestion, even if it just leads to a more precise error message, would be appreciated. If you have any questions, just let me know!</p>
  
  <p>Thanks in advance!</p>