Highly improve encryption security

This commit is contained in:
Govindass
2020-06-08 16:53:23 +03:00
parent 691b47c558
commit 3dffba07df
10 changed files with 87 additions and 89 deletions

View File

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

View File

@@ -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<String> channels;
public RedisSub(AddonPlugin plugin, Jedis j, List<String> channels) {
public RedisSub(AddonPlugin plugin, BinaryJedis j, List<String> 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."));

View File

@@ -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;
}

View File

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