2020-10-29 08:01:36 +00:00
|
|
|
package net.limework.rediskript.data;
|
2020-06-28 14:40:53 +00:00
|
|
|
|
|
|
|
import org.cryptomator.siv.SivMode;
|
|
|
|
import org.cryptomator.siv.UnauthenticCiphertextException;
|
|
|
|
|
|
|
|
import javax.crypto.IllegalBlockSizeException;
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
|
|
|
|
public class Encryption {
|
|
|
|
|
2021-05-16 22:10:02 +00:00
|
|
|
private final boolean encryptionEnabled;
|
2020-06-28 14:40:53 +00:00
|
|
|
private String encryptionKey;
|
|
|
|
private String macKey;
|
|
|
|
private final SivMode AES_SIV = new SivMode();
|
|
|
|
|
2021-05-16 22:10:02 +00:00
|
|
|
public Encryption(boolean encryptionEnabled, String encryptionKey, String macKey){
|
|
|
|
this.encryptionEnabled = encryptionEnabled;
|
|
|
|
if (this.encryptionEnabled) {
|
2021-02-12 10:59:28 +00:00
|
|
|
// AES encryption
|
2021-05-16 22:10:02 +00:00
|
|
|
this.encryptionKey = encryptionKey;
|
2021-06-14 05:22:50 +00:00
|
|
|
this.macKey = macKey;
|
2020-06-28 14:40:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isEncryptionEnabled() { return encryptionEnabled; }
|
|
|
|
|
|
|
|
public String decrypt(byte[] message) throws UnauthenticCiphertextException, IllegalBlockSizeException {
|
2020-10-22 10:30:41 +00:00
|
|
|
return new String(AES_SIV.decrypt(encryptionKey.getBytes(StandardCharsets.UTF_8), macKey.getBytes(StandardCharsets.UTF_8), message), StandardCharsets.UTF_8);
|
2020-06-28 14:40:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public byte[] encrypt(String message) {
|
2020-10-22 10:30:41 +00:00
|
|
|
return AES_SIV.encrypt(encryptionKey.getBytes(StandardCharsets.UTF_8), macKey.getBytes(StandardCharsets.UTF_8), message.getBytes(StandardCharsets.UTF_8));
|
2020-06-28 14:40:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|