Crypto.java 2.02 KB
Newer Older
Melledy's avatar
Melledy committed
1
2
3
4
5
6
7
8
9
package emu.grasscutter.utils;

import java.security.SecureRandom;
import java.util.Base64;

import emu.grasscutter.Grasscutter;
import emu.grasscutter.net.proto.GetPlayerTokenRspOuterClass.GetPlayerTokenRsp;
import emu.grasscutter.net.proto.QueryCurrRegionHttpRspOuterClass.QueryCurrRegionHttpRsp;

10
11
import static emu.grasscutter.Configuration.*;

KingRainbow44's avatar
KingRainbow44 committed
12
13
public final class Crypto {
	private static final SecureRandom secureRandom = new SecureRandom();
Melledy's avatar
Melledy committed
14
15

	public static byte[] DISPATCH_KEY;
16
17
	public static byte[] DISPATCH_SEED;

Melledy's avatar
Melledy committed
18
	public static byte[] ENCRYPT_KEY;
19
20
	public static long ENCRYPT_SEED = Long.parseUnsignedLong("11468049314633205968");
	public static byte[] ENCRYPT_SEED_BUFFER = new byte[0];
Melledy's avatar
Melledy committed
21
22
	
	public static void loadKeys() {
23
24
		DISPATCH_KEY = FileUtils.read(KEY("dispatchKey.bin"));
		DISPATCH_SEED = FileUtils.read(KEY("dispatchSeed.bin"));
25

26
27
		ENCRYPT_KEY = FileUtils.read(KEY("secretKey.bin"));
		ENCRYPT_SEED_BUFFER = FileUtils.read(KEY("secretKeyBuffer.bin"));
Melledy's avatar
Melledy committed
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
	}
	
	public static void xor(byte[] packet, byte[] key) {
		try {
			for (int i = 0; i < packet.length; i++) {
				packet[i] ^= key[i % key.length];
			}
		} catch (Exception e) {
			Grasscutter.getLogger().error("Crypto error.", e);
		}
	}
	
	public static void extractSecretKeyBuffer(byte[] data) {
		try {
			GetPlayerTokenRsp p = GetPlayerTokenRsp.parseFrom(data);
43
			FileUtils.write(KEY("/secretKeyBuffer.bin"), p.getSecretKeyBytes().toByteArray());
Melledy's avatar
Melledy committed
44
45
			Grasscutter.getLogger().info("Secret Key: " + p.getSecretKey());
		} catch (Exception e) {
KingRainbow44's avatar
KingRainbow44 committed
46
			Grasscutter.getLogger().error("Crypto error.", e);
Melledy's avatar
Melledy committed
47
48
49
50
51
52
		}
	}
	
	public static void extractDispatchSeed(String data) {
		try {
			QueryCurrRegionHttpRsp p = QueryCurrRegionHttpRsp.parseFrom(Base64.getDecoder().decode(data));
53
			FileUtils.write(KEY("/dispatchSeed.bin"), p.getRegionInfo().getSecretKey().toByteArray());
Melledy's avatar
Melledy committed
54
		} catch (Exception e) {
KingRainbow44's avatar
KingRainbow44 committed
55
			Grasscutter.getLogger().error("Crypto error.", e);
Melledy's avatar
Melledy committed
56
57
58
59
60
61
		}
	}
	
	public static byte[] createSessionKey(int length) {
		byte[] bytes = new byte[length];
		secureRandom.nextBytes(bytes);
62
		return bytes;
Melledy's avatar
Melledy committed
63
64
	}
}