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

Tags: , ,

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. 🙂

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));
            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);
            masterKey = new String(Crypt32Util.cryptUnprotectData(encryptedMasterKey.getBytes()));
        } catch (Exception e) {


[value of **encryptedMasterKey**]
com.sun.jna.platform.win32.Win32Exception: The data is invalid.
    at com.sun.jna.platform.win32.Crypt32Util.cryptUnprotectData(
    at com.sun.jna.platform.win32.Crypt32Util.cryptUnprotectData(
    at com.sun.jna.platform.win32.Crypt32Util.cryptUnprotectData(
    at com.main.Main.main(


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

Source: stackoverflow