112 lines
4.8 KiB
Java
112 lines
4.8 KiB
Java
package com.io.yutian.verify;
|
|
|
|
import com.io.yutian.mclive.Main;
|
|
import org.bukkit.Bukkit;
|
|
import org.bukkit.entity.Player;
|
|
|
|
import javax.crypto.Cipher;
|
|
import javax.crypto.KeyGenerator;
|
|
import javax.crypto.SecretKey;
|
|
import javax.crypto.spec.SecretKeySpec;
|
|
import java.security.NoSuchAlgorithmException;
|
|
import java.security.SecureRandom;
|
|
import java.util.Base64;
|
|
|
|
public class AESUtil {
|
|
|
|
private static final String KEY_ALGORITHM = "AES";
|
|
// AES/ECB/PKCS5Padding
|
|
public static final String DEFAULT_CIPHER_ALGORITHM = "101101000011010100110011010101010000011000010110010001100100011010010110111001100111";
|
|
|
|
// 将二进制字符串转换为普通字符串
|
|
public static String convertString(String binaryString) {
|
|
StringBuilder result = new StringBuilder();
|
|
// 每8位二进制表示一个字符
|
|
for (int i = 0; i < binaryString.length(); i += 8) {
|
|
// 获取8位二进制子串
|
|
String byteString = binaryString.substring(i, i + 8);
|
|
// 将二进制字符串转换为十进制整数
|
|
int charCode = Integer.parseInt(byteString, 2);
|
|
// 将十进制整数转换为字符
|
|
result.append((char) charCode);
|
|
}
|
|
|
|
return result.toString();
|
|
}
|
|
|
|
public static boolean isVerifyCheck(Player p, String pluginName, String roomId){
|
|
if(!Main.check_plugin){
|
|
return false;
|
|
}
|
|
PluginVerifyResult verifyResult = VerifyHandler.verify("127.0.0.1",pluginName,roomId);
|
|
if (!verifyResult.equals(PluginVerifyResult.VERIFY_SUCCESS)) {
|
|
if(verifyResult == PluginVerifyResult.USER_STATE_DISABLE){
|
|
Bukkit.getConsoleSender().sendMessage("[验证日志 - 拦截] "+p.getName()+" 的直播间授权已到期.");
|
|
p.sendMessage("§c[系统]§a验证尚未通过,您的直播间授权已到期.");
|
|
} else
|
|
if(verifyResult == PluginVerifyResult.FAIL_CODE){
|
|
Bukkit.getConsoleSender().sendMessage("[验证日志 - 拦截] "+p.getName()+" 的直播间尚未进行授权. "+roomId);
|
|
p.sendMessage("§c[系统]§a验证尚未通过,直播间§e<"+roomId+">§a尚未进行授权.");
|
|
} else
|
|
if(verifyResult == PluginVerifyResult.FAIL_TIMEOUT){
|
|
Bukkit.getConsoleSender().sendMessage("[验证日志 - 拦截] "+p.getName()+" 的本地网络有问题,无法连接验证服务器.");
|
|
p.sendMessage("§c[系统]§a验证尚未通过,您当前的网络环境有问题.");
|
|
} else {
|
|
Bukkit.getConsoleSender().sendMessage("[验证日志 - 拦截] "+p.getName()+" 的无法通过验证. "+verifyResult);
|
|
p.sendMessage("§c[系统]§a验证尚未通过,无法连接直播间,请联系管理员。§c§l#" + verifyResult);
|
|
}
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public static String getAESRandomKey() {
|
|
SecureRandom random = new SecureRandom();
|
|
long randomKey = random.nextLong();
|
|
return String.valueOf(randomKey);
|
|
}
|
|
|
|
public static String encrypt(String content, String key) {
|
|
try {
|
|
Cipher cipher = Cipher.getInstance(convertString("0100000101000101010100110010111101000101010000110100001000101111010100000100"+DEFAULT_CIPHER_ALGORITHM));
|
|
byte[] byteContent = content.getBytes("utf-8");
|
|
cipher.init(Cipher.ENCRYPT_MODE, getSecretKey(key));
|
|
byte[] result = cipher.doFinal(byteContent);
|
|
return byte2Base64(result);
|
|
} catch (Exception ex) {
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public static String decrypt(String content, String key) {
|
|
try {
|
|
Cipher cipher = Cipher.getInstance(convertString("0100000101000101010100110010111101000101010000110100001000101111010100000100"+DEFAULT_CIPHER_ALGORITHM));
|
|
cipher.init(Cipher.DECRYPT_MODE, getSecretKey(key));
|
|
byte[] result = cipher.doFinal(base642Byte(content));
|
|
return new String(result, "utf-8");
|
|
} catch (Exception ex) {
|
|
}
|
|
return null;
|
|
}
|
|
|
|
private static SecretKeySpec getSecretKey(final String key) {
|
|
try {
|
|
KeyGenerator kg = KeyGenerator.getInstance(KEY_ALGORITHM);
|
|
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
|
|
random.setSeed(key.getBytes());
|
|
kg.init(128, random);
|
|
SecretKey secretKey = kg.generateKey();
|
|
return new SecretKeySpec(secretKey.getEncoded(), KEY_ALGORITHM);
|
|
} catch (NoSuchAlgorithmException ex) {
|
|
}
|
|
return null;
|
|
}
|
|
|
|
private static String byte2Base64(byte[] bytes) {
|
|
return Base64.getEncoder().encodeToString(bytes);
|
|
}
|
|
|
|
private static byte[] base642Byte(String base64Key) {
|
|
return Base64.getDecoder().decode(base64Key);
|
|
}
|
|
} |