diff --git a/.idea/artifacts/SkLimework_jar.xml b/.idea/artifacts/SkLimework_jar.xml index d932e82..213b017 100644 --- a/.idea/artifacts/SkLimework_jar.xml +++ b/.idea/artifacts/SkLimework_jar.xml @@ -7,6 +7,7 @@ + diff --git a/.idea/dictionaries/Govindas.xml b/.idea/dictionaries/Govindas.xml new file mode 100644 index 0000000..4035677 --- /dev/null +++ b/.idea/dictionaries/Govindas.xml @@ -0,0 +1,7 @@ + + + + jedis + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml index db570e2..b0601b2 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -7,9 +7,16 @@ + + + + + + + - + - + + + + + + + + + + + + + diff --git a/SkLimework.jar b/SkLimework.jar index 86e3270..7ab3dbe 100644 Binary files a/SkLimework.jar and b/SkLimework.jar differ diff --git a/pom.xml b/pom.xml index fb09dcd..84e13d0 100644 --- a/pom.xml +++ b/pom.xml @@ -87,5 +87,10 @@ json 20190722 + + org.cryptomator + siv-mode + 1.4.0 + \ No newline at end of file diff --git a/src/main/java/net/limework/skLimework/AddonPlugin.java b/src/main/java/net/limework/skLimework/AddonPlugin.java index 57d6014..1dd5714 100644 --- a/src/main/java/net/limework/skLimework/AddonPlugin.java +++ b/src/main/java/net/limework/skLimework/AddonPlugin.java @@ -13,6 +13,8 @@ import net.limework.skLimework.elements.ExprMessage; import org.bukkit.Bukkit; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.plugin.java.JavaPlugin; +import org.cryptomator.siv.SivMode; +import org.cryptomator.siv.UnauthenticCiphertextException; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; @@ -40,9 +42,10 @@ public class AddonPlugin extends JavaPlugin { private JedisPool jedisPool; private RedisSub redisSub; private ExecutorService service; - private Cipher encryptionCipher; - private Cipher decryptionCipher; private boolean encryptionEnabled; + private String encryptionKey; + private String macKey; + private final SivMode AES_SIV = new SivMode(); @Override public void onEnable(){ @@ -52,47 +55,8 @@ public class AddonPlugin extends JavaPlugin { 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(); - } - assert sha != null; - key = sha.digest(key); - key = Arrays.copyOf(key, 16); - SecretKeySpec encryptionKey = new SecretKeySpec(key, "AES"); - - encryptionCipher = null; - try { - encryptionCipher = Cipher.getInstance("AES/SIV/PKCS5Padding"); - } catch (NoSuchAlgorithmException e) { - e.printStackTrace(); - } catch (NoSuchPaddingException e) { - e.printStackTrace(); - } - try { - encryptionCipher.init(Cipher.ENCRYPT_MODE, encryptionKey); - } catch (InvalidKeyException e) { - e.printStackTrace(); - } - decryptionCipher = null; - try { - decryptionCipher = Cipher.getInstance("AES/SIV/PKCS5Padding"); - } catch (NoSuchAlgorithmException e) { - e.printStackTrace(); - } catch (NoSuchPaddingException e) { - e.printStackTrace(); - } - try { - decryptionCipher.init(Cipher.DECRYPT_MODE, encryptionKey); - } catch (InvalidKeyException e) { - e.printStackTrace(); - } + encryptionKey = config.getString("Redis.EncryptionKey"); + macKey = config.getString("Redis.MacKey"); } addon = Skript.registerAddon(this); @@ -157,23 +121,11 @@ public class AddonPlugin extends JavaPlugin { public boolean isEncryptionEnabled() { return encryptionEnabled; } - public String encrypt(String message) { - String encrypted = null; - try { - encrypted = Base64.getEncoder().encodeToString(encryptionCipher.doFinal(message.getBytes(StandardCharsets.UTF_8))); - } catch (IllegalBlockSizeException | BadPaddingException e) { - e.printStackTrace(); - } - return encrypted; + public String decrypt(byte[] message) throws UnauthenticCiphertextException, IllegalBlockSizeException { + return new String(AES_SIV.decrypt(encryptionKey.getBytes(), macKey.getBytes(), message), StandardCharsets.UTF_8); } - public String decrypt(String message) { - String decrypted = null; - try { - decrypted = new String(decryptionCipher.doFinal(Base64.getDecoder().decode(message)), StandardCharsets.UTF_8); - } catch (IllegalBlockSizeException | BadPaddingException e) { - e.printStackTrace(); - } - return decrypted; + public byte[] encrypt(String message) { + return AES_SIV.encrypt(encryptionKey.getBytes(), macKey.getBytes(), message.getBytes()); } } diff --git a/src/main/java/net/limework/skLimework/Events/RedisSub.java b/src/main/java/net/limework/skLimework/Events/RedisSub.java index a99ad32..046fdc5 100644 --- a/src/main/java/net/limework/skLimework/Events/RedisSub.java +++ b/src/main/java/net/limework/skLimework/Events/RedisSub.java @@ -3,31 +3,35 @@ package net.limework.skLimework.Events; import net.limework.skLimework.AddonPlugin; import org.bukkit.Bukkit; import org.bukkit.ChatColor; +import org.cryptomator.siv.UnauthenticCiphertextException; import org.json.JSONObject; -import redis.clients.jedis.Jedis; -import redis.clients.jedis.JedisPubSub; +import redis.clients.jedis.BinaryJedis; +import redis.clients.jedis.BinaryJedisPubSub; import redis.clients.jedis.exceptions.JedisConnectionException; +import javax.crypto.IllegalBlockSizeException; +import java.util.Collections; import java.util.List; -public class RedisSub extends JedisPubSub implements Runnable{ +public class RedisSub extends BinaryJedisPubSub implements Runnable{ private AddonPlugin plugin; - private Jedis j; - private String[] channels; + private BinaryJedis j; private Boolean isShuttingDown = false; + private List channels; - public RedisSub(AddonPlugin plugin, Jedis j, List channels) { + public RedisSub(AddonPlugin plugin, BinaryJedis j, List channels) { this.plugin = plugin; this.j = j; - this.channels = channels.toArray(new String[0]); + this.channels = channels; } @Override public void run(){ try{ - this.j.subscribe(this, channels); + this.j.subscribe(this, channels.get(0).getBytes(), channels.get(1).getBytes(), channels.get(2).getBytes(), channels.get(3).getBytes(), channels.get(4).getBytes()); + } catch (Exception je){ plugin.getLogger().warning("Lost connection to redis!"); newJedis(); @@ -56,11 +60,21 @@ public class RedisSub extends JedisPubSub implements Runnable{ @Override - public void onMessage(String channel, String message) { + public void onMessage(byte[] channel, byte[] message) { + AddonPlugin plugin = (AddonPlugin) Bukkit.getPluginManager().getPlugin("SKLimework"); + String channelString = new String(channel); try { - JSONObject json = new JSONObject(message); + String decrypted = null; + try { + assert plugin != null; + decrypted = plugin.decrypt(message); + } catch (UnauthenticCiphertextException | IllegalBlockSizeException e) { + e.printStackTrace(); + } + assert decrypted != null; + JSONObject j = new JSONObject(decrypted); //System.out.println("Message got from channel: "+channel +" and the Message: " +json.toString()); - plugin.getServer().getPluginManager().callEvent(new onRedisMessage(channel, json.getString("Message"))); + plugin.getServer().getPluginManager().callEvent(new onRedisMessage(channelString, j.getString("Message"))); } catch (Exception e) { e.printStackTrace(); Bukkit.getLogger().warning(ChatColor.translateAlternateColorCodes('&', "&2[&aGBot&a] &cI Got a Message that Was empty from channel "+ channel +" Please check your code that you used to send the message. ^ ignore the error.")); diff --git a/src/main/java/net/limework/skLimework/Events/onRedisMessage.java b/src/main/java/net/limework/skLimework/Events/onRedisMessage.java index eade583..285a75e 100644 --- a/src/main/java/net/limework/skLimework/Events/onRedisMessage.java +++ b/src/main/java/net/limework/skLimework/Events/onRedisMessage.java @@ -17,11 +17,7 @@ public class onRedisMessage extends Event { 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; - } + this.message = message; } diff --git a/src/main/java/net/limework/skLimework/elements/EffSendMessage.java b/src/main/java/net/limework/skLimework/elements/EffSendMessage.java index 96b4911..504567b 100644 --- a/src/main/java/net/limework/skLimework/elements/EffSendMessage.java +++ b/src/main/java/net/limework/skLimework/elements/EffSendMessage.java @@ -11,9 +11,11 @@ import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.event.Event; import org.json.JSONObject; -import redis.clients.jedis.Jedis; +import redis.clients.jedis.BinaryJedis; import redis.clients.jedis.exceptions.JedisConnectionException; +import java.nio.charset.StandardCharsets; + public class EffSendMessage extends Effect { //"hi" static { @@ -36,18 +38,19 @@ public class EffSendMessage extends Effect { } assert plugin != null; plugin.getJedisExecutionService().execute(() -> { - Jedis j = plugin.getJedisPool().getResource(); + BinaryJedis 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()); - }catch (Exception e){e.printStackTrace();} + json.put("Message", message); + json.put("Type", "Skript"); + json.put("Date", System.nanoTime()); //for unique string every time & PING calculations + byte[] msg; + if (plugin.isEncryptionEnabled()) { + msg = plugin.encrypt(json.toString()); + } else { + msg = message.getBytes(StandardCharsets.UTF_8); + } + j.publish(channel.getBytes(), msg); + //System.out.println("SkriptSide sent MESSAGE: ["+ message + "] to channel: " + channel + " and json: \n" + json.toString()); j.close(); }); diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 0b743d1..ee6d0b5 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -9,6 +9,7 @@ Redis: #useful if SSL is disabled EncryptMessages: false EncryptionKey: "16CHARACTERS KEY" + MacKey: "16CHARACTERS KEY" Channels: - "Channel1"