forked from Limework/RediSkript
Add AES-128 encryption
This commit is contained in:
@@ -11,12 +11,25 @@ import net.limework.skLimework.elements.EvtRedis;
|
||||
import net.limework.skLimework.elements.ExprChannel;
|
||||
import net.limework.skLimework.elements.ExprMessage;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import redis.clients.jedis.JedisPool;
|
||||
import redis.clients.jedis.JedisPoolConfig;
|
||||
|
||||
import javax.crypto.BadPaddingException;
|
||||
import javax.crypto.Cipher;
|
||||
import javax.crypto.IllegalBlockSizeException;
|
||||
import javax.crypto.NoSuchPaddingException;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
import java.beans.Expression;
|
||||
import java.io.IOException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.InvalidKeyException;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Base64;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
@@ -27,12 +40,43 @@ public class AddonPlugin extends JavaPlugin {
|
||||
private JedisPool jedisPool;
|
||||
private RedisSub redisSub;
|
||||
private ExecutorService service;
|
||||
|
||||
private Cipher cipher;
|
||||
private boolean encryptionEnabled;
|
||||
|
||||
@Override
|
||||
public void onEnable(){
|
||||
instance = this;
|
||||
this.saveDefaultConfig();
|
||||
FileConfiguration config = this.getConfig();
|
||||
encryptionEnabled = config.getBoolean("Redis.EncryptMessages");
|
||||
if (encryptionEnabled) {
|
||||
// AES-128 encryption
|
||||
String configKey = config.getString("Redis.EncryptionKey");
|
||||
byte[] key = null;
|
||||
assert configKey != null;
|
||||
key = configKey.getBytes(StandardCharsets.UTF_8);
|
||||
MessageDigest sha = null;
|
||||
try {
|
||||
sha = MessageDigest.getInstance("SHA-1");
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
key = Arrays.copyOf(key, 16);
|
||||
SecretKeySpec encryptionKey = new SecretKeySpec(key, "AES");
|
||||
|
||||
cipher = null;
|
||||
try {
|
||||
cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
|
||||
} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
try {
|
||||
cipher.init(Cipher.ENCRYPT_MODE, encryptionKey);
|
||||
} catch (InvalidKeyException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
addon = Skript.registerAddon(this);
|
||||
try { addon.loadClasses("net.limework.skLimework", "elements");
|
||||
Skript.registerEvent("redis message", EvtRedis.class, onRedisMessage.class, "redis message");
|
||||
@@ -51,8 +95,7 @@ public class AddonPlugin extends JavaPlugin {
|
||||
}
|
||||
}, 0);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
} catch (IOException e) { e.printStackTrace(); }
|
||||
JedisPoolConfig jconfig = new JedisPoolConfig();
|
||||
@@ -94,4 +137,26 @@ public class AddonPlugin extends JavaPlugin {
|
||||
}
|
||||
|
||||
public JedisPool getJedisPool() { return jedisPool; }
|
||||
|
||||
public boolean isEncryptionEnabled() { return encryptionEnabled; }
|
||||
|
||||
public String encrypt(String message) {
|
||||
String encrypted = null;
|
||||
try {
|
||||
encrypted = Base64.getEncoder().encodeToString(cipher.doFinal(message.getBytes(StandardCharsets.UTF_8)));
|
||||
} catch (IllegalBlockSizeException | BadPaddingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return encrypted;
|
||||
}
|
||||
|
||||
public String decrypt(String message) {
|
||||
String decrypted = null;
|
||||
try {
|
||||
decrypted = new String(cipher.doFinal(Base64.getDecoder().decode(message)), StandardCharsets.UTF_8);
|
||||
} catch (IllegalBlockSizeException | BadPaddingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return decrypted;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package net.limework.skLimework.Events;
|
||||
|
||||
import net.limework.skLimework.AddonPlugin;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.event.Event;
|
||||
import org.bukkit.event.HandlerList;
|
||||
|
||||
@@ -13,7 +15,13 @@ public class onRedisMessage extends Event {
|
||||
onRedisMessage(String channelName , String message) {
|
||||
super(true);
|
||||
this.channelName = channelName;
|
||||
this.message = message;
|
||||
AddonPlugin instance = (AddonPlugin) Bukkit.getPluginManager().getPlugin("SKLimework");
|
||||
assert instance != null;
|
||||
if (instance.isEncryptionEnabled()) {
|
||||
this.message = instance.decrypt(message);
|
||||
} else {
|
||||
this.message = message;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -34,11 +34,16 @@ public class EffSendMessage extends Effect {
|
||||
Bukkit.getLogger().warning(ChatColor.translateAlternateColorCodes('&', "&2[&aGBot&a] &cMessage Was empty Please check your code."));
|
||||
return;
|
||||
}
|
||||
assert plugin != null;
|
||||
plugin.getJedisExecutionService().execute(() -> {
|
||||
Jedis j = plugin.getJedisPool().getResource();
|
||||
JSONObject json = new JSONObject();
|
||||
try {
|
||||
json.put("Message", message);
|
||||
if (plugin.isEncryptionEnabled()) {
|
||||
json.put("Message", plugin.encrypt(message));
|
||||
} else {
|
||||
json.put("Message", message);
|
||||
}
|
||||
json.put("Type", "Skript");
|
||||
j.publish(channel, json.toString());
|
||||
//System.out.println("SkriptSide sent MESSAGE: ["+ message + "] to channel: " + channel + " and json: \n" + json.toString());
|
||||
|
||||
Reference in New Issue
Block a user