platform.win32.Win32Exception:数据无效 | Java

huangapple go评论74阅读模式
英文:

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

问题

我正在编写这段代码,以恢复用户保存的 Chrome 密码并在控制台上显示它们。
我已经能够解码 Base64 编码的内容。但是在使用 Crypt32Util.cryptUnprotectData 对这段内容进行解密时遇到了问题... 我是一个初学者。 platform.win32.Win32Exception:数据无效 | Java

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
[**encryptedMasterKey 的值**]
com.sun.jna.platform.win32.Win32Exception: 数据无效。
	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)
英文:

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. platform.win32.Win32Exception:数据无效 | Java
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)

答案1

得分: 1

decoder.decode() 返回二进制数据。您不能从二进制数据创建String

如果您想要一个包含从decoder.decode()返回的byte[]中前5个字节的byte[],请使用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);

然而,我怀疑这是正确的。您为什么认为主密码可以加密为仅有5个字节,其余的是什么?

更有可能的是所有的字节都是主密钥的加密版本:

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

byte[] masterKey = Crypt32Util.cryptUnprotectData(Base64.getDecoder().decode(encryptedKey));
英文:

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

答案2

得分: 0

@Andreas的回答几乎正确
我使用了这段代码,但出现了异常

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

所以它只是在以下部分更改了一些值

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

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

它起作用了,但现在我需要理解它的密钥格式 platform.win32.Win32Exception:数据无效 | Java
谢谢,Andreas

英文:

The answer of @Andreas is almost correct
I used this code which gave me an exception

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

So it just changed some values in

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

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

It worked But now I have to understand its key format platform.win32.Win32Exception:数据无效 | Java
Thanks, Andreas

huangapple
  • 本文由 发表于 2020年10月6日 02:51:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/64214514.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定