|
60精币
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.security.Security;
import java.util.Arrays;
import javax.crypto.Cipher;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.junit.Test;
import com.github.windpapi4j.WinDPAPI;
import com.github.windpapi4j.WinDPAPI.CryptProtectFlag;
import com.mlkui.common.helper.HexDumpHelper;
public class ChromeCookieTest
{
@Test
public void test()
{
Security.addProvider(new BouncyCastleProvider());
int keyLength = 256 / 8;
int nonceLength = 96 / 8;
String kEncryptionVersionPrefix = "v10";
int GCM_TAG_LENGTH = 16;
try
{
byte[] encryptedKeyBytes = Base64.decodeBase64(
"RFBBUEkBAAAA0Iyd3wEV0RGMegDAT8KX6wEAAACtP/XjPc54RaDgxj8Eef6QAAAAAAIAAAAAABBmAAAAAQAAIAAAAHICJrBWxxkRwEH6iO4zyo4cupd1kX23HTvlGjbf3rAAAAAA6AAAAAAgAAIAAAANV1OHmguiAyFxk6vAFOb+K1bwNCjUshXByRmwbXYd8mMAAAAKzGG5QNq4NliAapY5N3rKaS+kqJNmYJJrla5tZ7LS/9Z39jogumIA0zjkypIiG7EkAAAACrrL+zxA4OSuc8onLmqfimVj/lhh21n8ERnTNMk+67dFnoy2KzcUgk8mJfKforKbgNRH5RcVcNOh4Lz/LcUgMu");
System.out.println(new String(encryptedKeyBytes));
assertTrue(new String(encryptedKeyBytes).startsWith("DPAPI"));
encryptedKeyBytes = Arrays.copyOfRange(encryptedKeyBytes, "DPAPI".length(), encryptedKeyBytes.length);
WinDPAPI winDPAPI = WinDPAPI.newInstance(CryptProtectFlag.CRYPTPROTECT_UI_FORBIDDEN);
byte[] keyBytes = winDPAPI.unprotectData(encryptedKeyBytes);
System.out.println(Base64.encodeBase64String(keyBytes));
System.out.println(HexDumpHelper.dumpHexString(keyBytes));
assertEquals(keyLength, keyBytes.length);
byte[] encryptedValue = new byte[] { (byte) 0x76, (byte) 0x31, (byte) 0x30, (byte) 0x74, (byte) 0x90, (byte) 0x31, (byte) 0x14, (byte) 0xed, (byte) 0x50, (byte) 0xac, (byte) 0x4e, (byte) 0xf9, (byte) 0x7e, (byte) 0x6b, (byte) 0x62, (byte) 0x17, (byte) 0x01, (byte) 0x52, (byte) 0x5f,
(byte) 0x52, (byte) 0x78, (byte) 0xcd, (byte) 0x24, (byte) 0x1a, (byte) 0x34, (byte) 0x0f, (byte) 0xc2, (byte) 0xbd, (byte) 0x2a, (byte) 0xc3, (byte) 0x2f, (byte) 0x00, (byte) 0x5d, (byte) 0x0d, (byte) 0x56, (byte) 0xf5, (byte) 0xa6, (byte) 0x84, (byte) 0x59 };
System.out.println(HexDumpHelper.dumpHexString(encryptedValue));
// Obtain the nonce.
byte[] nonce = Arrays.copyOfRange(encryptedValue, kEncryptionVersionPrefix.length(), kEncryptionVersionPrefix.length() + nonceLength);
System.out.println(HexDumpHelper.dumpHexString(nonce));
// Strip off the versioning prefix before decrypting.
encryptedValue = Arrays.copyOfRange(encryptedValue, kEncryptionVersionPrefix.length() + nonceLength, encryptedValue.length);
System.out.println(HexDumpHelper.dumpHexString(encryptedValue));
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");
GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(GCM_TAG_LENGTH * 8, nonce);
cipher.init(Cipher.DECRYPT_MODE, keySpec, gcmParameterSpec);
String encryptedValueString = new String(cipher.doFinal(encryptedValue));
System.err.println(encryptedValueString);
}
catch (Exception ex)
{
System.out.println(ex.toString());
}
}
}
补充内容 (2020-7-1 22:09):
懂的大神 帮个忙 谢谢! |
|