Skip to content
Advertisement

platform.win32.Win32Exception: The data is invalid | Java

I was writing this code to restore the user’s saved chrome passwords and display them on the console. I was able to decode Base64 encoded. But I am failing in decrying from this Crypt32Util.cryptUnprotectData any help … I am a beginner. 🙂 Main.java

import java.io.FileReader;
import java.util.Base64;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import com.sun.jna.platform.win32.Crypt32Util;

public class Main {

    public static void main(String[] args) {
        String name = System.getProperty("user.home");
        name += "\AppData\Local\Google\Chrome\User Data\";
        String masterKey = "";
        String localState = name + "Local State";
        try {
            Object object = new JSONParser().parse(new FileReader(localState));
            System.out.println("Success");
            JSONObject jsonObject = (JSONObject) object;
            JSONObject tempJsonObject = (JSONObject) jsonObject.get("os_crypt");
            Base64.Decoder decoder = Base64.getDecoder();
            String encryptedKey = (String) tempJsonObject.get("encrypted_key");
            String decryptedKey = new String(decoder.decode(encryptedKey));
            String encryptedMasterKey = decryptedKey.substring(5);
            System.out.println(encryptedMasterKey);
            masterKey = new String(Crypt32Util.cryptUnprotectData(encryptedMasterKey.getBytes()));
            System.out.println(masterKey);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Output

Success
[value of **encryptedMasterKey**]
com.sun.jna.platform.win32.Win32Exception: The data is invalid.
    at com.sun.jna.platform.win32.Crypt32Util.cryptUnprotectData(Crypt32Util.java:144)
    at com.sun.jna.platform.win32.Crypt32Util.cryptUnprotectData(Crypt32Util.java:117)
    at com.sun.jna.platform.win32.Crypt32Util.cryptUnprotectData(Crypt32Util.java:104)
    at com.main.Main.main(Main.java:26)

Advertisement

Answer

decoder.decode() returns binary data. You cannot create a String from binary data.

If you want a byte[] with the first 5 bytes from the byte[] returned by decoder.decode(), use Arrays.copyOfRange():

String encryptedKey = (String) tempJsonObject.get("encrypted_key");

Base64.Decoder decoder = Base64.getDecoder();
byte[] decodedKey = decoder.decode(encryptedKey);
byte[] encryptedMasterKey = Arrays.copyOfRange(decodedKey, 0, 5);
byte[] masterKey = Crypt32Util.cryptUnprotectData(encryptedMasterKey);

However, I doubt that is correct. Why do you believe the master password could be encrypted to only 5 bytes, and what is all the rest then?

It’s far more likely that all the bytes are encrypted version of the master key:

String encryptedKey = (String) tempJsonObject.get("encrypted_key");

byte[] masterKey = Crypt32Util.cryptUnprotectData(Base64.getDecoder().decode(encryptedKey));
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement