Unlock bitlocker drive from java via cmd

-1

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:

    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;
}

My Problem

If I execute this java code I get The handle is invalid with Code 0x80070006.

What I already tried

  1. Different JDK Version 32 and 64 Bit Java 8 and Java 7 (JDK 32 complains somehow that it can't find the command manage-bde)
  2. Different combinations of output streams, with and without newline...
  3. Another script command for the processbuilder like "cmd.exe", "/k", "manage-bde.exe", "-unlock", "D:", "-password", or with /c instead of /k
  4. With and without admin rights
  5. Simple *.bat with the command manage-bde.exe -unlock D: -password (which works perfectly)
  6. Locking the drive through a java command (which works perfectly)
  7. The command without -password (which let's bitlocker claim that I have to define how I want to unlock the drive)

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.

My guess

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.

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 manage-bde -unlock D: -password within the command line and after a few lines of output Bitlocker asks you for the password.

Well I described it as good as I can and hope that someone knows what the problem is.

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!

Thanks in advance!

java
windows
command-line
asked on Stack Overflow Nov 16, 2015 by Jan

1 Answer

1

I encountered same problem recently. I did a lot search. It seems that mange-bde.exe doesn't read user input from stdin. Someone said ssh client and telent clent running on Linux doesn't read password from stdin. Another example Linux command passwd. It has a flag called -stdin which enable the shell to read password from stdin. Therefore, I guessed manage-bde.exe may works in a similar way.

My solution is simulating keyboard input. The awt package can do the job.

answered on Stack Overflow Nov 12, 2016 by lhz

User contributions licensed under CC BY-SA 3.0