Add AES-128 encryption

This commit is contained in:
Govindas 2020-05-27 14:43:16 +03:00
parent b6a8b5005c
commit ef03a7cc72
6 changed files with 106 additions and 19 deletions

9
.idea/discord.xml Normal file
View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="DiscordProjectSettings">
<option name="show" value="true" />
</component>
<component name="ProjectNotificationSettings">
<option name="askShowProject" value="false" />
</component>
</project>

View File

@ -7,11 +7,13 @@
</component>
<component name="ChangeListManager">
<list default="true" id="61139119-6327-48a6-9183-0df6346ed8d8" name="Default Changelist" comment="">
<change afterPath="$PROJECT_DIR$/.idea/discord.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/net/limework/skLimework/AddonPlugin.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/net/limework/skLimework/AddonPlugin.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/net/limework/skLimework/Events/RedisSub.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/net/limework/skLimework/Events/RedisSub.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/net/limework/skLimework/Events/onRedisMessage.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/net/limework/skLimework/Events/onRedisMessage.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/net/limework/skLimework/elements/EffSendMessage.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/net/limework/skLimework/elements/EffSendMessage.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/resources/config.yml" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/resources/config.yml" afterDir="false" />
</list>
<option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
@ -35,6 +37,10 @@
</option>
</component>
<component name="ProjectId" id="1ZwZbhHZiGU2Gkz7zWjyRIlJngw" />
<component name="ProjectViewState">
<option name="showExcludedFiles" value="true" />
<option name="showLibraryContents" value="true" />
</component>
<component name="PropertiesComponent">
<property name="GenerateAntBuildDialog.backupFiles" value="true" />
<property name="GenerateAntBuildDialog.enableUiFormCompile" value="true" />
@ -52,18 +58,6 @@
<recent name="net.limework.skLimework.elements" />
</key>
</component>
<component name="RunDashboard">
<option name="ruleStates">
<list>
<RuleState>
<option name="name" value="ConfigurationTypeDashboardGroupingRule" />
</RuleState>
<RuleState>
<option name="name" value="StatusDashboardGroupingRule" />
</RuleState>
</list>
</option>
</component>
<component name="RunManager">
<configuration name="Tester" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="net.limework.skLimework.DoNotUse.Tester" />
@ -84,6 +78,9 @@
</list>
</recent_temporary>
</component>
<component name="SQLPlugin.ProjectConfiguration">
<queries />
</component>
<component name="SvnConfiguration">
<configuration />
</component>

View File

@ -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");
@ -53,7 +97,6 @@ public class AddonPlugin extends JavaPlugin {
} catch (IOException e) { e.printStackTrace(); }
JedisPoolConfig jconfig = new JedisPoolConfig();
jconfig.setMaxTotal(this.getConfig().getInt("Redis.MaxConnections"));
@ -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;
}
}

View File

@ -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,8 +15,14 @@ public class onRedisMessage extends Event {
onRedisMessage(String channelName , String message) {
super(true);
this.channelName = channelName;
AddonPlugin instance = (AddonPlugin) Bukkit.getPluginManager().getPlugin("SKLimework");
assert instance != null;
if (instance.isEncryptionEnabled()) {
this.message = instance.decrypt(message);
} else {
this.message = message;
}
}
@Override

View File

@ -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 {
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());

View File

@ -6,6 +6,9 @@ Redis:
Port: 6379
TimeOut: 40000
useSSL: true
#useful if SSL is disabled
EncryptMessages: false
EncryptionKey: "16CHARACTERS KEY"
Channels:
- "Channel1"