Finish AES encryption

This commit is contained in:
Govindas 2020-05-27 16:22:05 +03:00
parent a6b6379234
commit 9d7fbcd33b
2 changed files with 24 additions and 8 deletions

View File

@ -7,8 +7,8 @@
</component>
<component name="ChangeListManager">
<list default="true" id="61139119-6327-48a6-9183-0df6346ed8d8" name="Default Changelist" comment="">
<change beforePath="$PROJECT_DIR$/.idea/artifacts/SkLimework_jar.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/artifacts/SkLimework_jar.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/SkLimework.jar" beforeDir="false" afterPath="$PROJECT_DIR$/SkLimework.jar" 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" />
</list>
<option name="SHOW_DIALOG" value="false" />

View File

@ -40,7 +40,8 @@ public class AddonPlugin extends JavaPlugin {
private JedisPool jedisPool;
private RedisSub redisSub;
private ExecutorService service;
private Cipher cipher;
private Cipher encryptionCipher;
private Cipher decryptionCipher;
private boolean encryptionEnabled;
@Override
@ -66,14 +67,29 @@ public class AddonPlugin extends JavaPlugin {
key = Arrays.copyOf(key, 16);
SecretKeySpec encryptionKey = new SecretKeySpec(key, "AES");
cipher = null;
encryptionCipher = null;
try {
cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
encryptionCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
}
try {
cipher.init(Cipher.ENCRYPT_MODE, encryptionKey);
encryptionCipher.init(Cipher.ENCRYPT_MODE, encryptionKey);
} catch (InvalidKeyException e) {
e.printStackTrace();
}
decryptionCipher = null;
try {
decryptionCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
}
try {
decryptionCipher.init(Cipher.DECRYPT_MODE, encryptionKey);
} catch (InvalidKeyException e) {
e.printStackTrace();
}
@ -145,7 +161,7 @@ public class AddonPlugin extends JavaPlugin {
public String encrypt(String message) {
String encrypted = null;
try {
encrypted = Base64.getEncoder().encodeToString(cipher.doFinal(message.getBytes(StandardCharsets.UTF_8)));
encrypted = Base64.getEncoder().encodeToString(encryptionCipher.doFinal(message.getBytes(StandardCharsets.UTF_8)));
} catch (IllegalBlockSizeException | BadPaddingException e) {
e.printStackTrace();
}
@ -155,7 +171,7 @@ public class AddonPlugin extends JavaPlugin {
public String decrypt(String message) {
String decrypted = null;
try {
decrypted = new String(cipher.doFinal(Base64.getDecoder().decode(message)), StandardCharsets.UTF_8);
decrypted = new String(decryptionCipher.doFinal(Base64.getDecoder().decode(message)), StandardCharsets.UTF_8);
} catch (IllegalBlockSizeException | BadPaddingException e) {
e.printStackTrace();
}