Java CryptUnprotectData Windows WiFi Passwords

北城余情 提交于 2021-02-05 11:23:06

问题


I'm trying to decrypt a Windows WiFi password on the same machine using Java which is supposed to work with cryptUnprotectData() but I'm getting the following error:

Exception in thread "main" com.sun.jna.platform.win32.Win32Exception: The data is invalid.
    at com.sun.jna.platform.win32.Crypt32Util.cryptUnprotectData(Crypt32Util.java:128)
    at com.sun.jna.platform.win32.Crypt32Util.cryptUnprotectData(Crypt32Util.java:103)
    at com.sun.jna.platform.win32.Crypt32Util.cryptUnprotectData(Crypt32Util.java:90)

I'm using this Java code:

String encryptedWirelessKey = "01000000D08C9DDF0115D1118C7A00C0***TRUNCATED***";
byte[] bytes = Crypt32Util.cryptUnprotectData(encryptedWirelessKey.getBytes(Charset.defaultCharset()));
System.out.println(new String(bytes));

Here you can read more about where Windows stores WiFi passwords. Why is the data invalid when I copied it straight from the XML keyMaterial tags? I'm administrator on the machine and the passwords are by my user account.

Update:

import com.sun.jna.platform.win32.Crypt32Util;

public class Testing
{
    public static void main(String[] arguments) throws Exception
    {
        String encryptedWirelessKey = "01000000D08C9DDF0115D1118C7A00C0***TRUNCATED***";
        byte[] bytes = Crypt32Util.cryptUnprotectData(hexStringToByteArray(encryptedWirelessKey));
        System.out.println(new String(bytes));
    }

    private static byte[] hexStringToByteArray(String s) {
        int len = s.length();
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
                    + Character.digit(s.charAt(i+1), 16));
        }
        return data;
    }
}

This throws the following exception:

Exception in thread "main" com.sun.jna.platform.win32.Win32Exception: Key not valid for use in specified state.
    at com.sun.jna.platform.win32.Crypt32Util.cryptUnprotectData(Crypt32Util.java:128)
    at com.sun.jna.platform.win32.Crypt32Util.cryptUnprotectData(Crypt32Util.java:103)
    at com.sun.jna.platform.win32.Crypt32Util.cryptUnprotectData(Crypt32Util.java:90)

What exactly does it mean? Lacking permissions?


回答1:


You're using getBytes() on a hex string, when you should be parsing the hex string into bytes.

Choose your preferred way from the following links.

In Java, how do I convert a hex string to a byte[]?

Convert a string representation of a hex dump to a byte array using Java?



来源:https://stackoverflow.com/questions/43008556/java-cryptunprotectdata-windows-wifi-passwords

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!